How to Delete Everything except tables in every worksheet?

chaftun

New Member
Joined
Jul 5, 2022
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
Hello everyone,

I use this macro to delete all the data except for tables in my workbook. the macro deletes only the first worksheet. I need it run through worksheets. what change do i need to make to make it work ?
Thanks

VBA Code:
Option Explicit

Sub deleteExceptTable()


         Dim WS_Count As Integer
         Dim I As Integer

      
         WS_Count = ActiveWorkbook.Worksheets.Count

        
         For I = 1 To WS_Count

    Dim urg As Range: Set urg = ActiveSheet.UsedRange
    Dim trg As Range: Set trg = ActiveSheet.ListObjects(1).Range

    Dim drg As Range
    Dim lSize As Long
    Dim cCount As Long
    
    ' Left
    lSize = trg.Column - urg.Column
    If lSize > 0 Then
        Set drg = urg.Columns(1).Resize(, lSize)
    End If
    ' Right
    cCount = urg.Column + urg.Columns.Count - trg.Column - trg.Columns.Count
    If cCount > 0 Then
        Set drg = CombinedRange(drg, _
            urg.Columns(lSize + trg.Columns.Count + 1).Resize(, cCount))
    End If
    
    Dim rCount As Long
    
    ' Top
    rCount = trg.Row - urg.Row
    If rCount > 0 Then
        Set drg = CombinedRange(drg, _
            Cells(urg.Row, trg.Column).Resize(rCount, trg.Columns.Count))
    End If
    ' Bottom
    rCount = urg.Row + urg.Rows.Count - trg.Row - trg.Rows.Count
    If rCount > 0 Then
        Set drg = CombinedRange(drg, Cells(trg.Row + trg.Rows.Count, _
            trg.Column).Resize(rCount, trg.Columns.Count))
    End If
    
    If Not drg Is Nothing Then
        drg.delete
    End If
 Next I

End Sub


Function CombinedRange( _
    ByVal BuiltRange As Range, _
    ByVal AddRange As Range) _
As Range
    If BuiltRange Is Nothing Then
        Set CombinedRange = AddRange
    Else
        Set CombinedRange = Union(BuiltRange, AddRange)
    End If
End Function
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Add the indicated line and see if that works for you...

VBA Code:
Sub deleteExceptTable()

    Dim WS_Count As Integer
    Dim I As Integer

    WS_Count = ActiveWorkbook.Worksheets.Count
    For I = 1 To WS_Count
    Worksheets(I).Activate   '******* ADD THIS LINE ****
    Dim urg As Range: Set urg = ActiveSheet.UsedRange
    Dim trg As Range: Set trg = ActiveSheet.ListObjects(1).Range

    Dim drg As Range
    Dim lSize As Long
    Dim cCount As Long
   
    ' Left
    lSize = trg.Column - urg.Column
    If lSize > 0 Then
        Set drg = urg.Columns(1).Resize(, lSize)
    End If
    ' Right
    cCount = urg.Column + urg.Columns.Count - trg.Column - trg.Columns.Count
    If cCount > 0 Then
        Set drg = CombinedRange(drg, _
            urg.Columns(lSize + trg.Columns.Count + 1).Resize(, cCount))
    End If
   
    Dim rCount As Long
   
    ' Top
    rCount = trg.Row - urg.Row
    If rCount > 0 Then
        Set drg = CombinedRange(drg, _
            Cells(urg.Row, trg.Column).Resize(rCount, trg.Columns.Count))
    End If
    ' Bottom
    rCount = urg.Row + urg.Rows.Count - trg.Row - trg.Rows.Count
    If rCount > 0 Then
        Set drg = CombinedRange(drg, Cells(trg.Row + trg.Rows.Count, _
            trg.Column).Resize(rCount, trg.Columns.Count))
    End If
   
    If Not drg Is Nothing Then
        drg.Delete
    End If
 Next I

End Sub


Function CombinedRange( _
    ByVal BuiltRange As Range, _
    ByVal AddRange As Range) _
As Range
    If BuiltRange Is Nothing Then
        Set CombinedRange = AddRange
    Else
        Set CombinedRange = Union(BuiltRange, AddRange)
    End If
End Function
 
Upvote 0
Add the indicated line and see if that works for you...

VBA Code:
Sub deleteExceptTable()

    Dim WS_Count As Integer
    Dim I As Integer

    WS_Count = ActiveWorkbook.Worksheets.Count
    For I = 1 To WS_Count
    Worksheets(I).Activate   '******* ADD THIS LINE ****
    Dim urg As Range: Set urg = ActiveSheet.UsedRange
    Dim trg As Range: Set trg = ActiveSheet.ListObjects(1).Range

    Dim drg As Range
    Dim lSize As Long
    Dim cCount As Long
  
    ' Left
    lSize = trg.Column - urg.Column
    If lSize > 0 Then
        Set drg = urg.Columns(1).Resize(, lSize)
    End If
    ' Right
    cCount = urg.Column + urg.Columns.Count - trg.Column - trg.Columns.Count
    If cCount > 0 Then
        Set drg = CombinedRange(drg, _
            urg.Columns(lSize + trg.Columns.Count + 1).Resize(, cCount))
    End If
  
    Dim rCount As Long
  
    ' Top
    rCount = trg.Row - urg.Row
    If rCount > 0 Then
        Set drg = CombinedRange(drg, _
            Cells(urg.Row, trg.Column).Resize(rCount, trg.Columns.Count))
    End If
    ' Bottom
    rCount = urg.Row + urg.Rows.Count - trg.Row - trg.Rows.Count
    If rCount > 0 Then
        Set drg = CombinedRange(drg, Cells(trg.Row + trg.Rows.Count, _
            trg.Column).Resize(rCount, trg.Columns.Count))
    End If
  
    If Not drg Is Nothing Then
        drg.Delete
    End If
 Next I

End Sub


Function CombinedRange( _
    ByVal BuiltRange As Range, _
    ByVal AddRange As Range) _
As Range
    If BuiltRange Is Nothing Then
        Set CombinedRange = AddRange
    Else
        Set CombinedRange = Union(BuiltRange, AddRange)
    End If
End Function
Thank you for your reply but i'm sorry it didn't work. it works only for the first worksheet.
 
Upvote 0
If you add this line in right below the line that I told you to add in Post #2, does the Worksheet Name change when the Message Box pops up... It should change with every loop...

VBA Code:
MsgBox "Worksheet Name " & Worksheet.Name & " Is Now Active!"
 
Upvote 0
Also, do you only have one table on every sheet?
 
Upvote 0
Also, I was just playing with your code and it does work on more than one sheet. It moves the table to a new location on the given sheet, but I am sure you are aware of that, and it does not handle a worksheet with more than one table (or no tables) but other than that it does run through the worksheets unless it fails from one of the two conditions I mentioned.
 
Upvote 0
Also, I was just playing with your code and it does work on more than one sheet. It moves the table to a new location on the given sheet, but I am sure you are aware of that, and it does not handle a worksheet with more than one table (or no tables) but other than that it does run through the worksheets unless it fails from one of the two conditions I mentioned.
Hello Igold thank you for your help, i finally found a solution by calling the procedure "deleteExceptTable" in another procedure I'll share the code with you. this code works great. I really appreciate your help
VBA Code:
Option Explicit

Sub deleteExceptTable()
    
    Dim urg As Range: Set urg = ActiveSheet.UsedRange
    Dim trg As Range: Set trg = ActiveSheet.ListObjects(1).Range

    Dim drg As Range
    Dim lSize As Long
    Dim cCount As Long
    
    ' Left
    lSize = trg.Column - urg.Column
    If lSize > 0 Then
        Set drg = urg.Columns(1).Resize(, lSize)
    End If
    ' Right
    cCount = urg.Column + urg.Columns.Count - trg.Column - trg.Columns.Count
    If cCount > 0 Then
        Set drg = CombinedRange(drg, _
            urg.Columns(lSize + trg.Columns.Count + 1).Resize(, cCount))
    End If
    
    Dim rCount As Long
    
    ' Top
    rCount = trg.Row - urg.Row
    If rCount > 0 Then
        Set drg = CombinedRange(drg, _
            Cells(urg.Row, trg.Column).Resize(rCount, trg.Columns.Count))
    End If
    ' Bottom
    rCount = urg.Row + urg.Rows.Count - trg.Row - trg.Rows.Count
    If rCount > 0 Then
        Set drg = CombinedRange(drg, Cells(trg.Row + trg.Rows.Count, _
            trg.Column).Resize(rCount, trg.Columns.Count))
    End If
    
    If Not drg Is Nothing Then
        drg.Clear
    End If
 

End Sub

Function CombinedRange( _
    ByVal BuiltRange As Range, _
    ByVal AddRange As Range) _
As Range
    If BuiltRange Is Nothing Then
        Set CombinedRange = AddRange
    Else
        Set CombinedRange = Union(BuiltRange, AddRange)
    End If
End Function


VBA Code:
Sub Delete()
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
WS.Activate
deleteExceptTable
Next WS
End Sub
 
Upvote 0
I am glad you got it sorted out, that is the important part. I was happy to help although I don't know how I did.
At any rate, thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,061
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top