+ Reply to Thread
Results 1 to 10 of 10

Create array of worksheets by identifying first and last worksheets in array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Create array of worksheets by identifying first and last worksheets in array

    Hi everyone,

    I've never ran into this issue before. I'm trying to create an array of worksheets to run one of my macros. The worksheet names change every now and then, but they ones I need are always in-between two specific worksheets. THe workbook is structure this way:

    Worksheet(1) = "Reps Start Here"
    Worksheets(2) - Worksheets(I)
    Worksheet(end) = "Reps Stop Here"

    So, between worksheets "Reps Start Here" and "Reps Stop Here" are the worksheet names I need to create an array from. Any idea how I can do that?

    Thanks!!!

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Create array of worksheets by identifying first and last worksheets in array

    Replace the names with the real names for your sheets and see if this gives you what you want
    Sub sheetIt()
        Dim x As Integer
            For x = Sheets("Reps Start Here").Index To Sheets("Reps Stop Here").Index
                msg = msg & vbCr & Sheets(x).Name
            Next x
        MsgBox msg
    End Sub
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Create array of worksheets by identifying first and last worksheets in array

    and if you want them in an array

    Sub sheetIt()
        Dim x As Integer, Y As Integer, Z As Integer, xArray
        Y = Sheets("Reps Start Here").Index
        Z = Sheets("Reps Stop Here").Index
        ReDim xArray(Z - Y + 1)
            
            For x = Y To Z
                xArray(x - Y) = Sheets(x).Name
                msg = msg & vbCr & xArray(x - Y)
            Next x
        MsgBox msg
    End Sub

  4. #4
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Create array of worksheets by identifying first and last worksheets in array

    Hi Kev,

    Thanks very much for getting back to me. While you're code works perfectly, I've found that not all sheets between the two I identified above are ones that can be used. So I need to take a different approach. Below is the code I'm using which currently hardcode identifies the sheets to be selected:

    Sub Split_Reps()
        Dim strPath As String
        Dim myFile As String
        Dim ws As Worksheet
        Dim wbNew As Workbook
        
        Application.ScreenUpdating = False
        
        MsgBox ("Location file")
            
        myFile = Application.GetOpenFilename
        
        Workbooks.Open (myFile)
               
        ArrayOne = Sheets(Array("AAA", "BBB", "CCC", _
            "DDD", "EEE")).Select
    
        MsgBox ("Identify where to save")
        
        strPath = Application.GetSaveAsFilename
        
        Sheets(ArrayOne).Select
        
        For Each ws In ActiveWindow.SelectedSheets
            ws.Copy
            Set wbNew = ActiveWorkbook
            Workbooks("Model.xlsm").Sheets("Data").Copy after:=wbNew.Sheets(1)
            Worksheets(1).UsedRange.Value = Worksheets(1).UsedRange.Value
            Worksheets("Data").Range("A22").Value = Worksheets(1).Range("B44").Value
            BreakLinks wbNew
            wbNew.Close True, ws.Name & ".xlsx"
        Next
        
        Application.ScreenUpdating = True
    End Sub
    How can I adjust the code so that instead of hardcoding the sheet names to create ArrayOne that I can just open the workbook (myFile) and select all the sheets? The selected sheets would be stored as ArrayOne and continue on with the code.

    Any ideas?

    Thanks!!

  5. #5
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Create array of worksheets by identifying first and last worksheets in array

    Is this what you want VBA to do?
    - open the file
    - ask you to select the sheets manually
    - put those selected sheets into an array
    - continue with the code

  6. #6
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Create array of worksheets by identifying first and last worksheets in array

    Hi kev,

    Yes, locate and open the file, select the worksheets manually, run the rest of the code. I'm only familiar with creating static arrays, so if its possible to create a variable array and have the rest of the code run with it that would be great. Alternatives are very welcome!

    Thanks for your help!

  7. #7
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Create array of worksheets by identifying first and last worksheets in array

    I'm thinking VBA can provide you with a list of all sheet names with check box perhaps to select those you want
    Q Are you usually selecting more than 50% of the sheets in the workbook?

  8. #8
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Create array of worksheets by identifying first and last worksheets in array

    Hi kev,

    There should be no more than 45 worksheets or so. I can't guarantee if it'll be more or less than 50% of the workbook. The list with checkboxes would be a great idea if the list can be generated with check boxes in the workbook that has the macro. That way I can copy the selected worksheets into a worksheet as a list for some other stuff I need to do with it.

  9. #9
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Create array of worksheets by identifying first and last worksheets in array

    I worked out a solution. Thanks very much kev

  10. #10
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Create array of worksheets by identifying first and last worksheets in array

    One way
    - split your macro into 2 separate macros (A & B)
    - macroA ends by opening the other workbook and activating UserForm
    - the UserForm contains ListBox to select sheets and a CommandButton
    - clicking on CommandButton Groups the Sheets and calls macroB

    This is the code for the UserForm:
    Private Sub CommandButton1_Click()
        Dim i As Integer
            For i = 0 To ListBox1.ListCount - 1
                If ListBox1.Selected(i) Then
                    Sheets(ListBox1.List(i)).Select (False)
                End If
            Next i
        Unload UserForm1
        MsgBox "Call 2nd part of macro here"
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim sh As Worksheet
            For Each sh In Worksheets
                ListBox1.AddItem sh.Name
            Next sh
    End Sub



    Please also post your solution for everyone who reads the thread later to benefit from.
    Thanks
    Last edited by kev_; 04-06-2017 at 02:20 AM.

+ 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] VBA Create Array For New Worksheets
    By hobbiton73 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-24-2014, 09:55 AM
  2. [SOLVED] Create 1-col array identifying occurrence count in a 1-col array
    By empsall in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-26-2013, 12:50 PM
  3. [SOLVED] How to make an array of workbooks made up of an array of worksheets?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-16-2012, 08:33 AM
  4. Array of worksheets
    By nick carntowan in forum Excel General
    Replies: 4
    Last Post: 01-06-2009, 06:19 PM
  5. Two worksheets, one array
    By bob in forum Excel General
    Replies: 2
    Last Post: 06-14-2006, 06:30 PM
  6. Array to Worksheets
    By Paul W Smith in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-16-2006, 04:35 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