Hello, I am trying to get the new worksheet I am adding to be placed after the last workbook.
I want to replace the "Add" in the line:
"Set Dest_Sh = ActiveWorkbook.Worksheets.Add."
with
"Add(After:=mainWB.Sheets(mainWB.Sheets.Count)).
But, it is not working for me. Any thoughts?
I want to replace the "Add" in the line:
"Set Dest_Sh = ActiveWorkbook.Worksheets.Add."
with
"Add(After:=mainWB.Sheets(mainWB.Sheets.Count)).
But, it is not working for me. Any thoughts?
Code:
Sub Consolidate()
Dim wb As ThisWorkbook
Dim sh As Worksheet
Dim Dest_Sh As Worksheet
Dim CopyRng As Range
Dim Start_Row As Integer
Dim End_Row As Integer
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "Export - Labor BOEs" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Export - Labor BOEs").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name "Export - Labor BOEs"
[U][I][B] Set Dest_Sh = ActiveWorkbook.Worksheets.Add[/B][/I][/U]
Dest_Sh.Name = "Export - Labor BOEs"
'Loop through all worksheets beginning with Labor BOE
For Each sh In ActiveWorkbook.Sheets
If Left(sh.Name, 9) = "Labor BOE" Then
End_Row = sh.Range("L" & Rows.Count).End(xlUp).Row
Start_Row = Dest_Sh.Range("L" & Rows.Count).End(xlUp).Row + 1
'Set the range that you want to copy
Set CopyRng = sh.Range("A2", "L" & End_Row)
'This example copies values/formats
CopyRng.Copy
With Dest_Sh.Range("A" & Start_Row)
.PasteSpecial 8 ' Column width
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
Next sh
ExitTheSub:
Application.GoTo Dest_Sh.Cells(1)
ActiveWindow.DisplayGridlines = False
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub