+ Reply to Thread
Results 1 to 6 of 6

Using an array in another array

Hybrid View

  1. #1
    Registered User
    Join Date
    08-26-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    7

    Using an array in another array

    Hi All

    I have a largish database which is updated monthly from various sources and which is contains details of members of 9 groups. After the update I then filter and produce 9 worksheets on a new workbook, the sheet names being 2 letter codes. I then use this code

    Dim WB As Workbook, Wb2 As Workbook, Path As String
        Path = "C:Group"
        Dim GroupA() As String, i As Integer
        
                        Application.DisplayAlerts = False
                        Application.ScreenUpdating = False
                        
        GroupA() = Split("AZ|BQ|CF|DD|HK|PY|ST|VG|WW", "|", -1, vbBinaryCompare)
        
       Set WB = ActiveWorkbook
     For i = 0 To UBound(GroupA)
      
             WB.Sheets(GroupA(i)).Copy
                 Set Wb2 = ActiveWorkbook
                  Wb2.SaveAs Filename:=Path & GroupA(i)
                    Wb2.Close
      Next i
    WB.Close
    What I would like to be able to do is to save the new workbooks, not named by the 2 letter code, but by the full name.

    I am sure this can be done using a second array but just can't figure it out.

    Any assistance welcome

    Robert

  2. #2
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Using an array in another array

    Hi Robert,

    Try this code in place of what you have above:
    Dim WB As Workbook, Wb2 As Workbook, Path As String
    Dim GroupA() As String, i As Integer
    Dim GroupB() As String
    
    Path = "C:Group"
    
    GroupB() = Split("Person1|Person2|...|Person9", "|", -1, vbBinaryCompare)
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    GroupA() = Split("AZ|BQ|CF|DD|HK|PY|ST|VG|WW", "|", -1, vbBinaryCompare)
    
    Set WB = ActiveWorkbook
    For i = 0 To UBound(GroupA)
        
        WB.Sheets(GroupA(i)).Copy
        Set Wb2 = ActiveWorkbook
        Wb2.SaveAs Filename:=Path & GroupB(i)
        Wb2.Close
    Next i
    
    WB.Close
    NB: I have not tested this code as I don't have your workbook, thus making it a bit difficult to guarantee that it will work.

    However if this doesn't work, it won't be far off, so please don't hesitate to let me know and I'll work with you until it's resolved. This may also mean I will ask you to upload your workbook so that I can test a solution before suggesting it here.

    Hope this helps

  3. #3
    Registered User
    Join Date
    08-26-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using an array in another array

    Thanks for that that is so much more simple and elegant than the tortuous loops I had got myself into. There does seem to be one minor problem in that it works perfectly until the last element of the array when I get an error message indicating that the path is incorrect. I'll have a closer look at it later and am sure the problem is with me and my element of the code.

    Thanks again

    Robert

  4. #4
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Using an array in another array

    No problems!

    I wouldn't mind taking a look for you if you wouldn't mind uploading the workbook(s) required to make this macro run to error.

    Thanks

  5. #5
    Registered User
    Join Date
    08-26-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using an array in another array

    Thanks again. As I suspected the problem lay with me; I had managed to put || between the penultimate and last settings for GroupB, which I discovered by checking the locals window which showed limits of (0 to 9) which led me to my error.

    Everything now perfect and thanks for your interest

    Robert

  6. #6
    Valued Forum Contributor
    Join Date
    02-08-2012
    Location
    Newcastle, Australia
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    1,429

    Re: Using an array in another array

    No worries, happy to help!

    Please don't forget to mark this thread as solved and please click on the * next to my post to say thanks

+ 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. Loop new messages containing a table, populate a dynamic array, paste array to Excel
    By laripa in forum Outlook Programming / VBA / Macros
    Replies: 1
    Last Post: 05-19-2013, 07:20 AM
  2. [SOLVED] Quick Array question - Copy array to another array then resize?
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-02-2013, 01:17 AM
  3. [SOLVED] Populate Listbox with all rows of a dynamic array where elements of a single array match.
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-07-2012, 04:54 AM
  4. Replies: 2
    Last Post: 02-23-2011, 07:24 AM
  5. Single Conditional Array x two Multi-Column Array - Approach needed
    By David Brown in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-28-2010, 11:41 AM

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