9

I'm pretty new to VBA and I don't know how to use a variable between subs. The code should be pretty self explanatory:

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim strWeapon As String
If Target.Address = "$I$8" Then
    MsgBox "You pick up a sword"
    strWeapon = "Sword"
ElseIf Target.Address = "$I$9" Then
    MsgBox "You pick up a magic staff"
    strWeapon = "Magic"
ElseIf Target.Address = "$I$10" Then
    MsgBox "You pick up a bow and arrows"
    strWeapon = "Bow"
End If
End Sub

Public Sub CommandButton1_Click()
If strWeapon = "Magic" Then
    UserForm1.Show
ElseIf strWeapon = "Sword" Then
    UserForm2.Show
ElseIf strWeapon = "Bow" Then
    UseForm2.Show
End If
End Sub

Is there a way I can use strWeapon in both subs? I get an error for not defining the variable strWeapon in the second sub. Thank you!

5
  • 3
    do you mean as a String variable that is updated and can be dispalyed in both Subs ? you can use the Public strWeapon as String , put this code line (for conviniance) above both Subs , under the Option Explicit Commented Dec 1, 2016 at 6:46
  • declare strWeapon outside the sub Worksheet_SelectionChange globally Commented Dec 1, 2016 at 6:47
  • I added it like you guys said to do and removed Dim strweapon as string at the top and now nothing happens when I click the commandButton Commented Dec 1, 2016 at 6:53
  • 1
    where is your CommandButton1 ? on which User_Form ? what exactly is your code suppose to do? Commented Dec 1, 2016 at 7:05
  • On second thoughts, your question is actually, "How do I keep a variable assigned after the sub routine has finished", is it not? Commented Dec 1, 2016 at 9:53

2 Answers 2

17

You have a couple of options.

  1. You can use a public variable as shai rado has mentioned- this is probably easiest, however you should limit the use of public variables where possible.

  2. You can pass the variable from one sub to another like this:

    sub Main()
        dim strWeapon as string
        strWeapon = "Fork"
        WillIWin strWeapon
    end sub
    
    sub WillIWin(strWeapon as string)
        select case strWeapon
            case "Gun"
                msgbox "Probably"
            case "Sword"
                msgbox "unlikely"
            case "Fork"
                Msgbox "Goodluck"
        end select
    End Sub
    
  3. Alternatively if you have multiple variables to pass, you might be better with a class, this option takes more work than I'm willing to put in here, but CPearson has a good introductory course for them.

Sign up to request clarification or add additional context in comments.

Comments

2

yes!

Public strWeapon as String

At the top of any module, do the following:

tpye Option Explicit then immediately below, put the Public declaration. this can then be used in any other subroutine OR module within your excel project

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.