Results 1 to 4 of 4

Thread: FILL array

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,929

    FILL array

    i have this piece of code:

    Code:
    With Sourcewb
            Set TheActiveWindow = ActiveWindow
            Set TempWindow = .NewWindow
            .Sheets(Array("ReadMe", "MailSheet(s)")).Copy
        End With
    instead to fill the array with a fixed name of sheet i need to fill the array with all sheets into work book but exclude "sheet1" and "sheet7".

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: FILL array

    Moved To Office Development

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: FILL array

    Code:
    Sub Sample()
        Dim sheetNames As String, sheetCount As Long
    
        sheetCount = ThisWorkbook.Sheets.Count
    
        If sheetCount = 1 Then
            MsgBox "This workbook has only 1 Sheet"
            Exit Sub
        End If
    
        For i = 1 To sheetCount
            Select Case i
            Case 1, 7
            Case 2
                sheetNames = """" & Sheets(i).Name
            Case Else
                sheetNames = sheetNames & """" & "," & """" & Sheets(i).Name
            End Select
        Next
        
        sheetNames = sheetNames & """"
        
        '~~> Then you can change the below
        '.Sheets(Array("ReadMe", "MailSheet(s)")).Copy
        
        '~~>To
        '.Sheets(Array(sheetNames)).Copy
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: FILL array

    That's won't work because Array(sheetNames) has only one element of a combined sheet names.
    That must be an array of one or more elements with each element is a sheet name.

    Try this: (replace ThisWorkbook with Sourcewb if required.)
    Code:
    Sub CopySheets()
        Dim sh         As Object
        Dim sNameStr   As String
        Dim arShName() As String
        
        For Each sh In ThisWorkbook.Sheets
            If sh.Name <> "Sheet1" And sh.Name <> "Sheet7" Then
                sNameStr = sNameStr & "/" & sh.Name
            End If
        Next
        If sNameStr <> "" Then
            sNameStr = Mid$(sNameStr, 2) '-- remove leading "/"
            arShName = Split(sNameStr, "/") '-- build array
            ThisWorkbook.Sheets(arShName).Copy
        End If
    End Sub
    Another way to build the array:
    Code:
    Sub CopySheets2()
        Dim sh         As Object
        Dim arShName() As String
        Dim i          As Long
        
        ReDim arShName(1 To ThisWorkbook.Sheets.Count)
        i = 0
        For Each sh In ThisWorkbook.Sheets
            Select Case sh.Name
                Case "Sheet1", "Sheet7"
                Case Else
                    i = i + 1
                    arShName(i) = sh.Name
            End Select
        Next
        If i > 0 Then
            ReDim Preserve arShName(1 To i)
            ThisWorkbook.Sheets(arShName).Copy
        End If
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

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



Click Here to Expand Forum to Full Width