简体   繁体   中英

How do you change the formated font color of a cell using VBA?

I would like to select rows in my excel sheet based on a criteria and then edit the format of other cells in the same row.

I know that I can select rows using autofilters (column n equals ""):

Sub beautify()

Dim rng As Range
Set rng = ActiveSheet.Range("F60:AJ3272")
rng.AutoFilter Field:=4, Field:=4, Criteria1:=""

End Sub 

Now how do I change the font of column F of the lines that I have selected to white.

You can use VBA to change the background color of a cell using .Interior.ColorIndex = RGB(r, g, b) : red and the font color of the text inside a cell with .Font.Color = RGB(r, g, b) : red The range to change these property on should be defined, like mentionned in your question by the column and the row you selected so say you chose column F and row 12 it should look like this:

Range("F12").Font.Color = -4142 

So say you want to scroll through every row of a column, and change the color of every blank cell what you could do is :

Dim i As Long

For i = 1 To Rows.Count
'Column F is 6
If Cells(i, 6).Value = "" Then
  Cells(i, 1).Interior.ColorIndex = RGB(150, 0, 0)
Next i

I made slight modifications to your code and it worked:

Sub beautify()

Dim i As Long

For i = 1 To 50
 If Cells(i, 9).Value = "" Then
  ActiveSheet.Range(Cells(i, 10), Cells(i, 31)).Font.Color = vbBlack
  ActiveSheet.Range(Cells(i, 8), Cells(i, 8)).Font.Color = vbWhite
 End If

Next i

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM