I have a program that takes all the sheets in an excel workbook and creates a pivot table on a new sheet that uses all the data. I would like to modify it so that the user can input which worksheets to use, and interpret the input. Here are my ideas for how it would work.
Input: 1, 3-5, Accounting - Marketing
Possible Middle Step: {1, 3, 4, 5, "Accounting", ... , "Marketing"}
Interpretation: {Worksheets(1), Worksheets(3), Worksheets(4), Worksheets(5), Worksheets("Accounting"), ..., Worksheets("Marketing")}
Input: "All"
Interpretation: {Worksheets(1), ... , Worksheets(Worksheets.Count)}
Issues:
1. Numbers need to be interpreted as integers and strings as strings
2. Spaces should be ignored. Ie. "1,3-5,Accouting-Marketing" = "1, 3 - 5, Accouting - Marketing"
3. The size of the array containg the selected worksheets will be variable.
4. Handling odd user input. Ie. Overlapping ranges, misspelled worksheet names, reversed ranges (5-3), or numbers out of range.
Here is what I've started. I'm actually not sure where to proceed from here. I was thinking of searching the string wsheets(i) for the two numbers and using them for a loop to add all the intervening numbers into an array, but I'm not sure how to deal with the above issues.
Edit: I managed to make a fair bit of progress. I think I've solved issues 1 and 2. I still need to solve issues 3 and 4, and I'm not sure if the way I've done this is the best way or not.
wsheetlist = InputBox("Enter the worksheets you want to include in the pivot table")
wsheets = Split(Replace(wsheetlist, " ", ""), ",")
For i = 0 To UBound(wsheets)
If wsheets(i) Like "*-*" Then
s1 = Left(wsheets(i), InStr(1, wsheets(i), "-") - 1)
s2 = Mid(wsheets(i), InStr(1, wsheets(i), "-") + 1)
If IsNumeric(s1) Then x = CInt(s1) Else: x = Worksheets(s1).Index
If IsNumeric(s2) Then y = CInt(s2) Else: y = Worksheets(s2).Index
For j = 0 To Abs(y - x)
sheetarray(j + k) = WorksheetFunction.Min(x, y) + j
Next j
k = k + j - 1
ElseIf wsheets(i) = "All" Then
For j = 0 To UBound(sheetarray)
sheetarray(j) = j
Next j
i = UBound(wsheets) + 1
Else: If IsNumeric(wsheets(i)) Then sheetarray(k) = CInt(wsheets(i)) Else: wsheets(i) = Worksheets(wsheets(i)).Index
End If
k = k + 1
Next i
I would post a sample with the full code, but the rest of my code isn't bug-free, and since this will be used to build a query, all I really need is strings of worksheet identifiers to put into the query.
Thank you everyone,
K64
Bookmarks