2017-10-11 76 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

相關問題