Hide multiple rows on sheet based on cell value - excel

I’m trying, with Excel 2013, to hide and unhide rows when a cell is a certain value.
It's a form which should expand based on answers given.
When C16 = YES hide rows 18:22
When C16 = NO hide rows 24:38
When C16 =blank hide rows 18:38
When L43 = YES unhide rows 43:68 (if it’s not yes a zero is displayed)
I have tried 2 methods.
First: Into the worksheet - selected change in the top right dropdown
Private Sub Worksheet_Change(ByVal Target As Range)
Range("A18:A22").EntireRow.Hidden = (Range("$C$16").Value = "Yes")
Range("A24:A38").EntireRow.Hidden = (Range("$C$16").Value = "NO")
Range("A18:A38").EntireRow.Hidden = (Range("$C$16").Value = "")
Range("A43:A68").EntireRow.Hidden = (Range("$L$43").Value = "0")
End Sub
Second: code from here:
Unhide rows based on cell value
Using both of these methods only one of the changes seems to go ahead. So cell C16 is changed but that means range L43 is ignored
Also when the cell was blank it didn’t change anything. It remained as is and didn’t hide the columns as required.

Your ranges overlap so even if C16 = "Yes" the line C16 = "" will override it and unhide it. L42 is probably also a number where as you're comparing it to a text value try using the following instead. Your code would run on every single change in your sheet as well so have also updated it to only run when either C16 or L43 is changed
Private Sub Worksheet_Change(ByVal Target As Range)
With Me
If Not Intersect(Target, Union(.Range("C16"), .Range("L43"))) Is Nothing Then
.Range("A18:A38").EntireRow.Hidden = False
Select Case LCase(.Range("C16").Value2)
Case "yes"
.Range("A18:A22").EntireRow.Hidden = True
Case "no"
.Range("A24:A38").EntireRow.Hidden = True
Case Else
.Range("A18:A38").EntireRow.Hidden = True
End Select
.Range("A43:A68").EntireRow.Hidden = False
Select Case LCase(.Range("L43").Value2)
Case "yes"
.Range("A43:A68").EntireRow.Hidden = False
Case Else
.Range("A43:A68").EntireRow.Hidden = True
End Select
End If
End With
End Sub
After comments
I'd break this into two from your comments. The first would watch the dropdown and execute on the change of that cell. The second would update using the calculate event. Put these Subs in the sheets where applicable
Private Sub Worksheet_Change(ByVal Target As Range)
With Me
If Not Intersect(Target, Union(.Range("C16"), .Range("L43"))) Is Nothing Then
.Range("A18:A38").EntireRow.Hidden = False
Select Case LCase(.Range("C16").Value2)
Case "yes"
.Range("A18:A22").EntireRow.Hidden = True
Case "no"
.Range("A24:A38").EntireRow.Hidden = True
Case Else
.Range("A18:A38").EntireRow.Hidden = True
End Select
End If
End With
End Sub
Private Sub Worksheet_Calculate()
Application.EnableEvents = False
With Me
.Range("A43:A68").EntireRow.Hidden = False
Select Case LCase(.Range("L43").Value2)
Case "yes"
.Range("A43:A68").EntireRow.Hidden = False
Case Else
.Range("A43:A68").EntireRow.Hidden = True
End Select
End With
Application.EnableEvents = True
End Sub

Try:
With Worksheets("Sheet1")
.Rows("18:68").EntireRow.Hidden = False
opt = UCase(.Range("C16").Value)
Select Case opt
Case "YES"
Rows("18:22").EntireRow.Hidden = True
Case "NO"
Rows("24:38").EntireRow.Hidden = True
Case ""
Rows("18:38").EntireRow.Hidden = True
Case Else
MsgBox "Invalid option in cell C16."
End Select
If UCase(.Range("L43").Value) = "Yes" Then
Rows("43:68").EntireRow.Hidden = True
Else
MsgBox "Invalid option in cell L43."
End Select
End With
...though I didn't understand what you wanted to be '0'.

Related

Hiding all rows below cells containing certain text

I am looking to hide all rows (until row 150), that contain a certain text in a certain column. The column contains a drop down of two choices, "Yes" and "No". If the answer is yes, I want to hide all the rows below, if it is no, then not hide.
ie, C2 is "No", don't hide. C3 is "Yes", hide...There are 150 rows of Yes or no, but once yes, you can hide all the rows.
I've tried to cycle the code below and it works with the first cell, but all rows after that do not work
Option Explicit
Private Sub HideRows(ByVal Target As Range)
If Range("C2").Value = "Yes - provide details" Then
Rows("3:150").EntireRow.Hidden = True
ElseIf Range("C2").Value = "No" Then
Rows("3:150").EntireRow.Hidden = False
End If
If Range("C3").Value = "Yes - provide details" Then
Rows("4:150").EntireRow.Hidden = True
ElseIf Range("C3").Value = "No" Then
Rows("4:150").EntireRow.Hidden = False
End If
' all the way through to C149
If Range("C149").Value = "Yes - provide details" Then
Rows("150").EntireRow.Hidden = True
ElseIf Range("C149").Value = "No" Then
Rows("150").EntireRow.Hidden = False
End If
End Sub
I expected to be able to cycle through the first If code, but it doesn't work after the 1st set of them
(this is untested)
Rows("3:150").EntireRow.Hidden = False
For i = 2 to 150
If Range("C" & i).Value = "Yes - provide details" Then
Rows(i + 1 & ":150").EntireRow.Hidden = True
Exit For
End If
Next i
Since it looks like you don’t want to filter because you want all rows below a “yes” to be hidden. I would recommend doing a loop.
Option Explicit
Sub HideRows()
Dim currRow as Integer: currRow = 1
Dim continue as Boolean: continue = True
While continue
If cells(currRow,3) = "Yes - provide details" then
rows(currRow & ":150").EntireRow.Hidden = true
continue = False
Else
currRow = currRow + 1
End If
Wend
End Sub
This is untested from mobile.

ActiveX Command Button that unhides next to a Cell if a value is entered, and hides if the cell is empty

I have 80 rows where the user can enter a predetermined value under column Ward. This unhides a button next to it. Upon clicking it, it empties the adjacent value and increments (+1) a particular cell in another sheet depending on the original value.
Currently, I have 80 ActiveX buttons next to the Ward cells that hides/unhides depending on the value of the Ward cells. I've noticed that adding more buttons slows down the spreadsheet because of the sheer volume of If Then statements I have.
If Range("F8").Value = 0 Then
Sheets("Admissions").EDAdmit1.Visible = False
Else
Sheets("Admissions").EDAdmit1.Visible = True
End If
If Range("L8").Value = 0 Then
Sheets("Admissions").ElecAdmit1.Visible = False
Else
Sheets("Admissions").ElecAdmit1.Visible = True
End If
If Range("F9").Value = 0 Then
Sheets("Admissions").EDAdmit2.Visible = False
Else
Sheets("Admissions").EDAdmit2.Visible = True
End If
If Range("L9").Value = 0 Then
Sheets("Admissions").ElecAdmit2.Visible = False
Else
Sheets("Admissions").ElecAdmit2.Visible = True
End If
.. and so on.
Not to mention the If Then statements I have for every button click.
Private Sub EDAdmit1_Click()
If Range("F8") = "ICU" Then
Worksheets("Overview").Range("AD11").Value = Worksheets("Overview").Range("AD11") + 1
ElseIf Range("F8") = "HDU" Then
Worksheets("Overview").Range("AF11").Value = Worksheets("Overview").Range("AF11") + 1
ElseIf Range("F8") = "DPU" Or Range("F8") = "Other" Then
Else
Col = WorksheetFunction.VLookup(Range("F8"), Range("U1:V27"), 2)
Worksheets("Overview").Range(Col).Value = Worksheets("Overview").Range(Col).Value + 1
End If
Range("F8").ClearContents
End Sub
Is there a more efficient way of doing this?
Admission List:
You could consider using "admit" hyperlinks in the cells next to the Ward selections: that way you only need one handler (Worksheet_FollowHyperlink in the worksheet module). Note you need to use Insert >> Hyperlink and not the HYPERLINK() formula-type links here (because formula-based links don't trigger the FollowHyperlink event).
You can ditch the hide/show code and instead use conditional formatting to change the link font color to hide the links when there's no Ward selected. If a user clicks on one of the hidden links then you can just do nothing.
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim rngSrc As Range, addr, ward
Set rngSrc = Target.Range '<< the cell with the link
ward = rngSrc.Offset(0, 1).Value '<< cell with Ward
'only do anything if a ward is selected
If Len(ward) > 0 Then
'find the cell to update
Select Case ward
Case "ICU"
addr = "AD11"
Case "HDU"
addr = "AF11"
Case "DPU", "Other"
addr = ""
Case Else
addr = Application.VLookup(ward, Me.Range("U1:V27"), 2, False)
End Select
'if we have a cell to update then
If Len(addr) > 0 Then
With Worksheets("Overview").Range(addr)
.Value = .Value + 1
End With
End If
rngSrc.Offset(0, 1).ClearContents
End If
rngSrc.Select '<< select the clicked-on link cell
' (in case the link points elsewhere)
End Sub
At the beginning of your code put this line:
Application.ScreenUpdating = False
this will disable all screen updates. Let your code do changes, and then enable screen updating, and all your changes will appear.
Application.ScreenUpdating = True
Disabling screen updating usually makes the execution of code faster.

Rearrange equations

For instance a simple equation is
F = m * a
And I have in my excel sheet
So in here I have not entered m
Would it be possible to rearrange the equation to calculate for m
m = F / a
Does excel have a built in facility to do this?
Can VBA do this?
Any other way to achieve this incase the above 2 are not possible?
So basically If I leave any one of the variable cells blank and fill in the other two, I would like the result for the 3rd variable.
Put this in the worksheet's private code sheet (right-click worksheet name tab, View Code).
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B2:B4")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
If Application.Count(Range("B2:B4")) = 2 Then
If IsEmpty(Range("B2")) Then
Range("B2") = Range("B3").Value2 * Range("B4").Value2
ElseIf IsEmpty(Range("B3")) Then
Range("B3") = Range("B2").Value2 / Range("B4").Value2
Else
Range("B4") = Range("B2").Value2 / Range("B3").Value2
End If
Range("B2").NumberFormat = "0 \N"
Range("B3").NumberFormat = "0.0 \k\g"
Range("B4").NumberFormat = "0 \m\/\s\²"
End If
End If
safe_exit:
Application.EnableEvents = True
End Sub
You can do this with a if() statement:
=if(b3="",b2/B4,if(b2="",B3*b4,if(b4="",b2/b3,"Check")))
Not put any error checking such as checking for numbers in any two cells etc...

Excel Macro VBA: Double click to tick issue

how can I make the double click works ONLY when column 1,2,3 and 4 have values? I don't know where I should insert the code.. it's something like if column 1,2,3 and 4 have values then doubleclick.enable = TRUE else doubleclick.enable= FALSE..
Kindly need advice. My code is as follows:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Select Case Target.Column
Case 6, 13
If Not Intersect(Target, Range("F2:F13, M2:M13")) Is Nothing Then Cancel = True
Target.Font.Name = "Times New Roman"
If Target = "" Then
Target = ChrW(&H2713)
Else
MsgBox "You cannot modify the cell."
End If
End Select
End Sub
I am asking just to clarify the issue.
You only want to call doubleclick procedure if any cell in column 1 to 4 have value
Or you want to check if corresponding row has value?
for option 1, you may use
If Application.CountA(Range("A1:D" & Rows.Count)) > 0 Then
'Your Code
End If
for second option
If Application.CountA(Range("A" & target.Row & ":D" & target.Row)) > 0 Then
'Your Code
End If
I tested above codes but in case of any mistake, we can modify them

How to Hide and Unhine rows using dollar figures using VBA

My apologies if my question is too basic. I am trying to achieve the following results
If Cell E11 value is less than 25,000, then hide Rows 14 & 15.
If Cell E11 value is between 25k-50k, then hide only row 15 only.
If Cell E11 value is between 50k-75k, then unhide both rows.
And it is possible to make the calculation automated?
So far I found the following code, which of course isn't helping with my situation.
Sub PG1()
If Range("E11").Value = "Pass" Then
Rows("14:14").EntireRow.Hidden = True
ElseIf Range("E11").Value = "Fail" Then
Rows("14:14").EntireRow.Hidden = False
End If
End Sub
A minor amount of trial and error based on nothing else but the code you posted gave me this code, which should get you started. It completes two of the three requirements (using different cells and rows), but it works. If it's still not something you can use to complete your task, you should probably hire someone to do this for you.
Sub ShowOrHide()
If ActiveSheet.Range("A1").Value < 25000 Then
ActiveSheet.Rows("2:3").EntireRow.Hidden = True
ElseIf ActiveSheet.Range("A1").Value >= 50000 Then
ActiveSheet.Rows("2:3").EntireRow.Hidden = False
End If
End Sub
Here you go.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$11" Then
If Target.Value < 25000 Then Rows("13:15").EntireRow.Hidden = True
If Target.Value > 25000 Then Rows("13:14").EntireRow.Hidden = False
If Target.Value > 50000 Then Rows("13:15").EntireRow.Hidden = False
If Target.Value > 75000 Then Rows("13:15").EntireRow.Hidden = True
End If
End Sub

Resources