Hi,
Does anyone know why the VBA code to copy all sheets from one workbook to another neglects the last sheet? I've tried the code on various workbooks in case the last sheet somehow is corrupt or named weirdly (which likely should not matter).
I've found variations of this code all over the web and from what I can tell the code below faithfully re-purposed/appropriated.
'GetTargetName' is a variable pulled when opening the workbook with the sheets I need to copy.
The 'Master' book contains one sheet which has control buttons to run various subroutines. I'm wondering if there's a correlation between the number of sheets I'm trying to copy and the number of sheets in the Master (which is just one) as the last sheet from the GetTargetName workook isn't copied. For example, when I expect 6 sheets to be copied to the Master I only get the first 5. It's always the last sheet that doesn't get copied.
Dim wb1 As Workbook
Set wb1 = Workbooks(GetTargetName)
Dim wb2 As Workbook
Set wb2 = Workbooks("Master.xlsm")
For Each Sheet In wb1.Sheets
Sheet.Copy after:=wb2.Sheets(wb2.Sheets.Count)
Next Sheet
Thanks in advance.
Matt
Does anyone know why the VBA code to copy all sheets from one workbook to another neglects the last sheet? I've tried the code on various workbooks in case the last sheet somehow is corrupt or named weirdly (which likely should not matter).
I've found variations of this code all over the web and from what I can tell the code below faithfully re-purposed/appropriated.
'GetTargetName' is a variable pulled when opening the workbook with the sheets I need to copy.
The 'Master' book contains one sheet which has control buttons to run various subroutines. I'm wondering if there's a correlation between the number of sheets I'm trying to copy and the number of sheets in the Master (which is just one) as the last sheet from the GetTargetName workook isn't copied. For example, when I expect 6 sheets to be copied to the Master I only get the first 5. It's always the last sheet that doesn't get copied.
Dim wb1 As Workbook
Set wb1 = Workbooks(GetTargetName)
Dim wb2 As Workbook
Set wb2 = Workbooks("Master.xlsm")
For Each Sheet In wb1.Sheets
Sheet.Copy after:=wb2.Sheets(wb2.Sheets.Count)
Next Sheet
Thanks in advance.
Matt