Click here to Skip to main content
15,905,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my workbook contains tons of data before get a fresh data from the connections i have to clear it from the existing data i got from the previous month. I have the VB code below but it will just work for a single sheet. what can i do to make it work for multiple sheets.

What I have tried:

Sub CleanTheTable()
Application.ScreenUpdating = False
Sheets("Active Roster").Select
ActiveSheet.ListObjects("ActiveRoster").HeaderRowRange.Select

If ActiveSheet.FilterMode Then
Selection.AutoFilter
End If

With Worksheets("Active Roster").ListObjects("ActiveRoster")
.Range.AutoFilter
On Error Resume Next
.DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
.DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
ActiveWindow.SmallScroll Down:=-10000

End With
Application.ScreenUpdating = True
End Sub
Posted
Updated 18-May-16 6:44am
Comments
Richard MacCutchan 18-May-16 6:35am    
Use a loop and the list of worksheets in the workbook, substituting each name in the list for each iteration of the loop.
Shilos_Dad 18-May-16 23:53pm    
thank you.
ZurdoDev 18-May-16 11:46am    
As Richard said, just look up how to loop. Simple. Or, you can hardcode all sheets names, bad idea, but can be done.

1 solution

Change your sub to be something like
VB.NET
Sub CleanTheTable(sheetNo As Integer)
    Application.ScreenUpdating = False
    Sheets(sheetNo).Select
    ActiveSheet.ListObjects("ActiveRoster").HeaderRowRange.Select
    
    If ActiveSheet.FilterMode Then
        Selection.AutoFilter
    End If
    
    Dim tbl As ListObject
    For Each tbl In Worksheets(sheetNo).ListObjects
        .Range.AutoFilter
        On Error Resume Next
        .DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.Delete
        .DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
        ActiveWindow.SmallScroll Down:=-10000
    Next
    Application.ScreenUpdating = True
End Sub

Then you can call it for all sheets similar to
VB.NET
Public Sub CleanAll()

    Dim i As Integer
    For i = 1 To Sheets.Count
        CleanTheTable i
    Next

End Sub

Caveat - I haven't fully tested this so there may be some minor typing errors
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900