ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Changing backcolor of commandbutton (https://www.excelbanter.com/excel-programming/295997-changing-backcolor-commandbutton.html)

scottnshelly[_21_]

Changing backcolor of commandbutton
 
Here's another easy one for ya. I stole this code from a guy at work.
I want to change it to change the backcolor of a commandbutton rathe
than a range. i tried, but i'm an idiot. here is his code:

Dim Color As Single
Dim Bob As Single
Dim spot As Range
Dim counter As Single
For Bob = 1 To 25 Step 1
Randomize
Color = Rnd()
Set spot = Sheet1.Range("B5:L25")
Call SetCellColor(spot, Color)
Application.Wait Now + TimeSerial(0, 0, 0.07)
Next Bob
Range("a1").Select
UserForm2.Show

then he has a module with :
Function SetCellColor(spot, Color)
Dim ColorN As Integer
If Color = "Lt Green" Then
ColorN = 15
ElseIf Color < 0.0399 Then
ColorN = 48
ElseIf (Color 0.04) And (Color < 0.0799) Then
ColorN = 34
ElseIf (Color 0.08) And (Color < 0.1199) Then
ColorN = 35
ElseIf (Color 0.12) And (Color < 0.1599) Then
ColorN = 39
ElseIf (Color 0.16) And (Color < 0.1999) Then
ColorN = 36
ElseIf (Color 0.2) And (Color < 0.2399) Then
ColorN = 33
ElseIf (Color 0.24) And (Color < 0.2799) Then
ColorN = 8
ElseIf (Color 0.28) And (Color < 0.3199) Then
ColorN = 4
ElseIf (Color 0.32) And (Color < 0.3599) Then
ColorN = 6
ElseIf (Color 0.36) And (Color < 0.3999) Then
ColorN = 44
ElseIf (Color 0.4) And (Color < 0.4499) Then
ColorN = 7
ElseIf (Color 0.45) And (Color < 0.5099) Then
ColorN = 54
ElseIf (Color 0.51) And (Color < 0.5599) Then
ColorN = 41
ElseIf (Color 0.56) And (Color < 0.6099) Then
ColorN = 42
ElseIf (Color 0.61) And (Color < 0.6499) Then
ColorN = 50
ElseIf (Color 0.65) And (Color < 0.6999) Then
ColorN = 3
ElseIf (Color 0.7) And (Color < 0.7399) Then
ColorN = 5
ElseIf (Color 0.74) And (Color < 0.7899) Then
ColorN = 10
ElseIf (Color 0.79) And (Color < 0.8299) Then
ColorN = 2
ElseIf (Color 0.83) And (Color < 0.8799) Then
ColorN = 38
ElseIf (Color 0.88) And (Color < 0.9299) Then
ColorN = 13
ElseIf (Color 0.93) And (Color < 0.9599) Then
ColorN = 45
ElseIf (Color 0.96) And (Color < 0.1) Then
ColorN = 1
Else
ColorN = xlNone
End If
Sheet1.Range("a1:q46").Interior.ColorIndex = ColorN
'Sheet1.Range("B4:l25").Pattern = xlSolid
End Function

i tried changing the set spot = sheet1.range("") to
set spot = sheet1.commandbutton1
but that didn't work

--
Message posted from http://www.ExcelForum.com


Bob Phillips[_6_]

Changing backcolor of commandbutton
 
You can only do it if it is a control toolbox button, and then you just set
the BackColor property.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"scottnshelly " wrote in
message ...
Here's another easy one for ya. I stole this code from a guy at work.
I want to change it to change the backcolor of a commandbutton rather
than a range. i tried, but i'm an idiot. here is his code:

Dim Color As Single
Dim Bob As Single
Dim spot As Range
Dim counter As Single
For Bob = 1 To 25 Step 1
Randomize
Color = Rnd()
Set spot = Sheet1.Range("B5:L25")
Call SetCellColor(spot, Color)
Application.Wait Now + TimeSerial(0, 0, 0.07)
Next Bob
Range("a1").Select
UserForm2.Show

then he has a module with :
Function SetCellColor(spot, Color)
Dim ColorN As Integer
If Color = "Lt Green" Then
ColorN = 15
ElseIf Color < 0.0399 Then
ColorN = 48
ElseIf (Color 0.04) And (Color < 0.0799) Then
ColorN = 34
ElseIf (Color 0.08) And (Color < 0.1199) Then
ColorN = 35
ElseIf (Color 0.12) And (Color < 0.1599) Then
ColorN = 39
ElseIf (Color 0.16) And (Color < 0.1999) Then
ColorN = 36
ElseIf (Color 0.2) And (Color < 0.2399) Then
ColorN = 33
ElseIf (Color 0.24) And (Color < 0.2799) Then
ColorN = 8
ElseIf (Color 0.28) And (Color < 0.3199) Then
ColorN = 4
ElseIf (Color 0.32) And (Color < 0.3599) Then
ColorN = 6
ElseIf (Color 0.36) And (Color < 0.3999) Then
ColorN = 44
ElseIf (Color 0.4) And (Color < 0.4499) Then
ColorN = 7
ElseIf (Color 0.45) And (Color < 0.5099) Then
ColorN = 54
ElseIf (Color 0.51) And (Color < 0.5599) Then
ColorN = 41
ElseIf (Color 0.56) And (Color < 0.6099) Then
ColorN = 42
ElseIf (Color 0.61) And (Color < 0.6499) Then
ColorN = 50
ElseIf (Color 0.65) And (Color < 0.6999) Then
ColorN = 3
ElseIf (Color 0.7) And (Color < 0.7399) Then
ColorN = 5
ElseIf (Color 0.74) And (Color < 0.7899) Then
ColorN = 10
ElseIf (Color 0.79) And (Color < 0.8299) Then
ColorN = 2
ElseIf (Color 0.83) And (Color < 0.8799) Then
ColorN = 38
ElseIf (Color 0.88) And (Color < 0.9299) Then
ColorN = 13
ElseIf (Color 0.93) And (Color < 0.9599) Then
ColorN = 45
ElseIf (Color 0.96) And (Color < 0.1) Then
ColorN = 1
Else
ColorN = xlNone
End If
Sheet1.Range("a1:q46").Interior.ColorIndex = ColorN
'Sheet1.Range("B4:l25").Pattern = xlSolid
End Function

i tried changing the set spot = sheet1.range("") to
set spot = sheet1.commandbutton1
but that didn't work.


---
Message posted from http://www.ExcelForum.com/




scottnshelly[_24_]

Changing backcolor of commandbutton
 
i know how to manually change the backcolor. is there a way to chang
it multiple times like the code posted previously? in that code when
click the button the fill color of a range changes colors several time
really fast. i want the backcolor of a commandbutton to change color
a bunch of times really fast

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 06:44 AM.

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