Can someone make this macro work for the entire worksheet

afierros

New Member
Joined
Sep 15, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Macro below - thanks for the help in advance.

Sub deleteHiddenColumnsRows()



Dim answer As Integer



answer = MsgBox("Ya sure? Hopefully you didn't hit this on accident", vbYesNo + vbQuestion, "Do eet")



If answer = vbYes Then



For lp = 256 To 1 Step -1
If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else
Next
For lp = 65536 To 1 Step -1
If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else
Next



Else
'nothing
End If



End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This might be quite slow.
If you don't have hidden columns or rows beyond where you have data ie outside of the used range, we could limit the loop to run just on the used range which might make it a little quicker.

VBA Code:
Sub deleteHiddenColumnsRows()

Dim answer As Integer
Dim lp As Long

answer = MsgBox("Ya sure? Hopefully you didn't hit this on accident", vbYesNo + vbQuestion, "Do eet")

If answer = vbYes Then
    
    For lp = ActiveSheet.Cells.Columns.Count To 1 Step -1
        If Columns(lp).EntireColumn.Hidden = True Then Columns(lp).EntireColumn.Delete Else
    Next
    
    For lp = ActiveSheet.Cells.Rows.Count To 1 Step -1
        If Rows(lp).EntireRow.Hidden = True Then Rows(lp).EntireRow.Delete Else
    Next

Else
    'nothing
End If

End Sub
 
Upvote 0
Another option, although I quite like @Alex Blakenburg 's suggestion as well (y)
VBA Code:
Option Explicit
Sub deleteHiddenColumnsRows()
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")                           '<-- *** Change to actual sheet name ***
    Dim answer As Integer, i As Long, c As Range, r As Range
    Dim LRow As Long, LCol As Long
    
    answer = MsgBox("Ya sure? Hopefully you didn't hit this on accident", vbYesNo + vbQuestion, "Do eet")
    If answer = vbYes Then
        LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
        LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
        
        With ws
            For i = 1 To LCol
                If .Columns(i).Hidden = True Then
                    If c Is Nothing Then Set c = .Columns(i) Else Set c = Union(c, .Columns(i))
                End If
            Next i
            If Not c Is Nothing Then c.EntireColumn.Delete
            For i = 1 To LRow
                If Rows(i).Hidden = True Then
                    If r Is Nothing Then Set r = Rows(i) Else Set c = Union(r, Rows(i))
                End If
            Next i
            If Not r Is Nothing Then r.EntireRow.Delete
        End With
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,221
Messages
6,123,699
Members
449,117
Latest member
Aaagu

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