尝试在单元格内基于字符串突出显示单元格

时间:2014-10-23 17:53:15

标签: string excel vba

基本上我要做的是以下内容:

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

一旦确定了,我想突出显示绿色(如果单元格的值大于5)或蓝色(如果单元格的值保持在4到5之间)的单元格。

上面的if块没有给我带来麻烦,它是初始程序。

阻止我完成此操作的原因是运行时[错误' 91']:"对象变量或With块变量未设置"。

我有一些编程经验,但没有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

4 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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

Set columnD = Range("D:D")

其次,在For循环中,您将整数与范围进行比较。如果要循环到最右侧的列,可以执行以下操作:

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

第三,您似乎打算将i用于行,j用于列?如果是这样,那么您的j位置错误。

假设i是行且j是列,我相信当您检查单元格中的值时,您应该引用Cells(i, j,),(制作列和行选择动态)而不是硬编码5的值。

最后,您实际上并不需要在开头声明的那三个范围变量。没有必要将细胞定位在现有范围内(尽管如果您愿意,也可以)。 VBA假定您正在处理活动工作簿的活动工作表。只要这两个假设成立,那么Cells(i,j)就可以了。如果您想添加一些特异性/防止在错误的工作表上运行,您可以使用Sheets("Sheet1").Cells(i,j)

PS - 我认为&#39;#&#39; 5之后是一个错字?

答案 2 :(得分:0)

你可能想要将范围值分配给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

答案 3 :(得分:0)

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

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