Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default How to write an If Then VBA

I am having trouble writing an If Then VBA that inserts the correct info into
my worksheet. See code below. I have a userform tha inserts all the info in
the correct rows but does not give me my answer.

Private Sub CommandButtonOk_Click()
If TextBoxName.Text = "" Then
MsgBox "You must enter a name."
Exit Sub
End If
NextBlankRow = Application.WorksheetFunction.CountA(Range("ColA") ) + 1
'MsgBox (NextBlankRow)
Cells(NextBlankRow, 1) = TextBoxName.Text
Cells(NextBlankRow, 2) = TextBoxAge.Text

If TextBoxAge = 3 And TextBoxAge <= 12 Then
Events = "Kids"
ElseIf TextBoxAge = 13 And TextBoxAge <= 19 Then
Events = "Teens"
ElseIf TextBoxAge = 20 And TextBoxAge <= 49 Then
Events = "Adults"
ElseIf TextBoxAge = 50 Then
Events = "Seniors"
End If

TextBoxName.Text = ""

In Excel I have Column 3 named Events and when I type the name and age I
would like the the event column filled in based on the above data.

Can anyone help?
TextBoxAge.Text = ""

TextBoxName.SetFocus

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to write an If Then VBA

Private Sub CommandButtonOk_Click()
Dim events as String

.. . .

Cells(NextBlankRow, 2) = TextBoxAge.Text

If TextBoxAge = 3 And TextBoxAge <= 12 Then
Events = "Kids"
ElseIf TextBoxAge = 13 And TextBoxAge <= 19 Then
Events = "Teens"
ElseIf TextBoxAge = 20 And TextBoxAge <= 49 Then
Events = "Adults"
ElseIf TextBoxAge = 50 Then
Events = "Seniors"
End If

cells(NetBlankRow,Range("Events").Column).Value = events

--
Regards,
Tom Ogilvy

"Marcie" wrote in message
...
I am having trouble writing an If Then VBA that inserts the correct info
into
my worksheet. See code below. I have a userform tha inserts all the info
in
the correct rows but does not give me my answer.

Private Sub CommandButtonOk_Click()
If TextBoxName.Text = "" Then
MsgBox "You must enter a name."
Exit Sub
End If
NextBlankRow = Application.WorksheetFunction.CountA(Range("ColA") ) + 1
'MsgBox (NextBlankRow)
Cells(NextBlankRow, 1) = TextBoxName.Text
Cells(NextBlankRow, 2) = TextBoxAge.Text

If TextBoxAge = 3 And TextBoxAge <= 12 Then
Events = "Kids"
ElseIf TextBoxAge = 13 And TextBoxAge <= 19 Then
Events = "Teens"
ElseIf TextBoxAge = 20 And TextBoxAge <= 49 Then
Events = "Adults"
ElseIf TextBoxAge = 50 Then
Events = "Seniors"
End If

TextBoxName.Text = ""

In Excel I have Column 3 named Events and when I type the name and age I
would like the the event column filled in based on the above data.

Can anyone help?
TextBoxAge.Text = ""

TextBoxName.SetFocus

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default How to write an If Then VBA

Tom,

Thank you so much. It worked. I knew I was missing something, just didn't
know what.
Marcie

"Tom Ogilvy" wrote:

Private Sub CommandButtonOk_Click()
Dim events as String

.. . .

Cells(NextBlankRow, 2) = TextBoxAge.Text

If TextBoxAge = 3 And TextBoxAge <= 12 Then
Events = "Kids"
ElseIf TextBoxAge = 13 And TextBoxAge <= 19 Then
Events = "Teens"
ElseIf TextBoxAge = 20 And TextBoxAge <= 49 Then
Events = "Adults"
ElseIf TextBoxAge = 50 Then
Events = "Seniors"
End If

cells(NetBlankRow,Range("Events").Column).Value = events

--
Regards,
Tom Ogilvy

"Marcie" wrote in message
...
I am having trouble writing an If Then VBA that inserts the correct info
into
my worksheet. See code below. I have a userform tha inserts all the info
in
the correct rows but does not give me my answer.

Private Sub CommandButtonOk_Click()
If TextBoxName.Text = "" Then
MsgBox "You must enter a name."
Exit Sub
End If
NextBlankRow = Application.WorksheetFunction.CountA(Range("ColA") ) + 1
'MsgBox (NextBlankRow)
Cells(NextBlankRow, 1) = TextBoxName.Text
Cells(NextBlankRow, 2) = TextBoxAge.Text

If TextBoxAge = 3 And TextBoxAge <= 12 Then
Events = "Kids"
ElseIf TextBoxAge = 13 And TextBoxAge <= 19 Then
Events = "Teens"
ElseIf TextBoxAge = 20 And TextBoxAge <= 49 Then
Events = "Adults"
ElseIf TextBoxAge = 50 Then
Events = "Seniors"
End If

TextBoxName.Text = ""

In Excel I have Column 3 named Events and when I type the name and age I
would like the the event column filled in based on the above data.

Can anyone help?
TextBoxAge.Text = ""

TextBoxName.SetFocus

End Sub




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
re write [email protected][_2_] Excel Programming 2 April 6th 06 12:36 PM
is it possible to execute write to the fields in another .xsl form a macro in another .xsl? e.g. some way to load another .xsl into an .xsl macro and write to its data? Daniel Excel Worksheet Functions 1 June 23rd 05 11:38 PM
Better way to write this Steph[_3_] Excel Programming 2 May 11th 05 11:33 PM
How to I write a UDF? Dr. Juzzy Excel Discussion (Misc queries) 1 January 30th 05 02:17 PM
Better way to write this VBA Heather[_5_] Excel Programming 0 September 29th 03 06:54 PM


All times are GMT +1. The time now is 10:24 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"