Can you reverse a macro?

nmbell

New Member
Joined
Oct 27, 2015
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I found a macro to "hide" rows that have a value <1 in them. However, occasionally, we need to go back and put values in those hidden rows. Is there a way to undue the macro or run a reverse of it? This is the macro I am using. I am also open to a better macro to accomplish what I want. Thank you!

Sub HideRows()
BeginRow = 1
EndRow = 420
ChkCol = 5

For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value < 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
End If
Next RowCnt
End Sub
 
Welcome to the Board!

Undoing a macro is not easy (see: Excel Tips From John Walkenbach: Undoing A VBA Subroutine).

However, I am not even sure you need Macros here at all. You should be able to use Filters to filter out (which hides) all the rows less than one. To undo it, just remove your Filter.
If you were to use VBA to do this, I would still use Filters. Loops are slow and more resource intensive and should be avoided if there are better alternatives.
If you turn on the Macro Recorder while you record yourself creating those Filters, you should be able to get most of the VBA code that you need.
 
Last edited:
Upvote 0
Sub HideRows()
BeginRow = 1
EndRow = 420
ChkCol = 5

For RowCnt = BeginRow To EndRow
'If Cells(RowCnt, ChkCol).Value < 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
'End If
Next RowCnt
End Sub
 
Upvote 0
I appreciate your quick reply!! I had originally tried to use the filter but within my table, there are merged rows that span the column that I want to filter. I think that is preventing the filter from working since the section between the column header and the first merged row filters properly but nothing below it does.. If that is true, do you know how I work around that?
 
Upvote 0
Code:
Sub Unhide()
'
' Unhide Macro
'


'
Application.ScreenUpdating = False
    Cells.Select
    Selection.EntireRow.Hidden = False
Application.ScreenUpdating = True
    Range("A1").Select


End Sub

Little simple, but you can adapt.
 
Upvote 0
I appreciate your quick reply!! I had originally tried to use the filter but within my table, there are merged rows that span the column that I want to filter. I think that is preventing the filter from working since the section between the column header and the first merged row filters properly but nothing below it does.. If that is true, do you know how I work around that?
Yes, get rid of the merged cells. They are a nightmare and causes lots of issues with things like VBA, sorting, etc. Most advanced Excel users avoid them like the plague!
You can usually easily replace them with the "Center Across Selection" formatting option, which gives you the same visual effect without all the issues.
See: http://blog.contextures.com/archives/2014/04/10/center-headings-without-merging-cells/
 
Upvote 0

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