0

I have to copy data from column U, AR and BF from sheet 1 and paste it to column A, B, C in sheet 2. I need a vba macro code for this.

Thanks in advance.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73

1 Answers1

0

look at

How can I copy columns from one sheet to another with VBA in Excel?

How to record macro in excel

 Sub foo1()

    Dim src As Worksheet
    Dim trg As Worksheet
    Dim LastRow As Long


    Set src = ThisWorkbook.Worksheets("Sheet1")
    Set trg = ThisWorkbook.Worksheets("Sheet2")




    src.Range("U:U").Copy Destination:=trg.Range("A1")


    src.Range("AR:AR").Copy Destination:=trg.Range("B1")


    src.Range("BF:BF").Copy Destination:=trg.Range("C1")


    End Sub
Abhinav Rawat
  • 452
  • 3
  • 15
  • If you declare `Dim LastRow, i As Long` then only `i` is of type `Long` but `LastRow` will be `Variant` it is exactly the same like `Dim LastRow As Variant, i As Long`. You need to specify a type for **every** variable like `Dim LastRow As Long, i As Long` otherwise Excel always assumes `Variant`! – Pᴇʜ Oct 30 '17 at 07:14
  • @Peh oh! I didn't knew that, thanks for sharing, i'll keep that in my mind :) – Abhinav Rawat Oct 30 '17 at 07:17
  • sorry to say @AbhinavRawat this is giving me a huge error. When I run the macro it is shifting only the third column. –  Oct 31 '17 at 05:45
  • other 2 columns are being replaced by the third –  Oct 31 '17 at 05:45
  • @RavindraSinghRawat try now – Abhinav Rawat Oct 31 '17 at 06:31