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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
So do you want the script to run automatically as you change a particular cell value.
Or run when you press a button to run the script?
 
Upvote 0
I'd like it to run automatically when the cell value is changed. Right now, I have it set it up as a Private Sub Worksheet_Change(ByVal Target As Range) (which might be incorrect).

But the second piece of the code (starting with ElseIf) was working fine until I added the N27 piece.
 
Upvote 0
I'd like it to run automatically when the cell value is changed. Right now, I have it set it up as a Private Sub Worksheet_Change(ByVal Target As Range) (which might be incorrect).

But the second piece of the code (starting with ElseIf) was working fine until I added the N27 piece.
Show me the entire code
 
Upvote 0
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


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
End If
End Sub

The first section doesn't apply to my question, it's just the second part.
 
Upvote 0
I do not understand this:
The first section doesn't apply to my question, it's just the second part.

Is the first part a part of the script?
If not why do you have it here?

To write the entire script I need to know all of what you want.
Do not assume since one part works and another part does not work that I do not need to know all of what you need.

The way your showing the first part of the script will run when you change any cell on the entire sheet.
 
Upvote 0
Yes, it's part of the script, it's just addressing another issue on the form that I didn't mention above.
 
Upvote 0
To many if and or statements here:
This is beyond my knowledgebase.
I will continue to monitor this thread to see what I can learn.
 
Upvote 0
To many if and or statements here:
This is beyond my knowledgebase.
I will continue to monitor this thread to see what I can learn.

I think this should equate to same thing:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'
    Dim celltxt As String, celltxt2 As String
'
    celltxt = ActiveSheet.Range("E33").Text
    celltxt2 = ActiveSheet.Range("N33").Text
'
'---------------------------------------------------------------------------------------------------
'
    If InStr(1, celltxt, "Expat", vbTextCompare) Or InStr(1, celltxt2, "Expat", vbTextCompare) Then
        Rows("34:34").EntireRow.Hidden = False
    Else
        Rows("34:34").EntireRow.Hidden = True
    End If
'
'---------------------------------------------------------------------------------------------------
'
    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" Then
        Range("60:63").EntireRow.Hidden = False
    ElseIf Range("E28").Value = "Hrly" And Range("N28").Value = "EX" Then
        Range("60:63").EntireRow.Hidden = False
    Else
        Range("60:63").EntireRow.Hidden = True
    End If
End Sub
 
Upvote 0
@johnnyL, the first line is going to stop it from getting to the 2nd line
If Range("N27").Value <> ""
Elseif ..... Range("N27").Value = "CE"
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,420
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