简体   繁体   English

EXCEL VBA将选定的单元格更改为颜色

[英]EXCEL VBA change selected cells to colour

I am trying to write a peice of VB that will look at a run an iff statement and then if true change the colour of the row that was true. 我正在尝试编写VB的代码,该代码将查看运行iff语句,然后如果为true,则更改为true的行的颜色。 Currently I can get it only to change the colour for one cell. 目前,我只能更改一个单元格的颜色。

the VB looks like this. VB看起来像这样。

Sub Blue()

Set A = Sheets("Analysis")

Dim d
Dim j
d = 1
j = 1

Do Until IsEmpty(A.Range("B" & j))


If (A.Range("F" & j) <> A.Range("G" & j)) Or (A.Range("H" & j) <> A.Range("I" & j)) Then

d = d + 1
A.Range("B" & j).Interior.ColorIndex = 28

End If

j = j + 1
Loop

End Sub

That works. 这样可行。 When i try to change this to highlight a selection of cells in the table. 当我尝试更改此值以突出显示表中的单元格选择时。 i get a 1004 run time error Sub Blue() 我收到1004运行时错误Sub Blue()

Set A = Sheets("Analysis")

Dim d
Dim j
d = 1
j = 1

Do Until IsEmpty(A.Range("B" & j))


If (A.Range("F" & j) <> A.Range("G" & j)) Or (A.Range("H" & j) <> A.Range("I" & j)) Then

d = d + 1
A.Range("A:U" & j).Interior.ColorIndex = 28

End If

j = j + 1
Loop

End Sub

I have spent loads of time looking on the internet and cant find any answeres to why this does not work. 我花了很多时间在互联网上寻找并找不到任何答案,为什么这不起作用。 any help would be appreicated. 任何帮助将不胜感激。

I am using VB as this will be part of a much larget peice of VB. 我正在使用VB,因为这将是VB很大一部分的一部分。

To change colour of columns A to U use this: 要将列A的颜色更改为U,请使用以下方法:
A.Range("A" & j & ":U" & j).Interior.ColorIndex = 28 - in your line you are missing the row for column A. A.Range("A" & j & ":U" & j).Interior.ColorIndex = 28您的行中缺少A列的行。

To change colour of the entire row use this: 要更改整个行的颜色,请使用以下命令:
A.Range("B" & j).EntireRow.Interior.ColorIndex = 28

我没有通过任何PC进行测试,但是您可以尝试

A.Range("A:U").rows(j).Interior.ColorIndex = 28

A range for more then one cell should be in this format "A1:U1", your code is equal to "A:U1". 一个以上单元格的范围应采用“ A1:U1”格式,您的代码等于“ A:U1”。

You could code like this: 您可以这样编写:

A.Range("A" & j & ":U" & j).Interior.ColorIndex = 28

I prefer this code: 我喜欢下面的代码:

A.Range(Cells(j, 1), Cells(j, 21)).Interior.ColorIndex = 28

Good Luck 祝好运

Jorgen Unger 乔根·昂格(Jorgen Unger)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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