0
Columns("Q:Q").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Dim lastRow As Long
lastRow = Range("O3" & Rows.Count).End(xlUp).Row
Range("P4:P" & lastRow).Select
Selection.Copy
Range("Q4").Select
ActiveSheet.Paste

I am trying to insert a column next to Q column, copy the data from P column based on the length of the adjacent column ("O") and paste the data into the inserted column.

Above is the code I am using to achieve this. But the problem is column P and O don't have the same length of data every time.

I am not sure what I am missing here. Someone correct me where I am doing it wrong.

Thanks in Advance!

Community
  • 1
  • 1
SenthamilVM
  • 93
  • 1
  • 3
  • 13

1 Answers1

1

Try this ("O3" & Rows.Count caused problems). Also no need to select cells.

Columns("Q:Q").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Dim lastRow As Long
lastRow = Range("O" & Rows.Count).End(xlUp).Row
Range("P4:P" & lastRow).Copy Range("Q4")
SJR
  • 22,986
  • 6
  • 18
  • 26
  • Pleased to hear it. Would you mind accepting the answer which will just take me over the 9k mark? – SJR Jul 04 '18 at 12:02