Can you combine variables?

DHS100

Board Regular
Joined
May 21, 2006
Messages
147
Hi,

I have a set of similar variables like this:
var_HasFinished1 = True
var_HasFinished2 = True
var_HasFinished3 = True
var_HasFinished4 = True
var_HasFinished5 = True

It would be very handy if instead of, for instance, saying something like:
if var_HasFinished1 = True then msgbox "Do Something to 1st process"

I could say:
if var_HasFinished & i = True then msgbox "Do Something to 1st process"
(the i would be a variable containing the value 1)

I've tried everything. I would be super grateful to anyone who knows how to do this or can think of a workaround!

Thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try using an array:

Code:
Sub test()
Dim var_HasFinished(1 To 6) As Boolean, i As Byte
For i = 1 To 6
    var_HasFinished(i) = True
    MsgBox var_HasFinished(i)
Next i
End Sub
 

xenou

MrExcel MVP
Joined
Mar 2, 2007
Messages
16,836
Office Version
  1. 2019
Platform
  1. Windows
Some intro material on using array variables:

http://www.anthony-vba.kefra.com/vba/excelvba-simulation.htm#Creating_and_Managing_Array

You'll might want to note that if you declare an array such as
Code:
Dim hasFinished(5)
This is telling Excel to create an array such that its highest index number is 5. You might be thinking "Great! - 5 elements." However, the index is by default zero-based, so the elements of the array are hasFinished(0), hasFinished(1), hasFinished(2), hasFinished(3), and hasFinished(4), and hasFinished(5). As you see, this is six elements, not five.
You can either work with the zero base (programmers get used to this), or you can make Excel start at 1 by defined the base:
Code:
Dim hasFinished(1 to 5)
Now there would be no index of zero, and the first array Element is hasFinished(1).

HTH
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
... or use this at the top of a module

Code:
Option Base 1
 

Forum statistics

Threads
1,181,373
Messages
5,929,571
Members
436,679
Latest member
helly123

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
Top