+ Reply to Thread
Results 1 to 1 of 1

Use Worksheets Specified by User Input (Like Print Dialogue)

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Use Worksheets Specified by User Input (Like Print Dialogue)

    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
    Last edited by k64; 05-16-2014 at 04:15 PM. Reason: Update Code

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Print individual page by user input
    By RonNCmale in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-27-2012, 04:14 PM
  2. Sort and print with user input
    By ru469 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 12-20-2009, 02:25 PM
  3. Combine cells for user input/print friendly output
    By rus in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 09-25-2009, 03:44 PM
  4. Capture user input into xldialog print
    By vpanang in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-27-2006, 04:45 PM
  5. [SOLVED] set up a pause in a print macro for user input
    By Scott53 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-07-2005, 03:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1