Excel VBA Macro to change cell color

MaggieSmaggie

New Member
Joined
Jul 5, 2017
Messages
2
I want to create a Macro and assign it to a button. This is what I want to happen:


I select a cell that I want to be changed by the macro to have a certain color.
When I click the macro button, the cell I had selected changes colors.


I know you can do this by changing the background color of a cell. However, I would like to use this Macro to be able to quickly change a cell to different fills or patterns by just clicking the button.


So far, when I write the code it can only work for an assigned range, like F9. How to I make it work for the active cell instead of always this cell?

Sub OrganicCover()
'
' OrganicCover Macro
'
' Keyboard Shortcut: Ctrl+q
'
Range("F9").Select
With Selection.Interior
.Pattern = xlPatternLinearGradient
.Gradient.Degree = 90
.Gradient.ColorStops.Clear
End With
With Selection.Interior.Gradient.ColorStops.Add(0)
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.400006103701895
End With
With Selection.Interior.Gradient.ColorStops.Add(1)
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.400006103701895
End With
Range("F11").Select
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Welcome to the Board!

Just remove the literal cell selection from your code, and replace all references of Selection to ActiveCell:
Code:
Sub OrganicCover()
'
' OrganicCover Macro
'
' Keyboard Shortcut: Ctrl+q
'
    With ActiveCell.Interior
        .Pattern = xlPatternLinearGradient
        .Gradient.Degree = 90
        .Gradient.ColorStops.Clear
    End With
    
    With ActiveCell.Interior.Gradient.ColorStops.Add(0)
        .ThemeColor = xlThemeColorAccent2
        .TintAndShade = 0.400006103701895
    End With
    
    With ActiveCell.Interior.Gradient.ColorStops.Add(1)
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0.400006103701895
    End With


End Sub
Also note, using Code Tags makes your code much more readable (https://www.mrexcel.com/forum/board...post-your-visual-basic-applications-code.html)
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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