How to hide/unhide rows based on cell value, multiple times in 1 sheet - hide

When the value in B16 is "0", I need to hide some other rows, and also un-hide them when the value is "1".
But, here's my issue:
Also, in the same sheet:
If the value in H4 is "0", I need to hide some rows. When the value is "1", I need to un-hide these rows.
The first part is working:
Private Sub Worksheet_Change(ByVal Target As Range)
With Sheets("Checklist")
If Range("b16").Value = 0 Then rows("17:21").EntireRow.Hidden = True
If Range("b16").Value >= 1 Then rows("17:21").EntireRow.Hidden = False
End With
End Sub
And here's where I understand I suck at VB
In other words, the code below is clearly not working. Can you please provide a working alternative?
Private Sub Worksheet_Change(ByVal Target As Range)
With Sheets("Checklist")
Select Case Taget.Address
Case "$b$16"
If IsNumeric(Target.Value) And Target.Value = 0 Then
rows("17:21").EntireRow.Hidden = True
End If
Case "$b$16"
If IsNumeric(Target.Value) And Target.Value = 0 Then
rows("17:21").EntireRow.Hidden = False
End If
Case "$h$4"
If IsNumeric(Target.Value) And Target.Value = 1 Then
rows("23:26").EntireRow.Hidden = True
End If
Case "$h$4"
If IsNumeric(Target.Value) And Target.Value = 1 Then
rows("23:26").EntireRow.Hidden = False
End If
End Select
End Sub
Thanks.

Related

Excel VBA: multiple Individual Exclusive Checkboxes with data validation (create a Macro with selecting cells to format them) (code is here)

I'm trying to introduce Checkboxes into my personal project Planning, unfortunately normal Checkboxes tend to bug out, so I found this side here and am trying to convert it into a macro to select the rows I want checks at. Specifically the last one that is NOT "Mutually Exclusive" but with data validation.
http://www.vbaexpress.com/kb/getarticle.php?kb_id=879
Unfortunately it does not let me make it into a macro like I wanted to and I spent a lot of time trying already. :(
Please Help
I tried to write a SelectionRng. Or searched for a way to write it into a Macro to select it in the Worksheet.
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Limit Target count to 1
If Target.Count > 1 Then Exit Sub
'Isolate Target to a specific range
If Intersect(Target, Range("Ckboxes")) Is Nothing Then Exit Sub
'Set Target font to "marlett"
Target.Font.Name = "marlett"
'Check value of target
If Target.Value <> "a" Then
Target.Value = "a" 'Sets target Value = "a"
Target.Interior.ColorIndex = 44
Cancel = True
Exit Sub
End If
If Target.Value = "a" Then
Target.ClearContents 'Sets target Value = ""
Target.Interior.ColorIndex = 0
Cancel = True
Exit Sub
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'Limit Target count to 1
If Target.Count > 1 Then Exit Sub
'Isolate Target to a specific range
If Intersect(Target, Range("Ckboxes")) Is Nothing Then Exit Sub
'Select a specific subset of the range "Ckboxes"
Select Case Target.Address
Case Else
'Populate the cell to the right of Target with its status
If Target.Value = "a" Then
Target.Offset(0, 6) = "Checked"
Else:
Target.Offset(0, 6).Value = "Not Checked"
End If
End Select
End Sub

Automatically adding timestamp to each column

so i have a sheet and whenever something in the range of A10:A23 is changed/updated, it is supposed to put a timestamp in the according column in Row B, however it isnt working and i have no idea why.
I already set the sheets code to "Worksheet" and "change"
Here is my code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = A And Target.Column >= 10 And Target.Column <= 23 Then
Cells(Target.Column, B) = Now()
End If
End Sub
Thanks in advance!
Assuming you will only change one cell at a time (which is not always true):
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A10:A23")) Is Nothing Then
If Target.Count = 1 Then
Application.EnableEvents = False
Target.Offset(0, 1) = Now()
Application.EnableEvents = True
End If
End If
End Sub

Change columns value based on another cell

I want to update 2 columns values based on another columns value (if value change). Suppose I have column A with a list (AA1, AA2, AA3), column B with a list (BB1, BB2), column C with a list (CC1, CC2). If a choose a value "AA1" from column A then Column B value should change to BB2 et column C to CC1. But nothing should happen if the value chosen in column A is different from "AA1". The same process occurs also for value "BB1" in column B. I added a vba but it not working. Also is there another way to do it without running a vba code ? Thanks
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changedCells As Range
Set changedCells = Range("A:C")
If Not Application.Intersect(changedCells, Range(Target.Address)) Is Nothing Then
If Target.Count > 1 Then Exit Sub
If Target.Column = 1 And LCase(Target.Value) = "aa1"Then
Cells(Target.Row, 2) = "BB2"
Cells(Target.Row, 3) = "CC1"
ElseIf Target.Column = 2 And LCase(Target.Value) = "bb1" Then
Cells(Target.Row, 1) = "AA3"
Cells(Target.Row, 3) = "CC2"
ElseIf Target.Column = 3 And LCase(Target.Value) = "cc2" Then
Cells(Target.Row, 1) = "AA2"
Cells(Target.Row, 2) = "BB2"
End If
End If
End Sub
Your code is broadly OK, except it will cause an Event Cascade (changing a cell triggers the Worksheet_Change event, which changes a cell, which triggers Worksheet_Change, which ...)
You need to add Application.EnableEvents = False to prevent this (add ... = True at the end)
Here's your code refactored to address this, and a few other minor issues
Private Sub Worksheet_Change(ByVal Target As Range)
Dim changedCells As Range
On Error GoTo EH '~~ ensure EnableEvents is turned back on if an error occurs
Set changedCells = Me.Range("A:C") '~~ explicitly refer to the correct sheet
If Target.Count > 1 Then Exit Sub '~~ do this first, to speed things up
If Not Application.Intersect(changedCells, Target) Is Nothing Then '~~ Target is already a range
Application.EnableEvents = False '~~ prevent an event cascade
'~~ original If Then Else works fine. But can be simplified
Select Case LCase(Target.Value)
Case "aa1"
If Target.Column = 1 Then
Me.Cells(Target.Row, 2) = "BB2"
Me.Cells(Target.Row, 3) = "CC1"
End If
Case "bb1"
If Target.Column = 2 Then
Me.Cells(Target.Row, 1) = "AA3"
Me.Cells(Target.Row, 3) = "CC2"
End If
Case "cc2"
If Target.Column = 3 Then
Me.Cells(Target.Row, 1) = "AA2"
Me.Cells(Target.Row, 2) = "BB2"
End If
End Select
End If
'~~ Fall through to EnableEvents
EH:
Application.EnableEvents = True '~~ ensure EnableEvents is turned back on
End Sub

How do I add multiple ranges in excel, to prevent duplicate entries

I am trying to stop duplicate entries in several columns, from a drop down list. I have it working for the first column, but when I try and add a range for columns C2:C9, D2:D9 and E2:E9 I get errors. This is the code I have for B2:B9, can anyone tell me how to add more ranges? Each column uses the same list for entries. It is a simple list of numbers 1 to 8. I want each column to be able to score 1 to 8, without duplicating the score in the individual column.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B2:B9")) Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If WorksheetFunction.CountIf(Range("B2:B9"), Target) > 1 Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Duplicate score. Please select a different value."
End If
End Sub
Thank you
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range(Cells(2, Target.Column), Cells(9, Target.Column))) Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If WorksheetFunction.CountIf(Range(Cells(2, Target.Column), Cells(9, Target.Column)), Target) > 1 Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Duplicate score. Please select a different value."
End If
End Sub
It will work for any column in rows 2:9.
Consider:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Set r = Range("B2:E9")
If Intersect(Target, r) Is Nothing Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If WorksheetFunction.CountIf(r, Target) > 1 Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Duplicate score. Please select a different value."
End If
End Sub
The code would be slightly different if the columns were disjoint.

hide next column upon getting total in previous column

I have been assigned work on Excel & Macro, and I am not much aware of those.
Task is to Hide Next Column automatically(withouht being refreshed or press F2) if Previous Column Total is 0. and incase if its Total is >=0 then unhide itautomatically(withouht being refreshed or press F2).
Assume I have Set of Columns & Rows(say C11 to C20) where I have to enter values (say 0 or >=0), and once i reach last cell(say C20) which has Formula of Sum for particular column(C11 to C20), If total is 0 then next column (say D) should get Hide without pressing any key if total is >=0 then column D should be as it is.
Please help me out.
Here is the code to Hide Column.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$31" Then
If Target <= 0 Then
Range("D31").EntireColumn.Hidden = True
Else
Range("D31").EntireColumn.Hidden = False
End If
End If
If Target.Address = "$D$31" Then
If Target <= 0 Then
Range("E31").EntireColumn.Hidden = True
Else
Range("E31").EntireColumn.Hidden = False
End If
End If
If Target.Address = "$E$31" Then
If Target <= 0 Then
Range("F31").EntireColumn.Hidden = True
Else
Range("F31").EntireColumn.Hidden = False
End If
End If
End Sub
Here is the solution i found after googling for this long. I am using this only for 1 column right now.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rCell As Range
For Each rCell In Range("C11:C31")
If Range("C31").Value = "0" Then
Range("D31").EntireColumn.Hidden = True
Else
If Range("C31").Value <> "0" Then
Range("D31").EntireColumn.Hidden = False
End If
End If
Next rCell
End Sub

Resources