+ Reply to Thread
Results 1 to 20 of 20

Loop through multiple sheets to add a row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    260

    Loop through multiple sheets to add a row

    Hello,

    I have below code to add row. My data is in table format.

    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim tbl As ListObject
    Set tbl = ws.ListObjects("Sales_Table")
    tbl.ListRows.Add 1
    Whereas I have multiple sheets in table format. I need to add rows in every sheet.

    Above code just adds the row in Active sheet and not in the other sheets. There are other sheets as well, like, Table1, Table 2, Table3 and so on.

    Please assist. Thanks!

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Loop through multiple sheets to add a row

    Sub J3v16()
    Dim ws As Worksheet
    For Each ws In Sheets
        ws.ListObjects(1).ListRows.Add
    Next ws
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    260

    Re: Loop through multiple sheets to add a row

    Does not seems to be working for me.

    I have multiple sheets. For instance, just assume, I just want to add the rows in Table1, Table3 and Table4

  4. #4
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    260

    Re: Loop through multiple sheets to add a row

    Attaching excel for reference

    Each day, I add a new row at row 5 and in A5 cell, yesterday's date is added (excluding weekends)

    I need to automatically add a new row and date (as stated above), in Table1, Table3 and Table4.
    Attached Files Attached Files

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Loop through multiple sheets to add a row

    You mentioned nothing about adding dates...only add a new row to existing table in all sheets...
    This sample above has only one sheet...?
    Go back to the drawing board and once you know what you want then come back to the thread

  6. #6
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    308

    Re: Loop through multiple sheets to add a row

    Are there specific sheet names with tables? Which sheet is Table2 on and so on? Your example has but 1 sheet and 1 table.

  7. #7
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    260

    Loop through multiple sheets to add a row

    Hello,

    I have below code to add row. My data is in table format.

    Each day, I add a new row at row 5 and in A5 cell, yesterday's date is added (excluding weekends)

    I need to automatically add a new row and date (as stated above), in Table1, Table3 and Table4.

    
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim tbl As ListObject
    Set tbl = ws.ListObjects("Sales_Table")
    tbl.ListRows.Add 1
    Whereas I have multiple sheets in table format. I need to add rows in every sheet.

    Above code just adds the row in Active sheet and not in the other sheets. There are other sheets as well, like, Table1, Table 2, Table3 and so on.

    Please assist. Thanks!
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    260

    Re: Loop through multiple sheets to add a row

    Above link is been closed due to duplication.

    Can advise on this thread

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Loop through multiple sheets to add a row

    Post 6 does just that...adds a new row to table in every sheet...
    What has row 5 or cells 5 got to do with this..Please be more transparent...
    Last edited by Sintek; 07-09-2024 at 06:07 AM.

  10. #10
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    260

    Re: Loop through multiple sheets to add a row

    I do not need to add rows in every sheet. I've attached a new excel for reference. Only in this 3 sheets the rows should be added

    The code that you provided, it inserts rows at the end of the last row.
    Attached Files Attached Files
    Last edited by E5254730; 07-09-2024 at 06:14 AM.

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Loop through multiple sheets to add a row

    I give up...Post 6 does what you want...

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    47,997

    Re: Loop through multiple sheets to add a row

    @Sintek: your code adds a row at the bottom of each Table. The OP want to insert a new row at the top of each Table (line 1).

    Option Explicit
    
    Sub Test()
    
    Dim ws As Worksheet
    Dim tbl As ListObject
    
    For Each ws In ThisWorkbook.Worksheets
        Set tbl = ws.ListObjects(1)
        tbl.ListRows.Add 1
    Next ws
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    47,997

    Re: Loop through multiple sheets to add a row

    @E5254730: I don't think your Month to Date Averages are including the newly inserted rows. Year to Date looks OK.

  14. #14
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Loop through multiple sheets to add a row

    Oops misunderstood...Tx Trevor...

  15. #15
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    47,997

    Re: Loop through multiple sheets to add a row

    No worries. I don't often play with Tables in VBA so a learn for me.

  16. #16
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,955

    Re: Loop through multiple sheets to add a row

    Each day, I add a new row at row 5 and in A5 cell, yesterday's date is added
    Edit to Post2 code and nor Post6 as previously referenced in error...

    Sub J3v16()
    Dim ws As Worksheet, Nxt As ListRow
    For Each ws In Sheets
        With ws.ListObjects(1)
            Set Nxt = .ListRows.Add(1)
            Nxt.Range(1) = Date
        End With
    Next ws
    End Sub
    Last edited by Sintek; 07-09-2024 at 11:59 AM.

  17. #17
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    260

    Re: Loop through multiple sheets to add a row

    Yes, this did the trick.

    But when it comes to date, it adds today's date, whereas i want to have previous date (excluding weekends). Today it will add Jul 9th

  18. #18
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    47,997

    Re: Loop through multiple sheets to add a row

    Change it to: Nxt.Range(1) = Date - 1

  19. #19
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    260

    Re: Loop through multiple sheets to add a row

    @TMS - thanks - I just added the code. It works.

    Much Appreciated.

  20. #20
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    47,997

    Re: Loop through multiple sheets to add a row

    You're welcome.


    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon (Next to Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ 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. VBA Loop to combine multiple zip codes to multiple items from 2 sheets onto 1
    By CBlauv in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-20-2023, 07:45 PM
  2. Excel Formula to Loop through multiple sheets
    By mchilapur in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-20-2017, 01:39 AM
  3. Loop add value to multiple sheets
    By Petter120 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-12-2015, 10:03 AM
  4. Loop and Clear Multiple Sheets
    By ptmuldoon in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-29-2014, 08:26 PM
  5. [SOLVED] Loop codes through multiple sheets (not all sheets in workbook)
    By hcyeap in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-18-2014, 02:11 AM
  6. Macro to loop through multiple sheets
    By excellearner121 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-05-2013, 02:41 AM
  7. Loop with AdvancedFilter Using Multiple Sheets
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-14-2012, 01:10 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