+ Reply to Thread
Results 1 to 8 of 8

How to store sheetnames in an array and copy from array to summary sheet?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-30-2018
    Location
    Sweden
    MS-Off Ver
    2016.
    Posts
    397

    How to store sheetnames in an array and copy from array to summary sheet?

    Hi, I am trying to create a summary worksheet with data from 26 separate tabs.

    I want to exclude the header in the 26 data sheets and copy from sheet1 to summary sheet, from sheet2 to summary sheet etc without the header.


    
    Sub CopySummary()
    Dim arr As Variant
    
    Dim ws As Worksheet
    Set ws = Sheets("Summary")
    
        If ws Is Nothing Then
            Exit Sub
        End If
    
      Dim LastRow As Long, LastRow2 As Long, LastRow3 As Long, LastRow4 As Long
            
      Dim sht As Worksheet, sht2 As Worksheet, sht3 As Worksheet
      Set sht = Sheets("Town1")
      Set sht2 = Sheets("Summary")
      Set sht3 = Sheets("Town2")
               
      LastRow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row
      LastRow2 = sht2.Cells(sht2.Rows.Count, "B").End(xlUp).Row
      LastRow3 = sht3.Cells(sht3.Rows.Count, "B").End(xlUp).Row
         
      Sheets("Town1").Range("A2:V" & LastRow).Copy Destination:=Sheets("Summary").Range("A2:V" & LastRow2)
       
      LastRow4 = sht2.Cells(sht2.Rows.Count, "A").End(xlUp).Row
          
      Sheets("Town2").Range("A2:V" & LastRow3).Copy Destination:=Sheets("Summary").Range("A" & LastRow4 & ":V" & LastRow4)
    
    End Sub
    All suggestions are welcome.

    1. Store the 26 sheet names in an array
    2. Loop through the array and copy items from sheet1 to summary sheet, sheet2 to summary etc
    3. Apply advanced filter on the summary

  2. #2
    Forum Contributor
    Join Date
    06-30-2018
    Location
    Sweden
    MS-Off Ver
    2016.
    Posts
    397

    Re: How to store sheetnames in an array and copy from array to summary sheet?

    I guess 1. is

    Dim arr As Variant
    arr = Sheets(Array("Town1", "Town2", "Town3"))
    And 2. could be something like?

    Dim i As Integer
    
    For i = 1 To UBound(arr)
    
    ' some code here
    
    Next i

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,870

    Re: How to store sheetnames in an array and copy from array to summary sheet?

    How about
    Sub waimea()
       Dim Ary As Variant
       Dim i As Long
       
       Ary = Array("Sheet1", "Sheet2", "sheet3")
       For i = 0 To UBound(Ary)
          With Sheets(Ary(i))
             .Range("A2:V" & .Range("A" & Rows.Count).End(xlUp).Row).Copy Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Offset(1)
          End With
       Next i
    End Sub

  4. #4
    Forum Contributor
    Join Date
    06-30-2018
    Location
    Sweden
    MS-Off Ver
    2016.
    Posts
    397

    Re: How to store sheetnames in an array and copy from array to summary sheet?

    Hi Fluff,

    thank you for your reply and your code, it works great but the "format" of the values is not copied?

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,870

    Re: How to store sheetnames in an array and copy from array to summary sheet?

    What do you mean the formats are not copied?
    Using copy destination copies everything.

  6. #6
    Forum Contributor
    Join Date
    06-30-2018
    Location
    Sweden
    MS-Off Ver
    2016.
    Posts
    397

    Re: How to store sheetnames in an array and copy from array to summary sheet?

    Hi Fluff,

    I have a date in column B and after the copy it shows as "43862"?

  7. #7
    Forum Contributor
    Join Date
    06-30-2018
    Location
    Sweden
    MS-Off Ver
    2016.
    Posts
    397

    Re: How to store sheetnames in an array and copy from array to summary sheet?

    Nevermind, I was wrong, I had used "remove formats".

    My bad!

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,870

    Re: How to store sheetnames in an array and copy from array to summary sheet?

    Glad to help & thanks for the feedback.

+ 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] Store sheets into array via loop to copy to new book
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 09-18-2017, 09:28 AM
  2. [SOLVED] store data in memory array and then paste to sheet all at once
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-15-2015, 03:07 PM
  3. [SOLVED] Dynamic Changing Data, Based on Criteria Store in Array, Sum array and store in Cell
    By penbeacho in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-28-2015, 10:31 AM
  4. Replies: 3
    Last Post: 10-05-2014, 11:48 AM
  5. Store array formula content in a VBA array
    By cgkmal in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-19-2013, 02:24 PM
  6. VBA store sheetnames in a array
    By oeldere in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-18-2013, 03:39 PM
  7. [SOLVED] How Do You Take Data Off A Sheet and Store It In An Array
    By Raleigh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2006, 04:50 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