Need a VBA Macro to hide/ unhide rows based on a string in a cell value

vipin30sep

New Member
Joined
Feb 14, 2019
Messages
20
I have a column (A) that has below values in different cells (A1, A2 and A3 and so on).

PPC Revenue - 2015
Non-PPC Revenue - 2015
ROAS - <Enter PPC 1 Name> - 2015
ROAS - <Enter PPC 1 Name> - 2017
Non-PPC Revenue - 2016
Non-PPC Revenue - 2020

I need a macro that can be used to hide/ unhide rows based on the condition - If the value is 2015 it should hide the rows that have 2015 in it (along with other text), so basically, using the macro i should be able to hide all 3 rows (above) as it has 2015 in it. Please assist. Appreciate your help!

 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this:
Code:
Sub Hide_Rows()
'Modified 7/17/2019 8:38:16 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = Lastrow To 1 Step -1
If InStr(Cells(i, 1).Value, "2015") Then Rows(i).Hidden = True
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Now if your saying you want to toggle them from hidden to not hidden use this script.

Code:
Sub Hide_Rows()
'Modified  7/17/2019  8:49:01 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = Lastrow To 1 Step -1
If InStr(Cells(i, 1).Value, "2015") Then Rows(i).Hidden = Not Rows(i).Hidden
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
If you want to choose what year then use this script:
Code:
Sub Hide_Rows()
'Modified  7/17/2019  9:42:18 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim ans As String
ans = InputBox("Enter Year", "Enter Year like this 2015", Format(Date, "YYYY"))
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = Lastrow To 1 Step -1
If InStr(Cells(i, 1).Value, ans) Then Rows(i).Hidden = Not Rows(i).Hidden
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Awesome! It works. Appreciate your help.

If you want to choose what year then use this script:
Code:
Sub Hide_Rows()
'Modified  7/17/2019  9:42:18 PM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim Lastrow As Long
Dim ans As String
ans = InputBox("Enter Year", "Enter Year like this 2015", Format(Date, "YYYY"))
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For i = Lastrow To 1 Step -1
If InStr(Cells(i, 1).Value, ans) Then Rows(i).Hidden = Not Rows(i).Hidden
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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