Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro to copy sheets into workbook stops after one sheet

Tags:

excel

vba

I have a workbook where to speed computation (long story) I created a macro to copy out three of the sheets to another file and then another macro to copy them back it.

The macro to copy out works fine, however the macro to copy back in halts after copying in one sheet.

I searched within StackOverflow and found some similar questions but couldn't find an answer which worked. One post thought it was related to Office versions and one to a Shift key issue.

Here is the code:

Application.Calculation = xlCalculateManual
Application.ScreenUpdating = False
Application.DisplayAlerts = True
Application.EnableEvents = False

'
'   Set up the  workbooks
'
Set ThisWkb = ThisWorkbook
Fname = Application.GetOpenFilename( _
    fileFilter:="Excel Macro Files, *.xlsm", _
    Title:="Select the Storage File", _
    MultiSelect:=False)
Set StorageWbk = Workbooks.Open(Fname)
'   
 MsgBox ("Beginning process - please click ok to any macro warning - you will see a confirmation when complete")


StorageWbk.Sheets("Sh A").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
StorageWbk.Sheets("Sh B").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
StorageWbk.Sheets("Sh C").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)

StorageWbk.Close

I sometimes find that if I then delete the new sheet and run the macro again it sometimes works and reads all three sheets in, but it also sometimes doesn't.

Any help is greatly appreciated.

like image 483
Boomer Avatar asked Oct 29 '22 15:10

Boomer


1 Answers

Quoting both YowE3K and nbayly to give the correct answer from the comments directly here (I had to look for it):

Unhide your hidden Sheets and copy the three links at once using:

StorageWbk.Sheets(Array("Sh A", "Sh B", "Sh C")).Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
like image 160
Pierre44 Avatar answered Nov 15 '22 06:11

Pierre44