suresh ullanki
Board Regular
- Joined
- Apr 29, 2013
- Messages
- 67
Hi,
I need to copy specific columns and paste into another workbook. I have written VBA code which is working fine. However, I was asked to make prompt as it has been hard coded. Can any one help with better way to achieve this.
Sub copycolumns60()
Dim lastrow As Long
Dim wkb2 As Workbook
Dim wkb As Workbook
Set wkb2 = ThisWorkbook
Set wkb = Workbooks.Open("C:\users\su\desktop\Book2.xlsx") ' Will add new workbook
lastrow = ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
' with column name
ThisWorkbook.Sheets("Sheet1").Range("A2:A" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("A2")
ThisWorkbook.Sheets("Sheet1").Range("b2:b" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("b2")
ThisWorkbook.Sheets("Sheet1").Range("c2:c" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("c2")
ThisWorkbook.Sheets("Sheet1").Range("d2:d" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("d2")
ThisWorkbook.Sheets("Sheet1").Range("e2:e" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("e2")
ThisWorkbook.Sheets("Sheet1").Range("f2:f" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("f2")
ThisWorkbook.Sheets("Sheet1").Range("g2:g" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("g2")
ThisWorkbook.Sheets("Sheet1").Range("h2:h" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("h2")
ThisWorkbook.Sheets("Sheet1").Range("i2:i" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("i2")
ThisWorkbook.Sheets("Sheet1").Range("j2:j" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("j2")
ThisWorkbook.Sheets("Sheet1").Range("k2:k" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("k2")
ThisWorkbook.Sheets("Sheet1").Range("l2:l" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("l2")
ThisWorkbook.Sheets("Sheet1").Range("m2:m" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("m2")
ThisWorkbook.Sheets("Sheet1").Range("r2:r" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("n2")
ThisWorkbook.Sheets("Sheet1").Range("s2:s" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("o2")
ThisWorkbook.Sheets("Sheet1").Range("v2:v" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("p2")
ThisWorkbook.Sheets("Sheet1").Range("w2:w" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("q2")
ThisWorkbook.Sheets("Sheet1").Range("x2:x" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("r2")
ThisWorkbook.Sheets("Sheet1").Range("y2:y" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("s2")
ThisWorkbook.Sheets("Sheet1").Range("z2:z" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("t2")
ThisWorkbook.Sheets("Sheet1").Range("aa2:aa" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("u2")
ThisWorkbook.Sheets("Sheet1").Range("ab2:ab" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("v2")
ThisWorkbook.Sheets("Sheet1").Range("ac2:ac" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("w2")
ThisWorkbook.Sheets("Sheet1").Range("ad2:ad" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("x2")
ThisWorkbook.Sheets("Sheet1").Range("ae2:ae" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("y2")
ThisWorkbook.Sheets("Sheet1").Range("af2:af" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("z2")
ThisWorkbook.Sheets("Sheet1").Range("ag2:ag" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("aa2")
ThisWorkbook.Sheets("Sheet1").Range("ah2:ah" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ab2")
ThisWorkbook.Sheets("Sheet1").Range("ai2:ai" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ac2")
ThisWorkbook.Sheets("Sheet1").Range("aj2:aj" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ad2")
ThisWorkbook.Sheets("Sheet1").Range("ak2:ak" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ae2")
ThisWorkbook.Sheets("Sheet1").Range("al2:al" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("af2")
ThisWorkbook.Sheets("Sheet1").Range("am2:am" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ag2")
ThisWorkbook.Sheets("Sheet1").Range("an2:an" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ah2")
ThisWorkbook.Sheets("Sheet1").Range("ao2:ao" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ai2")
ThisWorkbook.Sheets("Sheet1").Range("ap2:ap" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("aj2")
ThisWorkbook.Sheets("Sheet1").Range("aq2:aq" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ak2")
ThisWorkbook.Sheets("Sheet1").Range("at2:at" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("al2")
ThisWorkbook.Sheets("Sheet1").Range("au2:au" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("am2")
ThisWorkbook.Sheets("Sheet1").Range("av2:av" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("an2")
ThisWorkbook.Sheets("Sheet1").Range("aw2:aw" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ao2")
ThisWorkbook.Sheets("Sheet1").Range("ax2:ax" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ap2")
ThisWorkbook.Sheets("Sheet1").Range("ay2:ay" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("aq2")
ThisWorkbook.Sheets("Sheet1").Range("az2:az" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ar2")
ThisWorkbook.Sheets("Sheet1").Range("bq2:bq" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("as2")
ThisWorkbook.Sheets("Sheet1").Range("br2:br" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("at2")
ThisWorkbook.Sheets("Sheet1").Range("ba2:ba" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("au2")
ThisWorkbook.Sheets("Sheet1").Range("bb2:bb" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("av2")
ThisWorkbook.Sheets("Sheet1").Range("bc2:bc" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("aw2")
ThisWorkbook.Sheets("Sheet1").Range("bd2:bd" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ax2")
ThisWorkbook.Sheets("Sheet1").Range("be2:be" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ay2")
ThisWorkbook.Sheets("Sheet1").Range("bf2:bf" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("az2")
ThisWorkbook.Sheets("Sheet1").Range("bg2:bg" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ba2")
ThisWorkbook.Sheets("Sheet1").Range("bh2:bh" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("bb2")
ThisWorkbook.Sheets("Sheet1").Range("bi2:bi" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("bc2")
ThisWorkbook.Sheets("Sheet1").Range("bj2:bj" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("bd2")
ThisWorkbook.Sheets("Sheet1").Range("bk2:bk" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("be2")
End Sub
I need to copy specific columns and paste into another workbook. I have written VBA code which is working fine. However, I was asked to make prompt as it has been hard coded. Can any one help with better way to achieve this.
Sub copycolumns60()
Dim lastrow As Long
Dim wkb2 As Workbook
Dim wkb As Workbook
Set wkb2 = ThisWorkbook
Set wkb = Workbooks.Open("C:\users\su\desktop\Book2.xlsx") ' Will add new workbook
lastrow = ThisWorkbook.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
' with column name
ThisWorkbook.Sheets("Sheet1").Range("A2:A" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("A2")
ThisWorkbook.Sheets("Sheet1").Range("b2:b" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("b2")
ThisWorkbook.Sheets("Sheet1").Range("c2:c" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("c2")
ThisWorkbook.Sheets("Sheet1").Range("d2:d" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("d2")
ThisWorkbook.Sheets("Sheet1").Range("e2:e" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("e2")
ThisWorkbook.Sheets("Sheet1").Range("f2:f" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("f2")
ThisWorkbook.Sheets("Sheet1").Range("g2:g" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("g2")
ThisWorkbook.Sheets("Sheet1").Range("h2:h" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("h2")
ThisWorkbook.Sheets("Sheet1").Range("i2:i" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("i2")
ThisWorkbook.Sheets("Sheet1").Range("j2:j" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("j2")
ThisWorkbook.Sheets("Sheet1").Range("k2:k" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("k2")
ThisWorkbook.Sheets("Sheet1").Range("l2:l" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("l2")
ThisWorkbook.Sheets("Sheet1").Range("m2:m" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("m2")
ThisWorkbook.Sheets("Sheet1").Range("r2:r" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("n2")
ThisWorkbook.Sheets("Sheet1").Range("s2:s" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("o2")
ThisWorkbook.Sheets("Sheet1").Range("v2:v" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("p2")
ThisWorkbook.Sheets("Sheet1").Range("w2:w" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("q2")
ThisWorkbook.Sheets("Sheet1").Range("x2:x" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("r2")
ThisWorkbook.Sheets("Sheet1").Range("y2:y" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("s2")
ThisWorkbook.Sheets("Sheet1").Range("z2:z" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("t2")
ThisWorkbook.Sheets("Sheet1").Range("aa2:aa" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("u2")
ThisWorkbook.Sheets("Sheet1").Range("ab2:ab" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("v2")
ThisWorkbook.Sheets("Sheet1").Range("ac2:ac" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("w2")
ThisWorkbook.Sheets("Sheet1").Range("ad2:ad" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("x2")
ThisWorkbook.Sheets("Sheet1").Range("ae2:ae" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("y2")
ThisWorkbook.Sheets("Sheet1").Range("af2:af" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("z2")
ThisWorkbook.Sheets("Sheet1").Range("ag2:ag" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("aa2")
ThisWorkbook.Sheets("Sheet1").Range("ah2:ah" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ab2")
ThisWorkbook.Sheets("Sheet1").Range("ai2:ai" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ac2")
ThisWorkbook.Sheets("Sheet1").Range("aj2:aj" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ad2")
ThisWorkbook.Sheets("Sheet1").Range("ak2:ak" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ae2")
ThisWorkbook.Sheets("Sheet1").Range("al2:al" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("af2")
ThisWorkbook.Sheets("Sheet1").Range("am2:am" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ag2")
ThisWorkbook.Sheets("Sheet1").Range("an2:an" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ah2")
ThisWorkbook.Sheets("Sheet1").Range("ao2:ao" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ai2")
ThisWorkbook.Sheets("Sheet1").Range("ap2:ap" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("aj2")
ThisWorkbook.Sheets("Sheet1").Range("aq2:aq" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ak2")
ThisWorkbook.Sheets("Sheet1").Range("at2:at" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("al2")
ThisWorkbook.Sheets("Sheet1").Range("au2:au" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("am2")
ThisWorkbook.Sheets("Sheet1").Range("av2:av" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("an2")
ThisWorkbook.Sheets("Sheet1").Range("aw2:aw" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ao2")
ThisWorkbook.Sheets("Sheet1").Range("ax2:ax" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ap2")
ThisWorkbook.Sheets("Sheet1").Range("ay2:ay" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("aq2")
ThisWorkbook.Sheets("Sheet1").Range("az2:az" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ar2")
ThisWorkbook.Sheets("Sheet1").Range("bq2:bq" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("as2")
ThisWorkbook.Sheets("Sheet1").Range("br2:br" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("at2")
ThisWorkbook.Sheets("Sheet1").Range("ba2:ba" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("au2")
ThisWorkbook.Sheets("Sheet1").Range("bb2:bb" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("av2")
ThisWorkbook.Sheets("Sheet1").Range("bc2:bc" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("aw2")
ThisWorkbook.Sheets("Sheet1").Range("bd2:bd" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ax2")
ThisWorkbook.Sheets("Sheet1").Range("be2:be" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ay2")
ThisWorkbook.Sheets("Sheet1").Range("bf2:bf" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("az2")
ThisWorkbook.Sheets("Sheet1").Range("bg2:bg" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("ba2")
ThisWorkbook.Sheets("Sheet1").Range("bh2:bh" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("bb2")
ThisWorkbook.Sheets("Sheet1").Range("bi2:bi" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("bc2")
ThisWorkbook.Sheets("Sheet1").Range("bj2:bj" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("bd2")
ThisWorkbook.Sheets("Sheet1").Range("bk2:bk" & lastrow).Copy Destination:=wkb.Sheets("Headcount Reg").Range("be2")
End Sub