My macro report keeps saying that the "Last = "LastCol" is not defined?
Sub AppendDataAfterLastColumn()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "9HP PN" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("9HP PN").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name "9HP PN"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "9HP PN"
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If LCase(Left(sh.Name, 4)) = "1094" Then
'Find the last Column with data on the DestSh
Last = LastCol(DestSh)
'Fill in the column(s) that you want to copy
Set CopyRng = sh.Range("C5:C" & "U5:U" & "AC5:AC")
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Columns.Count > DestSh.Columns.Count Then
MsgBox "There are not enough columns in the Destsh"
GoTo ExitTheSub
End If
'This example copies values/formats and Column width
CopyRng.Copy
With DestSh.Cells(1, Last + 1)
.PasteSpecial 8 ' Column width
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Bookmarks