Question

I have several worksheets containing monthly data and would like to create a summary worksheet that includes data from all of the worksheets. How can I code VBA to count the number of worksheets and create a table in a new worksheet whose rows are limited to the worksheet count? In addition, the names of the worksheets would then populate the first column of the table.

For example, I have the following worksheets: January, February, March and April. The macro recognises that I only have 4 worksheets and creates a table on a new worksheet that has this amount of rows. The first column in each row is populated with, respectively, January, February, March and April.

Given that the worksheet differ in both count and name each month, I am not sure how to capture this.

Was it helpful?

Solution

try this code:

 Sub test()
    Dim summarySh As Worksheet
    Dim sh As Worksheet
    Dim i As Integer

    'add new sheet at the end'
    Set summarySh = Worksheets.Add(After:=Worksheets(Worksheets.Count))
    'rename it'
    summarySh.Name = "Summary"

    i = 1
    'loop through all sheets'
    For Each sh In Worksheets
        'if sh is not summary sheet, then'
        If sh.Name <> summarySh.Name Then
            'write sheet name in summary sheet column A'
            summarySh.Range("A" & i) = sh.Name
            i = i + 1
        End If
    Next
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top