Error In Code

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,805
Office Version
  1. 365
Platform
  1. Windows
I have a code that basically looks for data in column A on sheet 2, in column AE on sheet 1. When that number is found it copies and inserts the entire row above and changes what is in column A to what's in column B on sheet 2 into the copied and inserted row in AE on sheet 1.

All of a sudden I am getting this error on some files. Any ideas please?

1738080052818.png


Code:
Sub FindCopyReplaceAbove()

Application.EnableEvents = False
Application.Calculation = xlCalculationManual = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim rng1    As Range
Dim Dn1     As Range
Dim rng2    As Range
Dim Dn2     As Range
Dim n       As Long
With Sheets("Sheet2")
    Set rng2 = .Range(.Range("A1"), .Range("A" & Rows.count).End(xlUp))
End With
For Each Dn2 In rng2
With Sheets("Sheet1")
    Set rng1 = .Range(.Range("AE2"), .Range("AE" & Rows.count).End(xlUp))
        For n = rng1.count + 1 To 2 Step -1
            With .Range("AE" & n)
            If .Value = Dn2 Then
                .EntireRow.Interior.ColorIndex = 35
                .EntireRow.Copy
                .EntireRow.Insert Shift:=xlUp
                .Offset(-1).Resize(, 2).Value = Dn2.Offset(, 1).Resize(, 2).Value
            End If
            End With
            Next n
   End With
 Next Dn2
 
Application.EnableEvents = True
Application.Calculation = xlCalculationManual = True
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
What cell address is Dn1 when the code errors in the line below

VBA Code:
Dn1.Offset(1).EntireRow.Insert Shift:=xlDown
 
Upvote 0
What cell address is Dn1 when the code errors in the line below

VBA Code:
Dn1.Offset(1).EntireRow.Insert Shift:=xlDown
It doesn't error as such with a yellow line, just brings up the photoed error after a few seconds.
 
Upvote 0
Put a Debug.Print Dn1.Address line at the start of the loop and post the last address that prints in the immediate window when the code errors

Rich (BB code):
For Each Dn1 In rng1
Debug.Print Dn1.Address
            If Dn1 = Dn2 Then
 
Upvote 0
Put a Debug.Print Dn1.Address line at the start of the loop and post the last address that prints in the immediate window when the code errors

Rich (BB code):
For Each Dn1 In rng1
Debug.Print Dn1.Address
            If Dn1 = Dn2 Then
Typical! I run it then on said file and it completed. I will have to post next time it happens. Watch this space.
 
Upvote 0
No formulas on sheet 1. Columns up to and including A to AY. Macro works well on majority of files and depends on amount of data on sheet 2.
Try this change as I do something else with matrices for both sheets.

VBA Code:
Sub FindCopyReplaceAbove()
  Dim rng1    As Range, Dn1     As Range
  Dim rng2    As Range, Dn2     As Range
  Dim n       As Long
  Dim dic     As Object
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual = False
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set dic = CreateObject("Scripting.Dictionary")
  
  With Sheets("Sheet2")
    Set rng2 = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
  End With
  For Each Dn2 In rng2
    dic(Dn2.Value) = Dn2.Row
  Next
  
  With Sheets("Sheet1")
    Set rng1 = .Range(.Range("AE2"), .Range("AE" & Rows.Count).End(xlUp))
    For n = rng1.Count + 1 To 2 Step -1
      With .Range("AE" & n)
        If dic.exists(.Value) Then
          .EntireRow.Interior.ColorIndex = 35
          .EntireRow.Copy
          .EntireRow.Insert Shift:=xlUp
          .Offset(-1).Resize(, 2).Value = Sheets("Sheet2").Range("A" & dic(.Value)).Offset(, 1).Resize(, 2).Value
        End If
      End With
    Next n
  End With
   
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
End Sub

😇
 
Upvote 0
Try this change as I do something else with matrices for both sheets.

VBA Code:
Sub FindCopyReplaceAbove()
  Dim rng1    As Range, Dn1     As Range
  Dim rng2    As Range, Dn2     As Range
  Dim n       As Long
  Dim dic     As Object
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual = False
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  Set dic = CreateObject("Scripting.Dictionary")
 
  With Sheets("Sheet2")
    Set rng2 = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
  End With
  For Each Dn2 In rng2
    dic(Dn2.Value) = Dn2.Row
  Next
 
  With Sheets("Sheet1")
    Set rng1 = .Range(.Range("AE2"), .Range("AE" & Rows.Count).End(xlUp))
    For n = rng1.Count + 1 To 2 Step -1
      With .Range("AE" & n)
        If dic.exists(.Value) Then
          .EntireRow.Interior.ColorIndex = 35
          .EntireRow.Copy
          .EntireRow.Insert Shift:=xlUp
          .Offset(-1).Resize(, 2).Value = Sheets("Sheet2").Range("A" & dic(.Value)).Offset(, 1).Resize(, 2).Value
        End If
      End With
    Next n
  End With
  
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
End Sub

😇
Thanks, so is this for the copy above or below?
 
Upvote 0
Sorry for my bad. I was having the error on the copy below with the code in post 7.
 
Upvote 0
Try this code, it's about your new code from post #7


VBA Code:
Sub FindCopyReplaceAbove()
  Dim rng1    As Range, Dn1     As Range
  Dim rng2    As Range, Dn2     As Range
  Dim n       As Long
  Dim dic     As Object
  
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual = False
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Set dic = CreateObject("Scripting.Dictionary")
  
  With Sheets("Sheet2")
    Set rng2 = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
  End With
  For Each Dn2 In rng2
    dic(Dn2.Value) = Dn2.Row
  Next
  
  With Sheets("Sheet1")
    Set rng1 = .Range(.Range("AE2"), .Range("AE" & Rows.Count).End(xlUp))
  End With
  For Each Dn1 In rng1
    If dic.exists(Dn1.Value) Then
      Dn1.EntireRow.Interior.ColorIndex = 35
      Dn1.EntireRow.Copy
      Dn1.Offset(1).EntireRow.Insert Shift:=xlDown
      Dn1.Offset(1).Resize(, 2).Value = Sheets("Sheet2").Range("A" & dic(Dn1.Value)).Offset(, 1).Resize(, 2).Value
    End If
  Next Dn1
  
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,063
Messages
6,188,655
Members
453,489
Latest member
jessrw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top