ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Change buttoncolor on button on a sheet from VBA (https://www.excelbanter.com/excel-programming/301971-change-buttoncolor-button-sheet-vba.html)

akyhne[_2_]

Change buttoncolor on button on a sheet from VBA
 
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

Ian Digby[_3_]

Change buttoncolor on button on a sheet from VBA
 
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))

"akyhne" wrote:

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


akyhne[_2_]

Change buttoncolor on button on a sheet from VBA
 
I always call my Controls in Forms with me.Controls or <Formname.Controls, but this simply doesn't work on Sheets

"Ian Digby" skrev:

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))

"akyhne" wrote:

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


akyhne[_2_]

Change buttoncolor on button on a sheet from VBA
 
sheet1.OLEObjects("Button" & cstr(I)).Object.BackColor = &HFF& works

"akyhne" skrev:

I always call my Controls in Forms with me.Controls or <Formname.Controls, but this simply doesn't work on Sheets

"Ian Digby" skrev:

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))

"akyhne" wrote:

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



All times are GMT +1. The time now is 03:27 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com