我的VBA宏收到错误消息(错误1004应用程序或对象定义错误)。 我正在使用Windows 10和Excel 2016。 这是我的代码:
Sub Fehlercheck()
n = 0
i = 0
Do
If ActiveCell.Range(n, 1) = ActiveCell.Range(nextRow, 1) Then
ActiveCell.Offset(nextRow, 0).Select
n = n + 1
Else
ActiveCell.Offset(-n, 1).Select
Do While i <= n
If ActiveCell = ActiveCell.Offset(nextRow, 0) Then
ActiveCell.Offset(nextRow, 0).Select
i = i + 1
Else
ActiveSheet.Range(ActiveCell.Offset(-i, -1), Cells(n - i, 0)).Select
Selection.Interior.Color = RGB(255, 0, 0)
ActiveSheet.Cells(n + 1, 1).Select
End If
Loop
End If
Loop While ActiveCell.Offset(nextRow, 0).Value <> 0
End Sub
提前帮助你
Ps:第一次问这里,问题可能不是完美的格式。
答案 0 :(得分:0)
为什么你需要VBA呢?
使用规则= $ A2&lt;&gt; $ B2的条件格式可以很容易地实现这一点,考虑到您在选择范围A2:B2后应用此规则并根据您的选择设置格式。
但是如果出于某种原因,你想在VBA的帮助下实现这一点,那就试试吧......
Sub CompareCells()
Dim LastRow As Long
Dim Rng As Range, Cell As Range
Application.ScreenUpdating = False
'find last row used in column A
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
'setting the range in column A, assuming data starts from row2
Set Rng = Range("A2:A" & LastRow)
'looping through cells in column A
For Each Cell In Rng
If Cell <> Cell.Offset(0, 1) Then
Cell.Resize(1, 2).Interior.Color = vbRed 'this will apply the color to column A and B
'Cell.Interior.Color = vbRed 'this will apply color only to column A
End If
Next Cell
Application.ScreenUpdating = True
End Sub