0

is there a way to call Objects or Methods by variables like following? -> Could be handy to loop through certain properties of Userforms and so on.

Sub CallObjByVar()
Dim x1, x2 as string
Dim y as variant

x1 = "Interior"
x2 = "Color"

ReDim y(0,1)
y(0,0) = x1
y(0,1) = x2

ThisWorkbook.ActiveWorksheet.Range("A1").x1.x2 = 65535
' OR
ThisWorkbook.ActiveWorksheet.Range("A1").y(0,0).y(0,1) = 65535

End Sub
iijoexx
  • 11
  • 1
  • What you want can be achieved using Classes and not just using "Strings". But then it will defeat the purpose because the method will be added to the intellisense. And you already have the above in Intellisense. I see no use re-inventing the wheel? Maybe you have something different in mind? – Siddharth Rout Mar 19 '21 at 08:26
  • For Example: Loop through every checkbox (in userform) and change the Values (TRUE/FALSE). When i have a list of the checkbox names, i want to call them in a loop like so: for i = 0 to 3 --- Me.Checkboxname(i).Value --- next i – iijoexx Mar 19 '21 at 08:33
  • `Loop through every checkbox (in userform)` Yes for that it makes sense not for intellisense methods. You can use `Me.Controls("CheckBox" & i)` or `TypeName(Ctl)` for looping through checkboxes. There are lot of example already there in stackoverflow for this – Siddharth Rout Mar 19 '21 at 08:37
  • Maybe `CallByName` - eg https://stackoverflow.com/questions/48275351/how-to-access-a-worksheet-property-using-a-variable/48287653#48287653 or https://stackoverflow.com/questions/47805916/save-all-information-from-a-range-and-restore-it-later/47808603#47808603 but as other commenters have pointed out, there are other ways which are likely "better". – Tim Williams Mar 19 '21 at 17:43

0 Answers0