Simplifying Variable Declarations with Other Variables

Kelinky_Sama

New Member
Joined
Aug 27, 2016
Messages
6
I'm rather new to Excel macros, so please be patient if I'm asking for something that should be rather simple.

What I'm attempting to do is recreate an RPG "inventory" of sorts with a UserForm in Excel VBA. So far, the way I've managed this has been by placing a ToggleButton underneath a disabled CheckBox for each slot in the inventory I want to use, with each being marked by a number corresponding with one another, if that makes sense; ToggleButton1 would be aligned with CheckBox1, ToggleButton2 with CheckBox2, and so forth. If an item is located in that slot, the value for that CheckBox would be changed to "True". To observe that item, the user would click on the ToggleButton, followed by clicking on a separate "Observe" CommandButton at the bottom of the UserForm, the code for which is shown below:

HTML:
Private Sub CommandButton3_Click()
Dim tag1 As StringDim tag2 As String

tag1 = BookInvent1.CheckBox1.tagtag2 = BookInvent1.CheckBox2.tag
If ToggleButton1.Value = True And CheckBox1.Value = True Then    
   Run tag1End IfToggleButton1.Value = False
If ToggleButton2.Value = True And CheckBox2.Value = True Then
    Run tag2
End If
ToggleButton2.Value = False

End Sub
In this case, the tags attached to the CheckBoxes are the names of the items located in the inventory. The names of the items are associated with macros, and the macro that runs depends on the name of the item. (i.e. If there was an apple in the first slot of the inventory, the value of CheckBox1 would be True, and the tag would be "Apple". Clicking on ToggleButton1 and then on the "Observe" button would run an "Apple" macro that would relay a series of MsgBox codes describing the apple.)

This, on its own, has worked for me. However, the inventory in question is intended to hold 40 items, not simply 2, and the resulting code in the above format would be rather extensive. I understand that I could shorten it somewhat through use of an array (for instance, using a For...Next loop in conjunction with an array to declare variables from each of the tags in question), but what about the other elements in the code above?

Imagine a For...Next loop like the one below:

HTML:
Private Sub CommandButton3_Click()
Dim i As IntegerDim tag(1 To 40) As String
For i = 1 To 40
    tag(i) = "BookInvent1" & ".CheckBox" & i & ".tag"
    If ("ToggleButton" & i & ".Value" = True And "CheckBox" & i & ".Value" = True) Then
        Run tag(i)
    End If
    "ToggleButton" & i & ".Value" = False
Next

End Sub

However, the above code proposes a series of problems. Each tag() in the array is not equal to the actual tags I want them to act as, but instead are treated as a separate string. Furthermore, after the End If concerning the ToggleButton, I'm given an error that reads: "Compile error: Expected: line number or label or statement or end of statement".

I apologize for being so lengthy with this request, but I would like to understand where it is that I'm going wrong.

I am currently running on Windows 8.1 with Microsoft Excel 2016 32-bit.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
i think this is what you're looking for

Code:
i = 1
MsgBox Me.Controls("Textbox" & i).Value
 
Upvote 0

Forum statistics

Threads
1,215,947
Messages
6,127,867
Members
449,410
Latest member
adunn_23

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