I am creating an inputs page on an excel document.
Cell B3- can have three values, each value triggers a different macro
Cell B4- can have two values, each value triggers a different macro
I wrote the following code:
Sub worksheet_change(ByVal target As Excel.Range)
If target.Cells.Count > 1 Then Exit Sub
If IsNumeric(target) And target.Address = "$B$3" Then
Select Case target.Value
Case Is = 2: Class2
Case Is = 3: Class3
End Select
End If
End Sub
Sub worksheet_change(ByVal target As Range)
Set target = Range("$B$4")
If target.Value = "yes" Then
Call RetireeLife
End If
End Sub
However, I found out the worksheet_change can only be used once in a worksheet but I am unsure on how to combine the codes. Any help would be greatly appreciated.
Depending on what you want to use, ElseIf or Cases, the following demonstrates both used in your worksheet_change:
Sub worksheet_change(ByVal target As Excel.Range)
If target.Cells.Count > 1 Then Exit Sub
If IsNumeric(target) And target.Address = "$B$3" Then
Select Case target.Value
Case Is = 2: Class2
Case Is = 3: Class3
Case Is = 4: YourOtherSub 'You did say 3 options :p
Case Else
MsgBox("Not a correct value in B3")
Exit Sub
End Select
ElseIf target.Address = "$B$4" Then
If target.Value = "yes" Then RetireeLife
ElseIf target.Value = "no" Then KeepLiving 'or whatever your other macro is called
Else
MsgBox("Not a correct value in B4")
Exit Sub
End If
End If
End Sub
Do note that the Exit Sub (apart from the first one) isn't necessary since you'll get out of the If's and exit the sub regardless when you get to those lines in the code. Just in case you'll append extra code, it's there already.
If you want to use Select for the cell-choices, you can use Select for the target.Address and then check within the "$B$3" case if it's Numeric.
Hope this helps!
Like this for example:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Address
Case "$B$3"
Select Case Target.Value
Case 2: Class2
Case 3: Class3
End Select
Case "$B$4"
If IsNumeric(Target.Value) Then RetireeLife
End Select
End Sub
Related
I have the cell "cModelBoolean" in my worksheet which is calculated by formula into a Boolean.
I'm trying to base a select case function based on this cell. If the cell value = True then display MsgBox.
I have no idea why this isn't working!! See my code below
Option Explicit
Private cModelBoolean As Range
Private Sub DefineRangeVariables()
Set cModelBoolean = Me.Range("_cModelBoolean")
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Call DefineRangeVariables
Select Case Target.Address
Case cModelBoolean.Address
Select Case cModelBoolean
Case Is = True
MsgBox "True"
Case Is = False
MsgBox "False"
End Select
End Select
Exit Sub
End Sub
Thanks
Solution:
Private Sub Worksheet_Change(ByVal Target As Range)
Call DefineRangeVariables
Select Case Target.Address
Case cModelBoolean.Address
MsgBox cModelBoolean
' ..or if you want MsgBox only when True, as you state...
If cModelBoolean Then MsgBox cModelBoolean
' ... all other cases
End Select
End Sub
I have a multiple selection, Option buttons, that change the value of cell D7 from 1 to 5, depending on choice. I want to unhide rows 16 to 26 if value is 1 and hide them if it's different, and so on for every other value.
But I can't even get this to work at all, and I'm not sure what I'm doing wrong.
Update: If I change the cell value, nothing happens, but if I delete all contents and add a value it gives: "Argument not optional", and it highlights this part of the code for me:
Private Sub Worksheet_Change(ByVal Target as Excel.Range)
Thank you
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If IsNumeric(Target) And Target.Address = "$D$7" Then
Select Case Target.Value
Case 0 To 90: Cell_Hider
End Select
End If
End Sub
Sub Cell_Hider(ByVal Target As Range)
If Range("$D$7").Value = "1" Then
Rows("16:26").EntireRow.Hidden = False
Else
Rows("16:26").EntireRow.Hidden = True
End If
End Sub
Your procedure Cell_Hider needs an argument but your code calls it without argument Case 0 To 90: Cell_Hider
You call Cell_Hider if the value is between 0 and 90 then that procedure needs the value to be 1 to show the rows and 0 or 2 to 90 will hide them. If you put 100 in that cell nothing happens at all. Sounds not like what you expect to me.
"1" is text not a number!
Something like the following would work:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If IsNumeric(Target) And Target.Address = "$D$7" Then
Select Case Target.Value
Case 0 To 90: Cell_Hider Target
End Select
End If
End Sub
Sub Cell_Hider(ByVal Target As Range)
If Target.Value = 1 Then
Target.Parent.Rows("16:26").EntireRow.Hidden = False
Else
Target.Parent.Rows("16:26").EntireRow.Hidden = True
End If
End Sub
Even though it doesn't look logic to me and I'm not sure what you are exactly trying to achieve.
Note that you can shorten it to
Sub Cell_Hider(ByVal Target As Range)
Target.Parent.Rows("16:26").EntireRow.Hidden = Not Target.Value = 1
End Sub
Hide/Unhide Rows on Cell Change
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sCell As Range: Set sCell = Me.Range("D7")
If Intersect(sCell, Target) Is Nothing Then Exit Sub
If IsNumeric(sCell.Value) Then
HideRows sCell
End If
End Sub
Sub HideRows(ByVal SourceCell As Range)
If SourceCell.Value = 1 Then
SourceCell.Worksheet.Rows("16:26").Hidden = False
Else
SourceCell.Worksheet.Rows("16:26").Hidden = True
End If
End Sub
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
I have this current code working, however I would like to repeat the same task for quite a few rows in a sheet and other sheets in the workbook.
The task I would like to repeat is to unhide and hide rows based on yes/ no drop down. I understand that it's possible to create a code in module and call it in each sheet. Would appreaciate help.
Thank you!!
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C6")) Is Nothing Then Exit Sub
If Target = "Yes" Then
Rows("7:7").Hidden = False
ElseIf Target = "No" Then
Rows("7:7").Hidden = True
End If
End Sub
Perhaps something like this, using the Workbook.SheetChange event. Add this code to the ThisWorkbook code module. It assumes that no other cells besides your drop-downs say "Yes" or "No." It can be easily modified if that is not the case. It can also be modified to only handle certain worksheets and exclude others, if needed.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Select Case Target.Value
Case "Yes"
Target.Offset(1).EntireRow.Hidden = True
Case "No"
Target.Offset(1).EntireRow.Hidden = False
End Select
End Sub
As noted in the answer to your previous question, you can use LCase to make this case-insensitive:
Select Case LCase(Target.Value)
Case "yes"
....
Case "no"
....
End Select
EDIT:
Modified to exclude certain sheets based on their name:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Select Case Sh.Name
Case "Sheet2", "Sheet4" ' change to the names of the sheets to exclude
Exit Sub
End Select
If VarType(Target.Value) = vbString Then
Select Case LCase(Target.Value)
Case "yes"
Target.Offset(1).EntireRow.Hidden = True
Case "no"
Target.Offset(1).EntireRow.Hidden = False
End Select
End If
End Sub
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.