I am trying to Copy from 1 workbook (S.xls) and pasting into (PD.xlsm). However I also get the same error 1004- Application-defined or object-defined error at this line "lastCol = sh1.Cells(1, Columns.Count).End(xlToLeft).Column"
Also there there a way to edit this so that it puts the number in a 11 digit format 100xxxxxxxx?
VBA Code:
Sub Rectangle1_Click()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim wb1 As Workbook, wb2 As Workbook
Dim j As Long, lr1 As Long, lr2 As Long
Dim f As Range
Application.ScreenUpdating = False
Set wb1 = Workbooks("S.xls")
Set sh1 = wb1.Sheets(1) 'origin
Set wb2 = Workbooks("PD.xlsm")
Set sh2 = wb2.Sheets("PD") 'destination
'last row on origin sheet
lr1 = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
'last row on destination sheet
lr2 = sh2.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
'for each head in sh1 (origin)
lastCol = sh1.Cells(1, Columns.Count).End(xlToLeft).Column
For j = 1 To lastCol
Set f = sh2.Rows(1).Find(sh1.Cells(1, j), , xlValues, xlWhole, , , False)
If Not f Is Nothing Then
sh2.Cells(lr2, f.Column).Resize(lr1).Value = sh1.Cells(2, j).Resize(lr1).Value
End If
Next
wb1.Close False
Application.ScreenUpdating = True
MsgBox "End"
End Sub
Also there there a way to edit this so that it puts the number in a 11 digit format 100xxxxxxxx?
Last edited by a moderator: