Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default Qn: Define a variable in Initialize for Userform

Hello Everyone,

How do I define a variable in order to use in the initialize part of a
userform. I am using John's Walkenbach's Help File example and I wanted the
program to default to a certain Help File based on info provided in 1 cell.
I have 4 airplanes, the airplane is selected in Cells(30,1). Based on which
airplane I select gives the topic number to start off with. This is what I
have below, but when I run it, I get errors, it doesn't like Airplane.Text,
because I don't think it is defined anywhere??. How can I read what
airplane is in the cell to select the correct topic??
(This code does NOT work)

Airplane.Text = Cells(30, 1)
If Airplane = "N2AJ" Then CurrentTopic = 1
If Airplane = "N9421D" Then CurrentTopic = 2
If Airplane = "N74NM" Then CurrentTopic = 3
If Airplane = "N146K" Then CurrentTopic = 4 Else CurrentTopic = 1



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Define a variable in Initialize for Userform

Hi Michael,

Try:

Sub Tester()
Dim Airplane As String
Dim CurrentTopic As Long
Airplane = Cells(1, 1).Value

Select Case Airplane
Case "N2AJ"
CurrentTopic = 1
Case "N9421D"
CurrentTopic = 2
Case "N74NM"
CurrentTopic = 3
Case "N146K"
CurrentTopic = 4
Case Else
CurrentTopic = 1
End Select
MsgBox CurrentTopic

End Sub


---
Regards,
Norman



"Michael Vaughan" wrote in message
...
Hello Everyone,

How do I define a variable in order to use in the initialize part of a
userform. I am using John's Walkenbach's Help File example and I wanted

the
program to default to a certain Help File based on info provided in 1

cell.
I have 4 airplanes, the airplane is selected in Cells(30,1). Based on

which
airplane I select gives the topic number to start off with. This is what

I
have below, but when I run it, I get errors, it doesn't like

Airplane.Text,
because I don't think it is defined anywhere??. How can I read what
airplane is in the cell to select the correct topic??
(This code does NOT work)

Airplane.Text = Cells(30, 1)
If Airplane = "N2AJ" Then CurrentTopic = 1
If Airplane = "N9421D" Then CurrentTopic = 2
If Airplane = "N74NM" Then CurrentTopic = 3
If Airplane = "N146K" Then CurrentTopic = 4 Else CurrentTopic = 1





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Define a variable in Initialize for Userform

Hi Michael,

Forgot to change:
Airplane = Cells(1, 1).Value
back to your address:
Airplane = Cells(30, 1).Value

---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Michael,

Try:

Sub Tester()
Dim Airplane As String
Dim CurrentTopic As Long
Airplane = Cells(1, 1).Value

Select Case Airplane
Case "N2AJ"
CurrentTopic = 1
Case "N9421D"
CurrentTopic = 2
Case "N74NM"
CurrentTopic = 3
Case "N146K"
CurrentTopic = 4
Case Else
CurrentTopic = 1
End Select
MsgBox CurrentTopic

End Sub


---
Regards,
Norman



"Michael Vaughan" wrote in message
...
Hello Everyone,

How do I define a variable in order to use in the initialize part of a
userform. I am using John's Walkenbach's Help File example and I wanted

the
program to default to a certain Help File based on info provided in 1

cell.
I have 4 airplanes, the airplane is selected in Cells(30,1). Based on

which
airplane I select gives the topic number to start off with. This is

what
I
have below, but when I run it, I get errors, it doesn't like

Airplane.Text,
because I don't think it is defined anywhere??. How can I read what
airplane is in the cell to select the correct topic??
(This code does NOT work)

Airplane.Text = Cells(30, 1)
If Airplane = "N2AJ" Then CurrentTopic = 1
If Airplane = "N9421D" Then CurrentTopic = 2
If Airplane = "N74NM" Then CurrentTopic = 3
If Airplane = "N146K" Then CurrentTopic = 4 Else CurrentTopic = 1







  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default Define a variable in Initialize for Userform

Hi Norman,

Thanks for the help. I got it to work now, but had problems with the code
that you gave. Initially I had the code in:

Private Sub UserForm_Initialize()
' Executed before the form is shown
Dim Row As Integer
Set HelpSheet = ThisWorkbook.Sheets(HelpSheetName)
TopicCount =
Application.WorksheetFunction.CountA(HelpSheet.Ran ge("A:A"))
For Row = 1 To TopicCount
ComboBoxTopics.AddItem HelpSheet.Cells(Row, 1)
Next Row
ComboBoxTopics.ListIndex = 0

Dim Airplane As String
Airplane = Cells(10, 5).Value
If Airplane = "N2AJ" Then CurrentTopic = 1
If Airplane = "N9421D" Then CurrentTopic = 2
If Airplane = "N74NM" Then CurrentTopic = 3
If Airplane = "N146K" Then CurrentTopic = 4
UpdateForm
End Sub

When I added your Sub Tester() within it, it didn't like that. So, I guess
you can't have a sub within a sub??? Should I have put Tester right before
the UpdateForm (see above) to run the subroutine Tester??

Sub Tester()
Dim Airplane As String
Dim CurrentTopic As Long
Airplane = Cells(1, 1).Value

"Norman Jones" wrote in message
...
Hi Michael,

Try:

Sub Tester()
Dim Airplane As String
Dim CurrentTopic As Long
Airplane = Cells(1, 1).Value

Select Case Airplane
Case "N2AJ"
CurrentTopic = 1
Case "N9421D"
CurrentTopic = 2
Case "N74NM"
CurrentTopic = 3
Case "N146K"
CurrentTopic = 4
Case Else
CurrentTopic = 1
End Select
MsgBox CurrentTopic

End Sub


---
Regards,
Norman



"Michael Vaughan" wrote in message
...
Hello Everyone,

How do I define a variable in order to use in the initialize part of a
userform. I am using John's Walkenbach's Help File example and I wanted

the
program to default to a certain Help File based on info provided in 1

cell.
I have 4 airplanes, the airplane is selected in Cells(30,1). Based on

which
airplane I select gives the topic number to start off with. This is

what
I
have below, but when I run it, I get errors, it doesn't like

Airplane.Text,
because I don't think it is defined anywhere??. How can I read what
airplane is in the cell to select the correct topic??
(This code does NOT work)

Airplane.Text = Cells(30, 1)
If Airplane = "N2AJ" Then CurrentTopic = 1
If Airplane = "N9421D" Then CurrentTopic = 2
If Airplane = "N74NM" Then CurrentTopic = 3
If Airplane = "N146K" Then CurrentTopic = 4 Else CurrentTopic = 1







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Define a variable in Initialize for Userform

Hi Michael,

Should I have put Tester right before
the UpdateForm (see above) to run the subroutine Tester??


It would have been clearer If I had named my sub as Sub
UserForm_Initialize(). The code between Sub Tester() and End Sub, was a
suggestion to replace your shown code.

I am pleased that your code now works but a couple of observations:

It is good programming practice explicitly to declare all variables. In this
connection, see Chip Pearson's detailed observations:

http://www.cpearson.com/excel/DeclaringVariables.htm
and
http://www.cpearson.com/excel/variables.htm

Additionally, from the perspectives of readability and code maintenance, I
would advocate that all variables be declared at the top of the procedure.

I replaced your multiple If ... Then form with a Select Case structure as a
matter of personal predelection rather than to suggest any error.


Regards,
Norman






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
Define Variable Name using VBA GTReferee Excel Discussion (Misc queries) 2 January 21st 09 06:37 PM
Userform Initialize & combo box values michaelberrier Excel Discussion (Misc queries) 3 June 27th 06 04:35 PM
Userform disappears when you try to initialize from a command button RPIJG[_60_] Excel Programming 8 July 2nd 04 08:14 PM
Define variable range acberry Excel Programming 2 May 12th 04 02:44 PM
Generic questions about variable scope the Initialize event TBA[_2_] Excel Programming 4 January 12th 04 03:42 PM


All times are GMT +1. The time now is 03:23 AM.

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

About Us

"It's about Microsoft Excel"