+ Reply to Thread
Results 1 to 5 of 5

Sheets(Array).Copy Dynamically

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-17-2006
    Posts
    152

    Sheets(Array).Copy Dynamically

    Hi all,

    i've hit a bit of a mental wall (been programming too many hours striaght) and cannot figure out how to do the following.

    I have a sheet array sh_array(3)
    of which all the slots may not contain data, but i want to turn it into the statement

    Sheets(Array("sheet1", "sheet2", etc, etc).Copy

    however as all the items in the sh_array may not be occupied i cannot simply do this

    Sheets(sh_array(0), sh_array(1), sh_array(2), sh_array(3)).Copy

    as it hits the subscription out of range if one of the items is a null.

    so say only sh_array() slots 0 and 1 contain data

    how do i dynamically turn it into the statement

    Sheets(sh_array(0), sh_array(1)).Copy

    as so forth if more/less elements of the sh_array are full.

    I thought about a if statement testing the sh_array length but there must be a better way to do it.

    Thanks

    Jon

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480
    Why not create a new temporary array with only valid sheet names?
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    11-17-2006
    Posts
    152
    i could but as i want the sheets selected and copied together how would i produce the statement even if i knew the exact array size. this is the dynamic statement i'm after:

    Sheets(New_Array(0), ..... , to New_Array(i)).Copy

    without running a loop statement? (which would only copy the sheets individually anyway.

    for i = 0 to New_Array.Length
    
    Sheets(New_Array(i)).copy
    
    Next i

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480
    Something like this. which simply selects sheet1 and sheet3 in the active workbook.

    Sub x()
    
        Dim sh_array(2)
        Dim shtTemp As Object
        Dim sh_NewArray()
        
        sh_array(0) = "Sheet1"
        sh_array(1) = ""
        sh_array(2) = "Sheet3"
    
        sh_NewArray = CompressArray(sh_array)
        
        Sheets(sh_NewArray).Select
        For Each shtTemp In ActiveWindow.SelectedSheets
            MsgBox shtTemp.Name
        Next
        
    End Sub
    
    
    Function CompressArray(Old()) As Variant()
        Dim lngIndex As Long
        Dim lngNitems As Long
        Dim vntTemp()
        
        For lngIndex = LBound(Old) To UBound(Old)
            If Len(Old(lngIndex)) > 0 Then
                ReDim Preserve vntTemp(lngNitems)
                vntTemp(lngNitems) = Old(lngIndex)
                lngNitems = lngNitems + 1
            End If
        Next
        CompressArray = vntTemp
        
    End Function

  5. #5
    Forum Contributor
    Join Date
    11-17-2006
    Posts
    152
    thanks for that

    its a good idea, i'll have a play about with it

    regards,

    jon

+ 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] Combination of Sum and Indirect function between sheets done dynamically
    By all4excel in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-17-2007, 10:03 AM
  2. dynamically linking multiple identical structured files
    By perinouk in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-12-2007, 08:09 AM
  3. Combining data dynamically
    By AMK4 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-12-2007, 12:20 PM
  4. Dynamically Variable Ranges
    By pinny in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 01-01-2007, 07:02 PM
  5. Dynamically create reports
    By LAF in forum Excel General
    Replies: 1
    Last Post: 11-03-2006, 04:53 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