+ Reply to Thread
Results 1 to 10 of 10

How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Report

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-20-2012
    Location
    India
    MS-Off Ver
    Office 365
    Posts
    454

    How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Report

    Hello,

    I am keenly looking to learn few things and tweak the below macro.

    1. How to add new worksheet "Report"? If it already exist then do nothing

    2. How to Loop through all the Sheets Except the Sheet "Report"

    3. Suggest any other tweaks If I missed to learn

    "For i = 1 To Sheets.Count - 1" This line seems to be incorrect. If I Move the "Report" Sheet from Last to First then The macro becomes messy.

    
    Private Sub Workbook_Open()
    'Declare nextblank row
    Dim Nextblankrow As Long, i As Long
    
    'CleareContents of Report Sheet
    Sheets("Report").Cells.ClearContents
    
    'Loop Through Sheets & Copy Data to reports Sheet
    For i = 1 To Sheets.Count - 1
    'Identify Lastrow
    Nextblankrow = Sheets("Report").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
    
    Sheets(i).Range("A2:A11").Copy
    Sheets("Report").Cells(Nextblankrow, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
    
    Next i
    
    End Sub
    Thank you.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,621

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    Private Sub Workbook_Open()
    'Declare nextblank row
    Dim i As Long
    
    If Not Evaluate("isref(Report!A1)") Then Sheets.Add.Name = "Report"
    
    'CleareContents of Report Sheet
    Sheets("Report").Cells.ClearContents
    
    'Loop Through Sheets & Copy Data to reports Sheet
    For i = 1 To Sheets.Count
        If Not Sheets(i).Name = "Report" Then
            Sheets("Report").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(10).Value = Sheets(i).Range("A2:A11").Value
        End If
    Next i
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    06-20-2012
    Location
    India
    MS-Off Ver
    Office 365
    Posts
    454

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    Thank you very much

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,621

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    You're welcome and thanks for the rep.

  5. #5
    Forum Contributor
    Join Date
    06-20-2012
    Location
    India
    MS-Off Ver
    Office 365
    Posts
    454

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    Hello,

    How to modify the code If I don't know the Exact ranges to be copied in Each Sheet? or If the Data in Each Sheet is Dynamic? Can I Use the Property CurrentRegion? Or UsedRange?

    Thank you

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,621

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    Please post example file of how a sheet looks like (desensitize data if needed)
    It's always data in column A that needs to copied no matter the length of data ?

  7. #7
    Forum Contributor
    Join Date
    06-20-2012
    Location
    India
    MS-Off Ver
    Office 365
    Posts
    454

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    No.It's not always Data in Column A that needs to be copied. But, the Starting cell is always "A2"

    What I mean to say is for Example

    Sheet1 - Data is in Range ("A2:C5") dynamic range which may change next day to ("A2:G25")
    Sheet2 - Data is in Range ("A2:F16") dynamic range too
    Sheet3 - Data is in Range ("A2:L25") same

    and So on

    So, If the data in Each sheet is Dynamic then how do I copy the Entire Data from all the Sheets(1,2,3,4,5, etc.,) to the Sheet named "Report". I want to Identify the Dynamic Range in Each Sheet and copy the same to "Report" Sheet.

    The Below line is copying only Ten Cells In Column"A"

    Sheets(i).Range("A2:A11").Value
    Thank you

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,621

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    Try this. I assume you have headers in Row 1 ?
    Private Sub Workbook_Open()
    'Declare nextblank row
    Dim i As Long, nRows As Long, nColumns As Long
    
    If Not Evaluate("isref(Report!A1)") Then Sheets.Add.Name = "Report"
    
    'CleareContents of Report Sheet
    Sheets("Report").Cells.ClearContents
    
    'Loop Through Sheets & Copy Data to reports Sheet
    For i = 1 To Sheets.Count
        If Not Sheets(i).Name = "Report" Then
            With Sheets(i)
                nRows = .Cells(1).CurrentRegion.Rows.Count
                nColumns = .Cells(1).CurrentRegion.Columns.Count
                Sheets("Report").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(nRows - 1, _
                        nColumns).Value = .Range("A2").Resize(nRows - 1, nColumns).Value
            End With
        End If
    Next i
    
    End Sub

  9. #9
    Forum Contributor
    Join Date
    06-20-2012
    Location
    India
    MS-Off Ver
    Office 365
    Posts
    454

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    Thank you Sir.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,621

    Re: How to Add New Worksheet named "Report" and run loop through all the Sheets Except "Re

    No probs.

+ 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. Display "(Multiple Items)" in the Pivot Table's Report Filter
    By ljhyun89s in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 07-11-2017, 09:43 AM
  2. Replies: 5
    Last Post: 12-27-2016, 04:24 PM
  3. Replies: 5
    Last Post: 05-07-2016, 02:16 PM
  4. Replies: 10
    Last Post: 12-31-2014, 11:04 AM
  5. VBA change pivot "report filter" from userform textbox or combobox
    By mmor79 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-11-2014, 12:57 PM
  6. Replies: 2
    Last Post: 06-06-2013, 12:45 PM
  7. Filter report in pivot table with "greater than" and "less than"
    By gygabyte017 in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 12-29-2012, 08:08 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