Change font colors with one shortcut.

db317

New Member
Joined
Jul 26, 2016
Messages
2
Hello all,

I was trying to create a shortcut that could change the font color of the selected cell.
Here is what I want to achieve:
1. There needs to be only one shortcut, for example "Ctrl+Alt+H"
2. When I use the shortcut, it changes the font color in selected cells to black.
3. When I use the shortcut on selected cells again, it changes the font color to blue.
4. Use the shortcut again and it changes the selected font color to red.
5. Then green.

I was trying to do it with a "If, Elseif" function but it didn't work out.

Please help me with this. I appreciate all your help
 

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 Forum!

Perhaps something like this:

Code:
'In the ThisWorkbook module
Private Sub Workbook_Open()
    
    Application.OnKey "+^{H}", "ChangeFontColor"

End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Application.OnKey "+^{H}"

End Sub
'In a standard code module
Private Sub ChangeFontColor()
            
    Dim c As Range
    
    For Each c In Selection
        With c.Font
            Select Case .Color
            Case vbBlack
                .Color = vbBlue
            Case vbBlue
                .Color = vbRed
            Case vbRed
                .Color = vbGreen
            Case Else
                .Color = vbBlack
            End Select
        End With
    Next c

End Sub
 
Upvote 0
Hi StephenCrump

Nice code. I needed to change the ChangeFontColor() so it was not Private, ie:

Code:
Sub ChangeFontColor()

Also, the shortcut key combination that works is 'Ctrl+Shift+H' not 'Ctrl+Alt+H' - is there a particular script for 'Ctrl-Alt-H'?

Cheers

pvr928
 
Upvote 0
I needed to change the ChangeFontColor() so it was not Private

OK, but I'm not clear why it wouldn't have worked as a Private Sub.

Also, the shortcut key combination that works is 'Ctrl+Shift+H' not 'Ctrl+Alt+H' - is there a particular script for 'Ctrl-Alt-H'?

Oops, my mistake (I use custom CTRL-SHIFT shortcuts myself).

The Developer Reference Help for Application.OnKey method has a full list. ALT is %
 
Upvote 0
Thanks StephenCrump

I just changed it back to Private Sub ChangeFontColor() and it works - not sure why it wasn't previously :confused:

Cheers

pvr928
 
Upvote 0

Forum statistics

Threads
1,225,876
Messages
6,187,537
Members
453,429
Latest member
JeanDuarnet

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