help using select case

LBoxington

New Member
Joined
Aug 16, 2011
Messages
6
So...recently I have been trying to learn how to do the macros on excel better and I recently purchased (recently defined as about a year ago, however, I didn't have much time to read it until recently due to school) called Excel 2007 Power Programming with VBA (since I have office 2007).

I am trying to experiment with using the select case construct.

Private testing_call()


Dim subtocall As String
Dim num As Variant

num = InputBox("Please input either the number 1 or 2")


Select Case num


Case 1: subtocall = "numname"
Case 2: subtocall = "firstname"
End Select
Application.rub subtocall
End Sub

Private Sub numname()

MsgBox "the number you typed is 1"


End Sub

Private Sub firstname()

MsgBox "The number you typed is 2"

End Sub

The line that I bolded is where I keep getting the error. It says "invalid outside procedure," for that line and I have no idea what I have done wrong.

Any ideas?

Another question. What is the difference between a class module and a module?

On a side note, this is my first post as I literally just registered like 10 minutes ago =) The significance of this statement is that if I posted in the wrong area or anything, let me know =).
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,
you couldn't have found a better place to post your questions.

The main thing is you forgot to declare the sub as a sub. You wrote:
Code:
Private testing_call()
Should be:
Code:
Private [COLOR="Blue"]Sub [/COLOR]testing_call()

Input boxes return (by default) text values, so I'd also ensure the return values are numeric values, and cast them to integers -- a "1" (string) is not the same as a 1 (numeric).

Code:
Private Sub testing_call()

Dim subtocall As String
Dim num As Variant

num = Application.InputBox("Please input either the number 1 or 2")

If IsNumeric(num) Then
    Select Case CLng(num)
        Case 1
            Call numname
        Case 2
            Call firstname
    End Select
Else
    MsgBox "Oops - you didn't enter a number 1 or 2"
End If

End Sub

Private Sub numname()

    MsgBox "the number you typed is 1"

End Sub

Private Sub firstname()

    MsgBox "The number you typed is 2"

End Sub

Worry about classes later ... if you know what they are, you'll know what to do with a class module. Otherwise, you'll use a standard module.
 
Upvote 0
Hi,
you couldn't have found a better place to post your questions.

The main thing is you forgot to declare the sub as a sub. You wrote:
Code:
Private testing_call()
Should be:
Code:
Private [COLOR=Blue]Sub [/COLOR]testing_call()
Input boxes return (by default) text values, so I'd also ensure the return values are numeric values, and cast them to integers -- a "1" (string) is not the same as a 1 (numeric).

Code:
Private Sub testing_call()

Dim subtocall As String
Dim num As Variant

num = Application.InputBox("Please input either the number 1 or 2")

If IsNumeric(num) Then
    Select Case CLng(num)
        Case 1
            Call numname
        Case 2
            Call firstname
    End Select
Else
    MsgBox "Oops - you didn't enter a number 1 or 2"
End If

End Sub

Private Sub numname()

    MsgBox "the number you typed is 1"

End Sub

Private Sub firstname()

    MsgBox "The number you typed is 2"

End Sub
Worry about classes later ... if you know what they are, you'll know what to do with a class module. Otherwise, you'll use a standard module.

I can't believe I overlooked the sub part. :( Thanks =) Everything works fine now =)
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top