How do I change the font color in a range of cells to the same font color in another range of cells using a vba.

Patches01

New Member
Joined
Aug 11, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
I want to change the font color in a range of cells to the same font color in another range of cells using a vba.

I have the below code however it only seems to work with one cell to another cell not a range of cells.


Me.Range("g4").Font.ColorIndex = Me.Range("f4").Font.ColorIndex

Thank you
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
VBA Code:
Me.Range("g4:n20").Font.ColorIndex = Me.Range("f4").Font.ColorIndex
this is if all the cells in the range share the same font color. And of course change the range to whatever you need.
 
Upvote 0
VBA Code:
Me.Range("g4:n20").Font.ColorIndex = Me.Range("f4").Font.ColorIndex
this is if all the cells in the range share the same font color. And of course change the range to whatever you need.
Some cells in the range are blank and the font color changes depending on the entered text for example less than a zero changes to red font and more than zero changes to black font. The above code did not work.
 
Upvote 0
In that case you do not need to change the Font color, but rather the NumberFormat:
VBA Code:
Me.Range("g4:n20").NumberFormat = Me.Range("f4").NumberFormat
 
Upvote 0
The code works, it just does not do what you need, because it has to be done in a different way. The color index of the basic cell is obviously not changed manually. Provide some sample data and explain in detail what exactly you need. Also explain how the number colors are changed - Conditional formatting or Custom number format.
 
Upvote 0
In that case you do not need to change the Font color, but rather the NumberFormat:
VBA Code:
Me.Range("g4:n20").NumberFormat = Me.Range("f4").NumberFormat
This may not work if the values in the target range are not numbers.
Conditional formatting may be a better option than vba.
 
Upvote 0
This may not work if the values in the target range are not numbers.
Conditional formatting may be a better option than vba.
I assumed the numberformat *is* using conditions to color the font and that numberformat has already been applied to cell F4.
 
Upvote 0
If F4 contains numbers and the number format works on it, it will not work on g4 if it contains text.
 
Upvote 0
The code works, it just does not do what you need, because it has to be done in a different way. The color index of the basic cell is obviously not changed manually. Provide some sample data and explain in detail what exactly you need. Also explain how the number colors are changed - Conditional formatting or Custom number format.
I apologise. I a new to VBN coding. To be more clear.
I have a sheet that is used for balancing deposits paid and refunded the following information is used.
Date, Name, Money paid or refunded and the initials of the staff member entering the information.
For Example
A4 = DATE, B4 = NAME, C4, D4.E4 AND F4 = THE DOLLOR AMOUNT PAID OR REFUNDED, G4 = THE INITIALS OF THE STAFF MEMBER
When the dollar amount is entered I have a code for the amount to change font color to RED if it is a refund to a customer and to stay BLACK if it is a payment to the business.
I require A4, B4 and G4 to change RED when a REFUND occurs.
Here is the code as I have it so far.
Private Sub CommandButton2_Click()

Dim myRange As Range
Set myRange = Range("c4:f42")

For Each Cell In myRange
If Cell.Value < 0 Then Cell.Font.ColorIndex = 3


Set myRange = Range("c4:f42")


If Cell.Value > 0 Then Cell.Font.ColorIndex = 1


Next

Thank you for your help
 
Upvote 0
In that case you do not need to change the Font color, but rather the NumberFormat:
VBA Code:
Me.Range("g4:n20").NumberFormat = Me.Range("f4").NumberFormat
I have a sheet that is used for balancing deposits paid and refunded the following information is used.
Date, Name, Money paid or refunded and the initials of the staff member entering the information.
For Example
A4 = DATE, B4 = NAME, C4, D4.E4 AND F4 = THE DOLLOR AMOUNT PAID OR REFUNDED, G4 = THE INITIALS OF THE STAFF MEMBER
When the dollar amount is entered I have a code for the amount to change font color to RED if it is a refund to a customer and to stay BLACK if it is a payment to the business.
I require A4, B4 and G4 to change RED when a REFUND occurs.
Here is the code as I have it so far.
Private Sub CommandButton2_Click()

Dim myRange As Range
Set myRange = Range("c4:f42")

For Each Cell In myRange
If Cell.Value < 0 Then Cell.Font.ColorIndex = 3


Set myRange = Range("c4:f42")
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,038
Latest member
apwr

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