Private Sub Worksheet_Change(ByVal Target As Range) not working - excel

I'm trying to hide rows based on the value in cell B3. This was working for me earlier but I unfortunately can't figure out what I changed. I right clicked on the tab and pasted this in 'View Code'. Here's my code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$3" Then
If Range("B3") = "a" Then
ActiveSheet.Rows("40:43").EntireRow.Hidden = False
ElseIf Range("B3") = "b" Then
ActiveSheet.Rows("40:43").EntireRow.Hidden = True
End If
End If
End Sub

Your code works, but if you already using the Target, why not use it full capabilities:
If Target.Address = "$B$3" Then
If Target.Value2 = "a" Then ' <-- use Target.Value2
Rows("40:43").EntireRow.Hidden = False ' <-- there's no need to use ActiveSheet
ElseIf Target.Value2 = "b" Then
Rows("40:43").EntireRow.Hidden = True
End If
End If
Note: there's no need to use ActiveSheet, as this event is triggered inside the ActiveSheet.
You can use Select Case if you will need to add more scenarios in the future:
If Target.Address = "$B$3" Then
Select Case Target.Value2
Case "a"
Rows("40:43").EntireRow.Hidden = False
Case "b"
Rows("40:43").EntireRow.Hidden = True
Case Else ' in case you want add more scenarios in the future
' do something else...
End Select
End If

Related

Hide Entire given rows if a specified cell is blank

I am a newbie in VBA coding.
I am trying to achieve that inside my function
sub Private Sub Worksheet_Change(ByVal Target As Range)
where it checks some specified cells value change to run a given macro. The additional macro that i want to add is that when ever cell a62 is Empty, it will hide rows a56:a61. and consecutively if a82 is empty, it will hide rows a78:a82.
i have the following code but it only hides the rows from the first empty cell until end of sheet.
Sub Test()
Dim i As Long
For i = 4 To 800
If Sheets("Results").Cells(i, 1).Value = "" Then
Rows(i & ":" & Rows.Count).EntireRow.Hidden = True
Rows("1:" & i - 1).EntireRow.Hidden = False
Exit Sub
End If
Next
End Sub
Please, check the next event code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$62" Or Target.Address = "$A$82" Then
Select Case Target.Address
Case "$A$62"
If Target.Value = "" Then
Range("A56:A61").EntireRow.Hidden = True
Else
Range("A56:A61").EntireRow.Hidden = False
End If
Case "$A$82"
If Target.Value = "" Then
Range("A78:A81").EntireRow.Hidden = True
Else
Range("A78:A81").EntireRow.Hidden = False
End If
End Select
End If
End Sub
It will be triggered only if you MANUALLY change the value of one of the two required cells.
If their value is the result of a formula, Worksheet_Calculate event must be used, but in a different way. This event does not have any argument (no Target) and you must check the two cells in discussion and act according to their value, independent if they were changed or not when the Calculate event is triggered. If this is the case, I can post such an event code, too.
Edited:
For the version which does not involve the manual changing of the values, please copy this event code in the sheet code module:
Private Sub Worksheet_Calculate()
If Me.Range("A62").Value = "" Then
Me.Range("A56:A61").EntireRow.Hidden = True
Else
Me.Range("A56:A61").EntireRow.Hidden = False
End If
If Me.Range("A82").Value = "" Then
Me.Range("A78:A82").EntireRow.Hidden = True
Else
Me.Range("A78:A82").EntireRow.Hidden = False
End If
'Edited:
'The part for both analyzed ranges being empty:
If Me.Range("A62").Value = "" And _
Me.Range("A82").Value = "" Then
'Do here what you need...
End If
End Sub

VBA Multiple Target.Address (OR) for the Same Action

In a worksheet I have two drop-down lists (cells C7 and C68) which each have a dependent drop-down in the cell below. I have a code (below) which will clear the cell of the dependent drop-down if I change the selection in the above list (so that the lists do not mis-match), however I can only get this to work for the one drop-down in the sheet. How can I amend this to that it works if I alter either of the cells with the "Parent" list?
.
Existing code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = True
Target.Offset(1, 0).Value = ""
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
I suggest using only 1 dropdown list (which user actually selects) and then 2nd "linked cell" is using vlookup from some kind of data transformation list.
All fixed - for anyone who also has this problem, the correct code was:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Or Target.Address = "$C$68" Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
Target.Offset(1, 0).Value = ""
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub

VBA - Change cell value when clicked from/to yes/no for merged cells

I have created a form that will give the user the choice to pick from 7 different options whcih will all be default blank. When they click the cell next to the option it will change from blank to "yes" and when clicked again it will remove the text and so on. The issue I have its the cell that is clickable from blank to "yes" is merged between R33 and S33. The code works on the cell R33 alone but not when I merge them. Can you help me out with this please?
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.EnableEvents = False
If Target.Cells.Count = 1 Then
If Not Intersect(Target, Range("R33")) Is Nothing Then
Select Case Target.Value
Case ""
Target.Value = "yes"
Case "yes"
Target.Value = ""
End Select
Range("A1").Select
End If
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
On Error GoTo Worksheet_SelectionChange_Error
Application.EnableEvents = False
Debug.Print Target.Cells.Count 'just FYI, remove it later
If Not Intersect(Target.Cells(1), Range("R33").MergeArea) Is Nothing Then
Select Case Target.Cells(1)
Case "yes"
Target.Cells(1) = vbNullString
Case vbNullString
Target.Cells(1) = "yes"
End Select
End If
Range("A1").Select
Application.EnableEvents = True
On Error GoTo 0
Exit Sub
Worksheet_SelectionChange_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ")
Application.EnableEvents = True
End Sub
The merged cells are a bit evil in Excel, but if you play around a bit you can achieve what you need:
Target.Cells.Count is equal to the number of merged cells, thus it is never 1. I have deleted it;
Target.Cells(1) is the way to refer the first cell of the MergedArea;
Range("R33").MergeArea is a good way to check the intersect;
As sometimes while executing _SelectionChange event, you may get an error and then leave the Application.EnableEvents = False, it is a good practice to use an Error Catcher, which sets it back to True;

Worksheet_Change to hide rows in Excel

I have a macro that is supposed to hide a row in excel when a value of a given cell is "ODD" (the word, not an odd number). I've tried two different formats; neither gives any visible error but neither hides the row.
Sub Worksheet_Change(ByVal target As Range)
If target.Address <> "$B$2" Then Exit Sub
ElseIf Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = False
Else
Rows("5:5").EntireRow.Hidden = True
End If
End If
End Sub
The other code I had is:
Select Case Range("B2").Value
Case Is = "ODD": Rows("5:5").EntireRow.Hidden = False
Case Else: Rows("5:5").EntireRow.Hidden = True
End Select
It was modified from a more advanced case statement and I just left it that way at first.
The Rows("5:5") would be better as Rows(5). The method you used would be better as Range("5:5").
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
Rows(5).EntireRow.Hidden = (UCase(Target.Value) = "ODD")
End If
End Sub
Since comparing B2 to ODD already produces a True or False, you can dispense with the If/Else/End If. Text comparisons in VBA are usually case sensitive, hence the need for UCase to force case insensitivity.
You are missing a key code line If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then Try the following
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Set cell = Range("B2")
If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then
If Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = False
Else
Rows("5:5").EntireRow.Hidden = True
End If
End If
End Sub
First make sure your Change Sub is stored in the Worksheet module of the Sheet you want this to perform on. Then you have a slight syntax error with your If Statements:
Private Sub Worksheet_Change(ByVal target As Range)
If target is Nothing Then Exit Sub
If target.Address <> "$B$2" Then Exit Sub
If Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = True
Else
Rows("5:5").EntireRow.Hidden = False
End If
End Sub
When you put the If...Then... on one line, it actually closes the If (no End If needed) Also, I flipped your True and False statements to match your requirement in your question.

Issue Combining Worksheet_Change subs (Invalid or Unqualified Reference Error)

I have a project that uses a Worksheet_Change sub to make certain images visible or invisible based on the value of a particular cell (in this case B23).
Now I'm trying to add a second criteria to make a different set of images visible/invisible based on the value in a different cell (in this case B24).
The problem is that I'm now getting a "Invalid or Unqualified Reference" Error, and it looks like it has to do with the ".pictures" piece. Here is the code I'm trying to run:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("$B$23,$B$24")) Is Nothing Then Exit Sub
If Target.Address = "$B$23" Then
Select Case Target.Value
Case "driving"
.Pictures("Auto_Map_Labeled").Visible = True
.Pictures("Bicycle_Map_Labeled").Visible = False
.Pictures("Pedestrian_Map_Labeled").Visible = False
Case "bicycling"
.Pictures("Auto_Map_Labeled").Visible = False
.Pictures("Bicycle_Map_Labeled").Visible = True
.Pictures("Pedestrian_Map_Labeled").Visible = False
Case "walking"
.Pictures("Auto_Map_Labeled").Visible = False
.Pictures("Bicycle_Map_Labeled").Visible = False
.Pictures("Pedestrian_Map_Labeled").Visible = True
End Select
ElseIf Target.Address = "$B$24" Then
Select Case Target.Value
Case "visible"
.Pictures("Thumbs_Up").Visible = True
Case "invisible"
.Pictures("Thumbs_Up").Visible = False
End Select
End If
End Sub
Any ideas what might be going wrong? I should note that I'm very new to VBA, so looking for the simplest solution possible.

Resources