如何使用VBA更改单元格的字体颜色?

时间:2021-12-01 03:44:54

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.

我想根据一个条件在我的excel表中选择行,然后编辑同一行中的其他单元格的格式。

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

我知道我可以使用autofilters(列n = "" ")来选择行:

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.

现在如何改变我选择的直线的F (F)的字体。

2 个解决方案

#1


1  

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:

可以使用VBA使用. interior更改单元格的背景颜色。ColorIndex = RGB(r, g, b):用. font表示单元格内文本的红色和字体颜色。颜色= RGB(r, g, b):红色表示改变这些属性的范围应该被定义,就像你的问题中提到的由你所选择的列和行所决定的范围那么你选择的列F和行12应该是这样的:

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

#2


0  

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

#1


1  

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:

可以使用VBA使用. interior更改单元格的背景颜色。ColorIndex = RGB(r, g, b):用. font表示单元格内文本的红色和字体颜色。颜色= RGB(r, g, b):红色表示改变这些属性的范围应该被定义,就像你的问题中提到的由你所选择的列和行所决定的范围那么你选择的列F和行12应该是这样的:

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

#2


0  

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