Hide rows in VBA based on cell values

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
442
Office Version
  1. 365
Platform
  1. Windows
Guys,

Is this at all possible in VBA based on worksheet called 'Sales'

if F6 = VW and I6 = Bristol, hide rows 64 to 65
if F6 = VW and I6 = Bath, hide rows 68 to 70 and 81 to 85
if F6 = VW and I6 is empty, hide rows 64 to 65 and 89 to 90
if F6 is empty and I6 is empty, show rows 64 to 100
Else show all rows
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
This should work
Code:
Sub hide_rows()


If Range("F6") = "VW" And Range("I6") = "Bristol" Then


    Rows("64:65").Hidden = True
    




ElseIf Range("F6") = "VW" And Range("I6") = "Bath" Then


    Rows("68:70").Hidden = True
    Rows("81:85").Hidden = True




ElseIf Range("F6") = "VW" And Range("I6") = "" Then


    Rows("64:65").Hidden = True
    Rows("89:90").Hidden = True




ElseIf Range("F6") = "" And Range("I6") = "" Then


    Rows("64:90").Hidden = True
    
Else: Rows("64:90").Hidden = False


End If


End Sub
 
Upvote 0
Thanks for this, I have changed this to run based on workbook change. This works great but both cells I6 and F6 are validation drop downs and I have noticed the marco will only run once I click off the cells. Is there away around this to make this run as soon as the validation box is changed? Hope this makes sense, code is below:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


If Range("F6") = "VW" And Range("I6") = "Bristol" Then


    Rows("100:120").Hidden = True
    
ElseIf Range("F6") = "VW" And Range("I6") = "Bath" Then




    Rows("121:140").Hidden = True
    Rows("142:142").Hidden = True




ElseIf Range("F6") = "VW" And Range("I6") = "" Then


    Rows("143:143").Hidden = True
    Rows("146:146").Hidden = True


ElseIf Range("F6") = "" And Range("I6") = "" Then


    Rows("170:180").Hidden = True
    
Else: Rows("100:181").Hidden = False


End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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