Macros
Clear Redundant Area
VBA: Clear Redundant Area
Sub Clear_Redundant_Area()
'This macro removes the unused saved areas through the sheet or entire workbook (based on user input).
Continue = InputBox("This macro is used to delete the redundant areas at the bottom of each sheet. Would you like to perform this for the entire workbook or just the current sheet?" _
& vbNewLine & vbNewLine & "Type 'S' for the Sheet and 'W' for the workbook.", "Continue?")
If Continue = "s" Then Continue = "S"
If Continue = "w" Then Continue = "W"
If Continue <> "S" And Continue <> "W" Then Exit Sub
If Continue = "S" Then GoTo SPerform
If MidSub <> vbYes Then Call TurnStuffOff
Set Rtn = ActiveSheet
Hide = vbNo
For Each Sheet In ActiveWorkbook.Sheets
If Sheet.Visible = False Then Sheet.Visible = True: Hide = vbYes
Sheet.Activate
'Set the "Expected" max row & column by cycling through each cell and obtaining the maximum number for each variable.
MaxColE = 1
MaxRowE = 1
WConstants:
On Error GoTo -1
On Error GoTo WFormulas
For Each Cell In ActiveSheet.UsedRange.SpecialCells(xlConstants)
If Cell.Column > MaxColE Then MaxColE = Cell.Column
If Cell.Row > MaxRowE Then MaxRowE = Cell.Row
Next Cell
WFormulas:
On Error GoTo -1
On Error GoTo WLastCell
For Each Cell In ActiveSheet.UsedRange.SpecialCells(xlFormulas)
If Cell.Column > MaxColE Then MaxColE = Cell.Column
If Cell.Row > MaxRowE Then MaxRowE = Cell.Row
Next Cell
WLastCell:
'Set the "Current" max row and column by looking at the last cell in the sheet.
MaxColC = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Column
MaxRowC = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row
'Remove the difference between expectation & current areas.
If MaxColC > MaxColE And MaxColE <> MaxColC Then Range(Cells(1, MaxColE + 1), Cells(1, MaxColC)).EntireColumn.Delete
If MaxRowC > MaxRowE And MaxRowE <> MaxRowC Then Range(Cells(MaxRowE + 1, 1), Cells(MaxRowC, 1)).EntireRow.Delete
If Hide = vbYes Then Sheet.Visible = False: Hide = vbNo
Next Sheet
Rtn.Activate
ActiveWorkbook.Save
If MidSub <> vbYes Then Call TurnStuffOn
Exit Sub
SPerform:
MaxColE = 1
MaxRowE = 1
SConstants:
On Error GoTo -1
On Error GoTo SFormulas
For Each Cell In ActiveSheet.UsedRange.SpecialCells(xlConstants)
If Cell.Column > MaxColE Then MaxColE = Cell.Column
If Cell.Row > MaxRowE Then MaxRowE = Cell.Row
Next Cell
SFormulas:
On Error GoTo -1
On Error GoTo SLastCell
For Each Cell In ActiveSheet.UsedRange.SpecialCells(xlFormulas)
If Cell.Column > MaxColE Then MaxColE = Cell.Column
If Cell.Row > MaxRowE Then MaxRowE = Cell.Row
Next Cell
SLastCell:
'Set the "Current" max row and column by looking at the last cell in the sheet.
MaxColC = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Column
MaxRowC = ActiveSheet.UsedRange.SpecialCells(xlLastCell).Row
'Remove the difference between expectation & current areas.
If MaxColC > MaxColE And MaxColE <> MaxColC Then Range(Cells(1, MaxColE + 1), Cells(1, MaxColC)).EntireColumn.Delete
If MaxRowC > MaxRowE And MaxRowE <> MaxRowC Then Range(Cells(MaxRowE + 1, 1), Cells(MaxRowC, 1)).EntireRow.Delete
ActiveWorkbook.Save
If MidSub <> vbYes Then Call TurnStuffOn
End Sub