lightmaster
New Member
- Joined
- Feb 7, 2018
- Messages
- 13
I've got a UserForm with hundreds of small Command Buttons on them that are all named as as either Predicted followed by some numbers or Actual followed by some numbers. I've also got an equal number of cells in the spreadsheet that are named the same as all those Command Buttons. What I'm trying to do is, when the UserForm is initialized, loop through all the Command Buttons and set their caption to the value of the corresponding spreadsheet cell. Using this code, I can both print the names of all the Command Buttons, and set all the captions to the value of that Named Range, but if I replace "Predicted418" with ctrl.Name, I get a "Run-time error '1004'" error. I figure the issue is that ctrl.Name must not be a string, and Range() requires a string, but I can't figure out how to get ctrl.Name to be a string.
Code:
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CommandButton" Then
Debug.Print ctrl.Name
ctrl.Caption = Sheets("Execution Test Entry").Range("Predicted418").Value
End If
Next ctrl