Adding strings to object names through a loop with if statements based on these strings

ParetoZen

New Member
Joined
Jun 18, 2013
Messages
4
When working with the software Stata I was able to perform the following code:

local var="1992 1991 1990 1989 1988 1987 1986 1985 1984"
foreach x of local var{
egen HHI_Y1_`x' = sum(MS_Y1^2) if Time == `x'
replace HHI_Y1 = HHI_Y1_`x' if Time == `x'
}

This worked great for generating variables (egen), alternating them and even use the local variables for conditional statements.

Now I'd like to do the same in VBA.
I got a codeblock that I want to execute with multiple ComboBoxes.
is there a way to say:

For x = 1 to 10
If Sheets("Product List").Cells(i, 1) = ComboBox`x'.Value And x > 5 Then
.Cells(ProductWorkingRow, 1) = ComboBox`x'.Value
Dim Variable_`x' as Double
'rest of codeblock
End If
Next x

Hope I made it clear what my goal is. Right now I have a dozen of these code blocks after eachother which doesnt look very efficient or professional + time consuming to replace all the x's with right number.

Thanks for the help, looking forward to participate on MrExcel.com forum.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
For your variable Variable_x you need to declare an array instead of the form:

Code:
Dim myVariable(1 to 10) As Double

You can refer to the comboboxes by their defined names eg:

Code:
For x = 1 to 10
  If Sheets("Product List").Cells(i, 1) = Me.Controls("ComboBox" & x).Value And x > 5 Then
   .Cells(ProductWorkingRow, 1) = Me.Controls("ComboBox" & x).Value 
    'etc etc...
 
Upvote 0
That works like a charm. It greatly enhances the programming speed and neatness. You opened a world for me in Excel VBA. Thanks a lot.
 
Upvote 0

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

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