I have tried customising a Ron de Braun code for my needs but I’m obviously missed something!!
Trying to produce a new sheet "CF_Nozz_Combined" that is populated from sheets "CFList_Nozz(with1)", "CFList_Nozz(with2)" etc
All data in "CFList_Nozz(with1) etc is;
Headers in A1:H1, data in A2:H2 on all sheets., but unknown # of rows
Results I’m getting at moment are:-
The existing sheet "CF_Nozz_Combined" is deleted and a new one produced, but is blank
This is my adapted code,
Trying to produce a new sheet "CF_Nozz_Combined" that is populated from sheets "CFList_Nozz(with1)", "CFList_Nozz(with2)" etc
All data in "CFList_Nozz(with1) etc is;
Headers in A1:H1, data in A2:H2 on all sheets., but unknown # of rows
Results I’m getting at moment are:-
The existing sheet "CF_Nozz_Combined" is deleted and a new one produced, but is blank
This is my adapted code,
VBA Code:
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
_________________________________________________________________________
Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'To Delete the sheet "CF_Nozz_Combined" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("CF_Nozz_Combined").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a NEW worksheet with the name "CF_Nozz_Combined"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "CF_Nozz_Combined"
StartRow = 2
'looping through worksheets that start "CFList_Nozz " and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If LCase(Left(sh.Name, 10)) = "CFList_Nozz" Then
'Find the last row with data on the DestSh and sh
Last = LastRow(DestSh)
shLast = LastRow(sh)
'If sh is not empty and if the last row >= StartRow copy the CopyRng
If shLast > 0 And shLast >= StartRow Then
‘Set Range want to copy
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
‘Copy values, Set to Copy values/formats,
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
'This will copy the sheet name in the J column
DestSh.Cells(Last + 1, "J").Resize(CopyRng.Rows.Count).Value = sh.Name
End If
Next
ExitTheSub:
'All the rest of stuff required
Application.Goto DestSh.Cells(1)
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub