UserForm Control references as variable

D

donwb

Excel 2003
On my UserForm I have several CommandButtons.
In my code, I want to refer to a selected or active one as a variable,
rather than by it's name, say CommandButton4.
So I store it's name:-
Worksheets("MyVariables").Range("A4")=UserForm1.ActiveControl.Name (which
works ok)
and instead of UserForm1.CommandButton4.BackColor = .................
I would like to do:-
UserForm1.Worksheets("MyVariables").Range("A4").BackColor =
..................
which doesn't work, as the synyax or method is wrong.
Can it be done and if so how.
 
P

Peter T

Put a CommandButton on a Userform, paste the following into the form code.
Ensure the activeworkbook has a sheet named "Sheet1" (the sheet doesn't need
to be active). Press F5 to run the form.

Private Sub UserForm_Activate()
Dim sCtrName As String
Dim nClr As Long
With ActiveWorkbook.Worksheets("Sheet1")

.Range("A4").Value = "CommandButton1"
.Range("B4").Value = RGB(123, 234, 56)

'''maybe a system colour
'.Range("B4").Value = vbInactiveTitleBar

sCtrName = .Range("A4").Value
nClr = .Range("B4").Value
End With

Me.Controls(sCtrName).BackColor = nClr

End Sub

Regards,
Peter T
 
D

donwb

Many thanks Peter T.
Tat worked.
donwb

Peter T said:
Put a CommandButton on a Userform, paste the following into the form code.
Ensure the activeworkbook has a sheet named "Sheet1" (the sheet doesn't
need to be active). Press F5 to run the form.

Private Sub UserForm_Activate()
Dim sCtrName As String
Dim nClr As Long
With ActiveWorkbook.Worksheets("Sheet1")

.Range("A4").Value = "CommandButton1"
.Range("B4").Value = RGB(123, 234, 56)

'''maybe a system colour
'.Range("B4").Value = vbInactiveTitleBar

sCtrName = .Range("A4").Value
nClr = .Range("B4").Value
End With

Me.Controls(sCtrName).BackColor = nClr

End Sub

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top