sorry i am not experienced in VBA. I have 2 workbooks (saleman .xls and DailyReport.xlsm). Saleman .xls is generated by my POS daily by over writing the existing data in the file. DailyReport.xlsm has functions to generate reports using data copied from saleman .xls. I am trying make a vba script that will copy data from saleman .xls ( excluding headings) and paste to the last row of the table in sheet 1 of DailyReport.xlsm. i tried the code below and received thiS error message "run time error 1004 application-defined or object-defined error".
Sub CopyReps_btn()
Dim WS, WS2 As Worksheet
Dim LastRow As Long
'Dim lastCell As Range
Dim LastCellRowNumber As Long
Dim sPath As String, sFile As String
Dim wb As Workbook, wb2 As Workbook
Dim vFile As Variant
'Set source workbook
Set wb = ThisWorkbook
Set wb = ActiveWorkbook
Set WS = ActiveSheet
With WS
Set lastCell = .Cells(.Rows.Count, "C").End(xlUp)
LastCellRowNumber = lastCell.Row + 1
End With
'Set selectedworkbook
sPath = "C:\Sales2016APD\DownloadAPD\"
sFile = sPath & "salesman.xls"
Workbooks.Open (sFile)
Set wb2 = ActiveWorkbook
Set WS2 = wb2.Sheets("salesman")
'last row
With WS2
LastRow = .Cells(.Rows.Count, A).End(xlUp).Row
'Select cells to copy
wb2.Worksheets("Output").Range("A2", "N" & LastRow).Select
Selection.Copy
End With
'Go back to original workbook you want to paste into
wb.Activate
'Paste starting at the last empty row
wb.Worksheets("DailyReport.xlsm").Range("C" & LastCellRowNumber).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Application.ScreenUpdating = True
'Close and save the workbook you copied from
wb2.Close
End Sub
Bookmarks