ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Cell working as a button? Cell color control (https://www.excelbanter.com/excel-worksheet-functions/156482-cell-working-button-cell-color-control.html)

Emerogork via OfficeKB.com

Cell working as a button? Cell color control
 
Can I get a cell to work as if it were a button? (Or can I place a button on
a worksheet?)

I have been playing with ActiveCell but have not quite figured how to trigger
it. In VB I could create a Sub Toggle () and run its contents from a button.
Can I program a cell to run a Sub?

Also,: Why doesn't my mouse thumb wheel work when I view code?

And Then: I can change the cell color but how do I change the shade or
crosshatch value using code?

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...tions/200708/1


Bernie Deitrick

Cell working as a button? Cell color control
 
Can I get a cell to work as if it were a button?

Yes. You need to use the worksheet's SelectionChange event, and check the cell against the target.
For example, this code will run the macro CellA1 whenever cell A1 is selected.

'In the sheet's codemodule
'Right-Click the sheet tab and select "View Code",
'then paste in the window that appears
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$A$1" Then
CellA1
Else
NotCellA1
End If
End Sub

'In a regular codemodule
Sub CellA1()
'Turn off events if you do anything to change a cell
'other than its formatting
MsgBox "Howdy from Cell A1"
With Range("A1").Interior
.ColorIndex = 3
.Pattern = xlLightDown
End With
End Sub

Sub NotCellA1()
'Turn off events if you do anything to change a cell
'other than its formatting
MsgBox "Howdy from NOT Cell A1"
With Range("A1").Interior
.ColorIndex = xlNone
.Pattern = xlNone
End With
End Sub



(Or can I place a button on a worksheet?)


Yes you can, and you can assign a macro to it.

And Then: I can change the cell color but how do I change the shade or
crosshatch value using code?


See the code above. Record a macro to get the desired values...

HTH,
Bernie
MS Excel MVP


"Emerogork via OfficeKB.com" <u36894@uwe wrote in message news:77792f0c970c6@uwe...
Can I get a cell to work as if it were a button? (Or can I place a button on
a worksheet?)

I have been playing with ActiveCell but have not quite figured how to trigger
it. In VB I could create a Sub Toggle () and run its contents from a button.
Can I program a cell to run a Sub?

Also,: Why doesn't my mouse thumb wheel work when I view code?

And Then: I can change the cell color but how do I change the shade or
crosshatch value using code?

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...tions/200708/1





All times are GMT +1. The time now is 12:32 PM.

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