试图在单元格内基于string突出显示单元格

基本上我想要做的是以下几点:

首先,根据单元格的string值确定我是否在正确的单元格行中。 即如果当前单元格的string值包含stringAB1或AB2,则遍历整行。

一旦确定了,我想强调单元格为绿色(如果单元格的值大于5)或蓝色(如果单元格的值在4和5之间)。

以上如果阻止不给我麻烦,这是最初的程序。

什么阻止我完成这是运行时[错误'91']:“对象variables或块variables未设置”。

我有一些编程经验,但没有VBA的经验。 任何帮助将不胜感激。

Sub ChangeCellColor() Dim columnD As Range Dim str1, str2 As String Dim currCell As Range Dim rightCell As Range Dim i As Long str1 = "AB1" str2 = "AB2" Columns(1).Font.Color = vbBlack For i = 1 To Rows.Count 'If the current cell in the D column contains either the string AB1 or AB2, it will look into the values here. If (currCell.Cells(i, 4).Value = str1) Or (currCell.Cells(i, 4).Value = str2) Then 'From the cell range of For j = 1 To Range("E10").End(xlToRight) If rightCell.Cells(j, 5) >= 5# Then rightCell.Interior.Color = vbRed ElseIf (rightCell.Cells(j, 5) >= 4 And rightCell.Cells(j, 5) <= 4.99) Then cell.Interior.Color = vbYellow End If Next j End If Next i End Sub 

试试这个:下面的代码查看D列中的每个单元格,并检查单元格值以确定cell.value = str1str2 。 然后,循环遍历该行中的每个单元格,从E列开始,根据您的参数更改颜色。

另外,请尝试Worksheet对象的Usedrange属性以获取所需的行数。

 Sub ChangeCellColor() Dim str1, str2 As String Dim i As Integer Dim j As Integer Dim col As Integer str1 = "AB1" str2 = "AB2" Columns(1).Font.Color = vbBlack For i = 1 To ThisWorksheet.Usedrange.Rows.Count With ThisWorksheet 'If the current cell in the D column contains either the string AB1 or AB2, it will look into the values here. If .Cells(i, 4).Value = str1 Or .Cells(i, 4).Value = str2 Then col = .Range("D" & i).End(xltoRight).Column For j = 5 To col If .Cells(i, j).Value >= 5 Then .Cells(i,j).Interior.Color = vbRed Else If .Cells(i, j).Value >= 4 And .Cells(i, j).Value <= 4.99 Then .Cells(i,j).Interior.Color = vbYellow End If End If Next j End If End With Next i End Sub 

有几个问题…首先,错误是因为分配给Rangevariables需要Set关键字,如下所示:

 Set columnD = Range("D:D") 

其次,在For循环中,你将一个整数与一个范围进行比较。 如果你想循环到最右边的列,你可以这样做:

 For j = 1 to Range("E10").End(xlToRight).Column 

第三,它看起来像你打算使用i的行和j的列? 如果是这样,你已经把你的j放在了错误的地方。

假设i是行, j是列,我相信当你检查你的单元格的值,你应该引用Cells(i, j,) ,(使列和行selectdynamic),而不是硬编码值5

最后,你实际上并不需要你在开始时声明的三个范围variables。 没有必要将细胞放置在现有的范围内(尽pipe你可以如果你愿意)。 VBA假定您正在处理活动工作簿的活动工作表。 只要这两个假设成立, Cells(i,j)就可以正常工作。 如果您想在错误的表单上添加一些特异性/保护function,可以使用Sheets("Sheet1").Cells(i,j)

PS – 我认为5号之后的“#”是一个错字?

您可能要将范围值分配给currCell和rightCell或将其除去。

 Sub ChangeCellColor() Dim columnD As Range Dim str1, str2 As String Dim currCell As Range Dim rightCell As Range Dim i As Long str1 = "AB1" str2 = "AB2" Columns(1).Font.Color = vbBlack For i = 1 To Rows.Count 'If the current cell in the D column contains either the string AB1 or AB2, it will look into the values here. If (Cells(i, 4).Value = str1) Or (Cells(i, 4).Value = str2) Then 'From the cell range of For j = 1 To Range("E10").End(xlToRight) If Cells(j, 5) >= 5 Then Cells(j, 5).Interior.Color = vbRed ElseIf (Cells(j, 5) >= 4 And Cells(j, 5) <= 4.99) Then Cells(j, 5).Interior.Color = vbYellow End If Next j End If Next i End Sub 

我认为没有VBA,但有条件格式,对于绿色,公式规则,如:

 =AND(OR(NOT(ISERROR(FIND("AB1",$D1))),NOT(ISERROR(FIND("AB2",$D1)))),N(A1)>5)