Macro excel hide / unhide rows selected with matches

Andrea_Ita

New Member
Joined
Aug 7, 2014
Messages
8
Hello everybody,
I need help with an excel macro.. am getting crazy!

The Macro shall hide/unhide some rows when I click a button. The problem is that the rows numbers change, as usually I delete or insert new one. I think the Macro shall look in the first column of the sheet, find "text1", then find "tex2" (that indicate the beginning and the end of the section) and hide all the rows from "text1" to "text2" included..
Can anybody help me?

Thanks
Andrea
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
This will hide the rows between "Text1" & Text2".
Code:
Sub Macro1()

    Dim FirstRow As Long
    Dim LastRow  As Long

    FirstRow = Columns("A").Find("Text1").Row
    LastRow = Columns("A").Find("Text2").Row

    Cells.EntireRow.Hidden = False
    Rows(FirstRow & ":" & LastRow).Hidden = True

End Sub
 
Upvote 0
I tried to make a button hide and unhide the rows but it didn't work..

Sub showhiderowHEM()
Dim FirstRow As Long
Dim LastRow As Long

FirstRow = Columns("B").Find("Text1").Row
LastRow = Columns("B").Find("Text2").Row

If Rows(FirstRow & ":" & LastRow).Hidden = True Then
Rows(FirstRow & ":" & LastRow).Hidden = False
Else
Rows(FirstRow & ":" & LastRow).Hidden = True
End If
End Sub
 
Upvote 0
Did you assign the macro to a button? What didn't work? Did you get any errors?

Btw, instead of your if statement you can use this line:
Code:
Rows(FirstRow & ":" & LastRow).Hidden = Not Rows(FirstRow & ":" & LastRow).Hidden
 
Upvote 0
Hi Gavin,
Yes I assined the macro to a button but it didn't work.
when I run the macro it gives me this error:

Run-time error'91'
Object variable or With block variable not set
 
Upvote 0
Are you sure the value "Text1" is in column B? If it can't find the value it would throw an error.
 
Upvote 0

Forum statistics

Threads
1,216,105
Messages
6,128,859
Members
449,472
Latest member
ebc9

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