Dazzawm
Well-known Member
- Joined
- Jan 24, 2011
- Messages
- 3,783
- Office Version
- 365
- Platform
- Windows
I have gone from a windows 7 32-bit to a windows 11 64-bit PC. The code below is doing opposite to what it should! I have sheet 2 with a number in A and another in B. When the number in A is found in column AE on sheet 1 it is supposed to copy the row and insert below and replace with the number that is next to it in column B sheet 2. But it inserts the row above instead! Would somebody help please?
Code:
Sub FindCopyReplaceBelow()
Application.EnableEvents = False
Application.Calculation = xlCalculationManual = False
Application.ScreenUpdating = False
Dim Rng1 As Range
Dim Dn1 As Range
Dim Rng2 As Range
Dim Dn2 As Range
With Sheets("Sheet2")
Set Rng2 = .Range(.Range("A1"), .Range("A" & Rows.Count).End(xlUp))
End With
With Sheets("Sheet1")
Set Rng1 = .Range(.Range("AE2"), .Range("AE" & Rows.Count).End(xlUp))
End With
For Each Dn2 In Rng2
For Each Dn1 In Rng1
If Dn1 = Dn2 Then
Dn1.EntireRow.Interior.ColorIndex = 5
Dn1.EntireRow.Copy
Dn1.Offset(1).EntireRow.Insert Shift:=xlDown
Dn1.Offset(1).Resize(, 2).Value = Dn2.Offset(, 1).Resize(, 2).Value
End If
Next Dn1
Next Dn2
Application.EnableEvents = True
Application.Calculation = xlCalculationManual = True
Application.ScreenUpdating = True
End Sub