0

I keep getting an error at the end of my macro. It says Next without for but am not sure how to fix it! This exact macro worked in another sheet so there lies my confusion. Thanks for the help! I have reviewed other threads and just cannot seem to come up with the right solution for my specific macro.

Sub Run()

Cells.Interior.ColorIndex = 0


For i = 1 To 10

    Worksheets("Sheet0").Activate
    D0 = Cells(i, 1)
    
    For j = 1 To 10
    
        Worksheets("Sheet1").Activate
        D1 = Cells(j, 1)
        
        If (D0 = D1) Then
            'Prefix
            'Worksheets("Sheet0").Activate
            'H0 = Cells(i, 2)
            'Worksheets("Sheet1").Activate
            'H1 = Cells(j, 2)
            'If (H0 <> H1) Then
            'Worksheets("Sheet0").Activate
                 'Cells(i, 2).Interior.ColorIndex = 6
            'Else
            'End If
            
            'First Name
            'Worksheets("Sheet0").Activate
            'H0 = Cells(i, 2)
            'Worksheets("Sheet1").Activate
            'H1 = Cells(j, 3)
            'If (H0 <> H1) Then
            'Worksheets("Sheet0").Activate
                 'Cells(i, 2).Interior.ColorIndex = 6
            'Else
            'End If
            
            'Middle Name
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 3)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 4)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 3).Interior.ColorIndex = 6
            Else
            End If
            
            'Last Name
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 4)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 5)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 4).Interior.ColorIndex = 6
            Else
            End If
            
            'Suffix
            'Worksheets("Sheet0").Activate
            'H0 = Cells(i, 6)
            'Worksheets("Sheet1").Activate
            'H1 = Cells(j, 6)
            'If (H0 <> H1) Then
            'Worksheets("Sheet0").Activate
                 'Cells(i, 6).Interior.ColorIndex = 6
            'Else
            'End If
            
            'Maiden Name
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 5)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 6)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 5).Interior.ColorIndex = 6
            Else
            End If
            
            
            'Preferred Name
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 2)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 3)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 2).Interior.ColorIndex = 6
            Worksheets("Sheet0").Activate
            If Not IsEmpty(Cells(i, 3)) Then
                 Cells(i, 8).Interior.ColorIndex = 6
            Else
            End If
            
            'Date of Birth
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 6)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 7)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 6).Interior.ColorIndex = 6
            Else
            End If
            
            'Email 1 vs 1
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 7)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 8)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 7).Interior.ColorIndex = 6
            Else
            End If
            
            'Email 1 vs 2
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 7)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 9)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 8).Interior.ColorIndex = 6
            Else
            End If
            
             'Email 1 vs 3
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 7)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 10)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 9).Interior.ColorIndex = 6
            Else
            End If
            
             'Email 1 vs 4
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 7)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 11)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 10).Interior.ColorIndex = 6
            Else
            End If
            
            'Email 1 vs 5
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 7)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 12)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 11).Interior.ColorIndex = 6
            Else
            End If
            
            'Phone Number 1 vs 1
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 12)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 13)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 12).Interior.ColorIndex = 6
            Else
            End If
            
             'Phone Number 1 vs 2
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 12)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 14)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 13).Interior.ColorIndex = 6
            Else
            End If
            
               'Phone Number 1 vs 3
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 12)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 15)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 14).Interior.ColorIndex = 6
            Else
            End If
            
                'Phone Number 1 vs 4
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 12)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 16)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 15).Interior.ColorIndex = 6
            Else
            End If
            
            'Address
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 16)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 17)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 16).Interior.ColorIndex = 6
            Else
            End If
            
            'Address2
            'Worksheets("Sheet0").Activate
            'H0 = Cells(i, 26)
            'Worksheets("Sheet1").Activate
            'H1 = Cells(j, 11)
            'If (H0 <> H1) Then
            'Worksheets("Sheet0").Activate
                 'Cells(i, 26).Interior.ColorIndex = 6
            'Else
            'End If
            
            'Address3
            'Worksheets("Sheet0").Activate
            'H0 = Cells(i, 27)
            'Worksheets("Sheet1").Activate
            'H1 = Cells(j, 12)
            'If (H0 <> H1) Then
            'Worksheets("Sheet0").Activate
                 'Cells(i, 27).Interior.ColorIndex = 6
            'Else
            'End If
            
             'City
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 17)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 19)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 17).Interior.ColorIndex = 6
            Else
            End If
            
             'State
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 18)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 20)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 18).Interior.ColorIndex = 6
            Else
            End If
            
             'ZIP
            Worksheets("Sheet0").Activate
            H0 = Cells(i, 19)
            Worksheets("Sheet1").Activate
            H1 = Cells(j, 21)
            If (H0 <> H1) Then
            Worksheets("Sheet0").Activate
                 Cells(i, 19).Interior.ColorIndex = 6
            Else
            End If

        Else
        End If
    
    Next j

Next i

Worksheets("Sheet0").Select

End Sub
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 3
    You haven't structured your If-Then blocks under "Preferred name" correctly (indenting your code consistently would help make that more obvious). FYI there is no need to activate a sheet in order to read from it or update it. https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba – Tim Williams Jun 15 '20 at 22:21
  • Awesome, thank you! I guess staring at it for so long can make you miss the simplest of things. Indenting will be in my future for sure. – Garrett Holland Jun 16 '20 at 01:05

0 Answers0