Amendment To Existing Code

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,816
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. I need it do exactly the same thing but copy and insert below instead. I imagine it has something to do with the offsets but don't know what! Thanks.

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
 
EntireRow.Insert will always insert above. Since you are copying the row it won't make any difference. The difference will be in which row you update.
So if you want the second row to update just remove the offset(-1) from this row.

Rich (BB code):
                .Offset(-1).Resize(, 2).Value = Dn2.Offset(, 1).Resize(, 2).Value
 
Upvote 0
Solution
EntireRow.Insert will always insert above. Since you are copying the row it won't make any difference. The difference will be in which row you update.
So if you want the second row to update just remove the offset(-1) from this row.

Rich (BB code):
                .Offset(-1).Resize(, 2).Value = Dn2.Offset(, 1).Resize(, 2).Value
Much obliged squire.
 
Upvote 0
EntireRow.Insert will always insert above. Since you are copying the row it won't make any difference. The difference will be in which row you update.
So if you want the second row to update just remove the offset(-1) from this row.

Rich (BB code):
                .Offset(-1).Resize(, 2).Value = Dn2.Offset(, 1).Resize(, 2).Value
Not sure if you can help with this please. I run the code with your amendment and it works fine but on a particular file I get the error below?

1738053049275.png
 
Upvote 0
on a particular file I get the error below?
What do you mean by this ? Are you running it against multiple files.
If you use F8 to step through the code, what line causes it to generate that error message ?
Inside the inner for loop add a line that says:
Debug.Print "n = " & n, "dn2 = " & Dn2.address
What is the last value of that line in the immediate window before it crashes ?
 
Upvote 0

Forum statistics

Threads
1,226,842
Messages
6,193,293
Members
453,789
Latest member
t_mariee

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