我目前正在从事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
使用范围和多头。将集合移入循环。
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