When you start this macro and there is nothing to do it ruins the sheet

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
379
Office Version
  1. 365
Platform
  1. Windows
Good morning,

I got this code for erasing hidden lines with works great, but when you do not have hidden lines and still start this macro it sort of ruins your sheet by adding a column and that is not what I want. Is there a way to prevent this from happening like and message for example?

Thank you for your time.


VBA Code:
Application.EnableEvents = False
  Application.ScreenUpdating = False
  With ActiveSheet.ListObjects(1)
    .ListColumns.Add Position:=2
    On Error Resume Next
    .ListColumns(2).DataBodyRange.SpecialCells(xlVisible).Value = 1
    On Error GoTo 0
    If .Parent.FilterMode Then .Parent.ShowAllData
    .Sort.SortFields.Clear
    .Sort.SortFields.Add2 Key:=.ListColumns(2).DataBodyRange, SortOn:=xlSortOnValues, Order:=xlAscending
    .Sort.Apply
    On Error Resume Next
    .ListColumns(2).DataBodyRange.SpecialCells(xlBlanks).EntireRow.Delete
    On Error GoTo 0
    .ListColumns(2).Delete
  End With
  Application.EnableEvents = False
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try adding these red lines
Rich (BB code):
Application.EnableEvents = False
  Application.ScreenUpdating = False
  With ActiveSheet.ListObjects(1)
    If .Range.Columns(1).SpecialCells(xlVisible).Count < .Range.Rows.Count Then
      .ListColumns.Add Position:=2
      On Error Resume Next
      .ListColumns(2).DataBodyRange.SpecialCells(xlVisible).Value = 1
      On Error GoTo 0
      If .Parent.FilterMode Then .Parent.ShowAllData
      .Sort.SortFields.Clear
      .Sort.SortFields.Add2 Key:=.ListColumns(2).DataBodyRange, SortOn:=xlSortOnValues, Order:=xlAscending
      .Sort.Apply
      On Error Resume Next
      .ListColumns(2).DataBodyRange.SpecialCells(xlBlanks).EntireRow.Delete
      On Error GoTo 0
      .ListColumns(2).Delete
    End If
  End With
  Application.EnableEvents = False

.. but shouldn't that last line be
Rich (BB code):
Application.EnableEvents = True
 
Upvote 0
Solution
Thank you that solved the problem and yes you are right about the EnableEvents that should indeed be TRUE.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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