I have an issue that is honestly putting me at a loss.
I am copying a set of workbooks within a 'master' workbook (copying all the sheets). There are 2 methods I can use, either going through each sheet in the workbooks to copy each and every one in the master or else copy the workbook as a whole and place in the master workbook. I am using the second method using an array to discard sheets I dont need.
Dim ws() As String ' declare string array
ReDim ws(wb.Worksheets.Count) As String ' set size dynamically
Dim counter As Long ' running counter for ws array
counter = 0
For c = 1 to WS_Count
If wb.Worksheets(c).Name <> "TEST" Then
ws(counter) = wb.Worksheets(c).Name
counter = counter + 1
End If
Next
ReDim Preserve ws(counter-1) As String
wb.Worksheets(ws).Copy Before:=master.Worksheets(master.Worksheets.Count)
Both approaches I have tried work well with certain files however:
1) The first approach is problematic because it leaves a reference to the original file and so i moved to approach 2 which bypasses this issue as no reference is kept.
2) approach 2 is resulting in some sort of infinite loop in a certain file. the funny thing about this is that if i change the order in which they are merged the command doesnt get stuck and with another approx 50 workbooks the codes seems to work just fine. (please note that this issue doesn't occur with method 1 but method 1 has been discarded due to the file links)
The line it simply gets stuck on (no error) is
wb.Worksheets(ws).Copy Before:=master.Worksheets(master.Worksheets.Count)
Did anyone ever encounter this issue with a file not wanting to merge? Did i hit some limit somewhere? I'm at a loss because using 26 different workbooks, my code in its entirity managed to create a master workbook of 896 sheets. In this set that's getting stuck, i'm merging a 164 worksheet file with a new worksheet containing 164 files. I am using Office Professional Plus 2010.
I am currently hiding alerts, however I believe i was getting on of those messages where it asks me if i want to wait for the application.
Anyone can point me in the right direction please?
ws
array has one more element than wb.Worksheets.Count. I prefer to do something like: ReDim ws(1 to wb.Worksheets.Count), currently it's the same as ReDim ws(0 to wb.Worksheets.Count). Also, you should be able to get rid ofCounter
, since, likec
, it's just incrementing by 1. – Doug Glancydictionary.Keys()
ordictionary.Values()
both return an array :) But yes, the # of sheets seems the likely problem here. – David Zemens