VBA code colouring cells as i click on them

kanelones

Board Regular
Joined
Aug 25, 2010
Messages
103
Hi guys, need a code to replicate what excel has on its own ribbon. i mean, need to asign a button in order to paint any cell i choose to , for example, yellow.

Tried this: ( not the result i looking for of course)

CAn you help me?

Tks!

Sub Macro3()
'
' Macro3 Macro
'


'




ActiveCell.Range(" my choose- one or many cells as i want ").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Do you want it to stay colored or only colored while it is active ???
 
Upvote 0
Upvote 0
Try this:
The active cell interior color will be yellow
But now this script will remove any cells interior color already set.
So all cells on the active sheet will have no interior color except for the active cell which will be Yellow.

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Modified  5/3/2019  10:20:39 PM  EDT
Cells.Interior.ColorIndex = xlNone
Target.Interior.Color = vbYellow
End Sub
 
Upvote 0
Here is another option:
Any cell with a value will be colored Green
The active cell will be colored Yellow.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Modified  5/3/2019  10:28:39 PM  EDT
Dim r As Range
Cells.Interior.ColorIndex = xlNone
For Each r In ActiveSheet.UsedRange
    If r.Value <> "" Then r.Interior.Color = vbGreen
Next
Target.Interior.Color = vbYellow
End Sub
 
Upvote 0
Nice, but not what i'm looking for...

Sorry i did not make myself clear, i just need to select some cells, and click on a button in order to paint them. Emulating the same function that we have on our excel ribbon, but using a button somewhere else on my sheet...
Just wondering if i can do that, it would help me on my daily job, reconciliating . Maybe is any way to do that..

The title i wrote on my thread was not fully understandable.


Thanks guys
 
Last edited:
Upvote 0
OK, i think i found it based on your code



Sub color()


Dim r As range
Cells.Interior.ColorIndex = xlNone
For Each r In ActiveSheet.UsedRange
If r.Value <> "" Then r.Interior.color = vbGreen
Next




End Sub

Thank you!
 
Upvote 0
This is the final one!

Just asigned these codes to my buttons and that's it!

Thank you guys, you help me think



'paint selected cells

Private Sub CommandButton1_Click()
Selection.Interior.ColorIndex = 50
Selection.Locked = True
Selection.FormulaHidden = False
End Sub






' erase color
Private Sub CommandButton2_Click()


Selection.Interior.ColorIndex = 0
Selection.Locked = True
Selection.FormulaHidden = False






End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top