Change buttoncolor on button on a sheet from VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Button1.BackColor = &H8000& works ok, but if I make a loop like this

for I = I to 10
if <argument> true then
("Button" & cstr(I)).BackColor = &H8000
end if
next

I get this error: Expected : Linenumber or Label or statement or end of statement
how should I call my Button
I tried with:
sheets("sheet1"). ("Button" & cstr(I)).BackColor = &H8000
sheet1. ("Button" & cstr(I)).BackColor = &H8000
activesheet. ("Button" & cstr(I)).BackColor = &H8000
.... and a lot of others
 
When you reference "button1" as in your first example which works, you are referencing an object. In the non-working other examples you are referencing text, which you can't set to a value.

Try (if the buttons are on a form)
<formname>.Controls("Button" & cstr(I))

or if they are on a sheet:
<sheetname>.Controls("Button" & cstr(I))
 
I always call my Controls in Forms with me.Controls or <Formname>.Controls, but this simply doesn't work on Sheets

"Ian Digby" skrev:
 
sheet1.OLEObjects("Button" & cstr(I)).Object.BackColor = &HFF& works

"akyhne" skrev:
 

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

Back
Top