|
-
Apr 25th, 2012, 12:45 PM
#1
Thread Starter
New Member
Variable as button name
Hello,
I am using macros in excel, and I want to use a variable to define the name of a button to change its backcolor. What Ive done is as follows:
lote = Label2.Caption
If OptionButton1.Value = True Then lote.BackColor = vbYellow
However it does not recognize lote as a button name to change the backcolor. Can anyone help me please?
Thanks.
-
Apr 25th, 2012, 03:16 PM
#2
Addicted Member
Re: Variable as button name
Hi,
You can access ActiveX controls on sheet using OLEObjects collection:
Code:
Dim strButtonName As String
strButtonName = "cmdButton2"
Sheets(1).OLEObjects(strButtonName).Object.BackColor = vbRed
If you command button is placed on form you can use Controls collection:
Code:
Dim strButtonName As String
strButtonName = "cmdButton1"
Me.Controls(strButtonName).BackColor = vbBlue
Hope this helps.
Regards,
Sebastian
using VB 2010 .NET Framework 4.0; MS Office 2010; SQL Server 2008 R2 Express Edition | Remember to mark resolved threads and rate useful posts. 
-
Apr 25th, 2012, 04:04 PM
#3
Thread Starter
New Member
Re: Variable as button name
Thanks you very much Sebastian.
It works well.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|