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!
Stringvariable that is updated and can be dispalyed in bothSubs ? you can use thePublic strWeapon as String, put this code line (for conviniance) above bothSubs, under theOption ExplicitCommandButton1? on which User_Form ? what exactly is your code suppose to do?