2017-10-11 72 views
0

我收到一条错误消息,说明我的VBA宏(错误1004应用程序或对象定义的错误)。 我使用Windows 10和Excel 2016 这里是我的代码:我收到我的vba宏的错误消息

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 

THX在您的帮助 PS:第一次问这里,问题可能不是完美的格式。

+3

哪种说法导致错误? – FunThomas

+0

什么是'nextrow'它似乎没有在任何地方声明 – Tom

+0

如果ActiveCell.Range(n,1)= ActiveCell.Range(nextRow,1)然后 –

回答

0

为什么你需要VBA?

考虑到您在选择范围A2: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 
+0

非常感谢<3 –

+0

@ThomasSchmid不客气! :) – sktneer

相关问题