有关无效限定符上的Excel VBA的问题

问题描述 投票:0回答:1

我目前正在从事vba任务...但是我陷入了困境。它说有无效的限定词并且无法运行。请帮助,非常感谢!!!

Dim i As Integer
Dim RowCount As Integer
Dim category As String
Dim product As String
Dim quantity As String

With Sheets("Sheet1")
    RowCount = Cells(Rows.Count, 1).End(xlUp).Row

    ' Another way to count the number of rows needed

    Range("A1").Select
    category = ActiveCell.Offset(i, 0).Value
    product = ActiveCell.Offset(i, 1).Value
    quantity = ActiveCell.Offset(i, 2).Value

    For i = 2 To RowCount
        ' insert the IF/THEN/ELSE structure here to
        ' format the font color of Fruits to blue,
        ' and the rest of the others to magenta


    If category = "Fruits" Then
    category.Font.Color = vbBlue
    product.Font.Color = vbBlue
    quantity.Font.Color = vbBlue

    Else
    category.Font.Color = vbMagenta
    product.Font.Color = vbMagenta
    quantity.Font.Color = vbMagenta
    End If
    Next

End With
excel vba
1个回答
0
投票

使用范围和多头。将集合移入循环。

Dim i As Long
Dim RowCount As Long
Dim category As Range
Dim product As Range
Dim quantity As Range

With Sheets("Sheet1")
    RowCount = .Cells(.Rows.Count, 1).End(xlUp).Row

    ' Another way to count the number of rows needed

    For i = 2 To RowCount
    Set category = .Cells(i, 1)
    Set product = .Cells(i, 2)
    Set quantity = .Cells(i, 3)
        ' insert the IF/THEN/ELSE structure here to
        ' format the font color of Fruits to blue,
        ' and the rest of the others to magenta


        If category = "Fruits" Then
            category.Font.Color = vbBlue
            product.Font.Color = vbBlue
            quantity.Font.Color = vbBlue
        Else
            category.Font.Color = vbMagenta
            product.Font.Color = vbMagenta
            quantity.Font.Color = vbMagenta
        End If
    Next

End With
© www.soinside.com 2019 - 2024. All rights reserved.