Loop through i to get variable values

purvis

New Member
Joined
Jul 9, 2018
Messages
1
I am trying to create a macro to loop through a set of 10 variables that may or may not be assigned values, if they are assigned a value then add the variable to an array. The variables all begin with hf and numbered 1 - 10. I am having difficulty figuring out how to retrieve the variable value while iterating through hf & 1-10 and then how I would add the variable to an array. In example below hf1, hf2 & hf5 would add values x, y & z to the array.

Any assistance is appreciated. Thank you.

Sub loop_through_i()
hf1 = "x"
hf2 = "y"
hf3 = ""
hf4 = ""
hf5 = "z"
hf6 = ""
hf7 = ""
hf8 = ""
hf9 = ""
hf10 = ""

For i = 1 To 10
If hf & i <> empty then
Set hf_array = Array("hf" & i)
End if
Next i
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi & welcome to MrExcel.
You cannot loop through variables like that, why not just put the values into an array to begin with
VBA Code:
Sub purvis()
   Dim Ary(1 To 10) As Variant
   
   Ary(1) = "X"
   Ary(2) = "y"
   Ary(5) = "Z"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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