Sub copycolumns()
Dim y As Workbook
Dim x As Workbook
Dim ws As Worksheet
Dim sh As Worksheet
Set x = Workbooks.Open("C:\Users\hshabbir\Desktop\testing.xlsx")
Set sh = x.Sheets("Sheet2")
Set y = ThisWorkbook
Set ws = y.Sheets("Sheet1")
Application.ScreenUpdating = 0
Dim i As Integer, searchedcolumn As Integer, searchheader As Object
For i = 1 To 8
Set searchheader = ws.Cells(1, i)
searchedcolumn = 0
On Error Resume Next
searchedcolumn = sh.Rows(1).Find(what:=searchheader.Value, lookat:=xlWhole).Column
On Error GoTo 0
If searchedcolumn <> 0 Then
ws.Columns(searchedcolumn).Copy Destination:=searchheader
End If
Next i
x.Close
End Sub
When i use following programme withen work book, it works fine and copies the date from one sheets to another. Sub copycolumns1()
Dim i As Integer, searchedcolumn As Integer, searchheader As Object
Set searchheader = Sheets(2).Cells(1, 1)
searchedcolumn = 0 On Error Resume Next searchedcolumn = Sheets(1).Rows(1).Find(what:=searchheader.Value, lookat:=xlWhole).Column On Error GoTo 0
If searchedcolumn <> 0 Then Sheets(1).Columns(searchedcolumn).Copy Destination:=searchheader End If
End Sub
but i am not getting it worked for moving from one workbook to another workbook
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.