VBA to Hide/Unhide Rows

Alm5423

New Member
Joined
Sep 22, 2021
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Hi Everyone,

I'm currently building a form where I have a number of approval lines at the bottom of a spreadsheet. In most cases, rows 60-63 need to be hidden from view but there are a select number of circumstances where I need to unhide them.

For example, I need to write a code that allows me to unhide rows 60-61 if cell N27 on my spreadsheet is filled in....but then I also need to unhide rows 60-63 if any of the following conditions are met:

Cell B36=True
Cell B46=True
Cell N27="CE"
Cell E28 ="Hrly" And Cell N28 "Ex"

In all other cases not mentioned above, rows 60-63 should remain hidden. Does anyone know how to write a code for something like that? I started to write down some lines below but couldn't figure out how to correctly integrate the N27 piece. I'm fairly new to the VBA syntax.

If Range("N27").Value <> "" Then
Range("60:61").EntireRow.Hidden = False
ElseIf Range("B36").Value = True Or Range("B46").Value = True Or Range("N27").Value = "CE" Or Range("E28").Value = "Hrly" And Range("N28").Value = "Ex" Then
Range("60:63").EntireRow.Hidden = False
Else: Range("60:63").EntireRow.Hidden = True
 
Don't blame me, blame the original Poster. :rolleyes:

And I don't think the first line would stop it from getting to the second line, Just the last 'Or' in the second line would never be executed. Right?

The second line will execute if the first line, Range("N27").Value = "", Unless I have had too many spirits.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
With the original code like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim celltxt As String
Dim celltxt2 As String
celltxt = ActiveSheet.Range("E33").Text
celltxt2 = ActiveSheet.Range("N33").Text
If InStr(1, celltxt, "Expat", vbTextCompare) Then
Rows("34:34").EntireRow.Hidden = False
ElseIf InStr(1, celltxt2, "Expat", vbTextCompare) Then
Rows("34:34").EntireRow.Hidden = False
Else: Rows("34:34").EntireRow.Hidden = True
End If

The script will run any time any value on the sheet is change Unless I have had too many spirits.

There is nothing here restricting it to a certain range
 
Upvote 0
Give this a try:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    Dim celltxt As String, celltxt2 As String
    
    ' XXX Calculations won't trigger the change event so the intersect
    ' XXX only needs to include the input cells that are the drivers
    If Intersect(Target, Range("E33, N33, B36, B46,N27,E28,N28")) Then
        
        celltxt = ActiveSheet.Range("E33").Text
        celltxt2 = ActiveSheet.Range("N33").Text
        If InStr(1, celltxt, "Expat", vbTextCompare) Then
            Rows("34:34").EntireRow.Hidden = False
        ElseIf InStr(1, celltxt2, "Expat", vbTextCompare) Then
            Rows("34:34").EntireRow.Hidden = False
        Else
            Rows("34:34").EntireRow.Hidden = True
        End If
        
        Range("60:63").EntireRow.Hidden = True
        
        If Range("B36").Value = True Or _
                Range("B46").Value = True Or _
                Range("N27").Value = "CE" Or _
                (Range("E28").Value = "Hrly" And Range("N28").Value = "EX") Then
            
            Range("60:63").EntireRow.Hidden = False
        ElseIf Range("N27").Value <> "" Then
            Range("60:61").EntireRow.Hidden = False
        End If
    
    End If
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub
 
Upvote 0
My first IF Intersect is incorrect please replace it wit this:-
VBA Code:
    If Not Intersect(Target, Range("E33,N33,B36,B46,N27,E28,N28")) Is Nothing Then
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

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