Hide Rows in Excel based on cell value - excel

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

Related

Trying to Have specific cell values trigger multiple macros in VBA code

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

Merging multiple Worksheet_SelectionChange

I have got no problem in running one Private Sub Worksheet_SelectionChange , but when i add multiple Worksheet_SelectionChange events it is not running. Later, i came to know that it is not possible running different worksheet selection change events in the same sheet.
I am having four different Private Sub Worksheet_SelectionChange events, trying to merge them with the help of various sites but none worked to me, as per my understanding.
Could i get some help,
1.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells1 As Range
Set cells1 = ActiveSheet.Range("B1:B27")
If Not (Intersect(Target, cells1) Is Nothing) Then
ActiveSheet.Range("B30").Value = Target.Value
End If
End Sub
2.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells2 As Range
Set cells2 = ActiveSheet.Range("C1:C27")
If Not (Intersect(Target, cells2) Is Nothing) Then
ActiveSheet.Range("C30").Value = Target.Value
End If
End Sub
3.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells3 As Range
Set cells3 = ActiveSheet.Range("S1:S27")
If Not (Intersect(Target, cells3) Is Nothing) Then
ActiveSheet.Range("S30").Value = Target.Value
End If
End Sub
4.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells4 As Range
Set cells4 = ActiveSheet.Range("T1:T27")
If Not (Intersect(Target, cells4) Is Nothing) Then
ActiveSheet.Range("T30").Value = Target.Value
End If
End Sub
I appreciate your help.
Thank you.
You can use a switch (select case) within your change event to allow options for which will occur.
Mock-up:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row > 27 then Exit Sub
Select Case Target.Column
Case 2, 3, 19, 20
Cells(30,Target.Column).Value = Target.Value
End Select
End Sub
I have added the Exit Sub check for if the row > 27, as your ranges are 1:27, for each of the Columns. This replaces the Intersect() check.
You perform the same action based on the Target.Column, so that is the only other parameter to verify and utilize.

How to trigger macros when clicking on a specific cell

I want to trigger two independent macros each based on different cells. To be specific, I want to trigger the Orange macro when cell E8 is clicked. And the Factiva macro when cell E9 is clicked. This is the code I came up with so far,,,, but does not work... The two macros are related to showing a graph(normal bar graph) !
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E9")) Is Nothing Then
Call Factiva
End If
End If
End Sub
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E8")) Is Nothing Then
Call Orange
End If
End If
End Sub
Sub Factiva()
'
' Factiva Macro
'
'
ActiveSheet.Shapes.Range(Array("factiva")).Visible = msoFalse
ActiveSheet.Shapes.Range(Array("factiva")).Visible = msoTrue
Application.CommandBars("Selection").Visible = False
End Sub
Sub Orange()
'
' Orange Macro
'
'
ActiveSheet.Shapes.Range(Array("Orange Business")).Visible = msoFalse
ActiveSheet.Shapes.Range(Array("Orange Business")).Visible = msoTrue
Application.CommandBars("Selection").Visible = False
End Sub
You can't have two procedures with the same name in the same module, or two handlers for the same worksheet event in a Worksheet module. Option Explicit isn't valid anywhere else than in a module's declarations section, at the very top (you can't have it between procedures).
You need the SelectionChange handler to determine which cell is selected, and decide which macro it wants to invoke accordingly.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count <> 1 Then Exit Sub 'bail out immediately instead of nesting
Select Case True
Case Not Intersect(Target, Range("E9")) Is Nothing
Factiva 'note: Call keyword is redundant
Case Not Intersect(Target, Range("E8")) Is Nothing
Orange 'note: Call keyword is redundant
'Case ...
End Select
End Sub
Note that if that is the only code that needs to invoke the Factiva and Orange procedures, then they can both be made Private. Also, consider renaming your procedures using meaningful names that start with a verb, e.g. ShowFactivaShape, or ShowOrangeBusinessShape.
In fact, you could parameterize the code and remove one of the two:
Private Sub ShowShape(ByVal shapeName As String)
ActiveSheet.Shapes(shapeName).Visible = msoTrue
Application.CommandBars("Selection").Visible = False
End Sub
Note that there shouldn't be a need to set visibility to msoFalse before you set it to msoTrue, and the Shapes.Range(Array(...)) is superfluous, since you're only interested in a single named Shape.
The SelectionChange handler would then look like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count <> 1 Then Exit Sub 'bail out immediately instead of nesting
Select Case True
Case Not Intersect(Target, Range("E9")) Is Nothing
ShowShape "Factiva"
Case Not Intersect(Target, Range("E8")) Is Nothing
ShowShape "Orange Business"
'Case ...
End Select
End Sub
Consider making a similar HideShape procedure if you need to hide "Orange Business" when "Factiva" is shown, and vice-versa - or better, consider adding a Optional ByVal isVisible As Boolean = True parameter to ShowShape, and then you can use the same procedure for both purposes:
Private Sub ShowShape(ByVal shapeName As String, Optional ByVal isVisible As Boolean = True)
ActiveSheet.Shapes(shapeName).Visible = IIf(isVisible, msoTrue, msoFalse)
Application.CommandBars("Selection").Visible = False
End Sub
That way you can easily show/hide shapes as needed:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count <> 1 Then Exit Sub 'bail out immediately instead of nesting
Select Case True
Case Not Intersect(Target, Range("E9")) Is Nothing
ShowShape "Factiva"
ShowShape "Orange Business", isVisible:=False
Case Not Intersect(Target, Range("E8")) Is Nothing
ShowShape "Orange Business"
ShowShape "Factiva", isVisible:=False
'Case ...
End Select
End Sub

Hide commandbutton based on cell value excel vba

I am trying to hide a commandbutton based on a specific cell value. I have looked up several codes and pasted them in excel (in the vba form when right clicking the sheet and selecting "view code").
What am I doing wrong?
Here's one of the codes I've tried:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("A1") = 0 Then ActiveSheet.CommandButton1.Visible = False
If Range("A1") = 1 Then ActiveSheet.CommandButton1.Visible = True
End Sub
Make sure you enable events before using your code. Also, you must place your code in Worksheet module, not in regular module. To enable events, use this simple sub.
Sub Enable_events()
Application.EnableEvents = True
End Sub
please run this first:
Sub enable_()
Application.EnableEvents = True
End Sub
and then your Code will run perfectly:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("A1") = 0 Then ActiveSheet.CommandButton1.Visible = False
If Range("A1") = 1 Then ActiveSheet.CommandButton1.Visible = True
End Sub
Your code is confusing, for a number of reasons.
Range, when it's not qualified with a Worksheet object, implicitly refers to the ActiveSheet, i.e. ActiveSheet.Range... but when it's in a worksheet's code-behind, it implicitly refers to that worksheet's Range property, i.e. Me.Range. Because the meaning of an unqualified Range call depends on context, it's best to always qualify it with an explicit Worksheet object.
So if you're in the code-behind module for Sheet1, then Range("A1") is equivalent to Sheet1.Range("A1"), or even better, Me.Range("A1").
The two conditions will be evaluated every time, but only one of them needs to be: it's inefficient.
Truth is, you don't need to assign a Boolean literal - a Boolean expression is much cleaner.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.CommandButton1.Visible = (Me.Range("A1") = 1)
End Sub
Now, assuming Application.EnableEvents returns True, that code will run every time the selection changes, which is rather overkill.
Handle the Worksheet.Change event instead, and only act when the modified cell is A1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.Intersect(Target, Me.Range("A1")) Is Nothing And Target.Count <> 1 Then
' we don't care about that cell: bail out
Exit Sub
End If
Me.CommandButton1.Visible = (Me.Range("A1") = 1)
End Sub
Please try this code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Selection.Cells.Count = 1 Then
If Range("A1") = 0 Then ActiveSheet.CommandButton1.Visible = False
If Range("A1") = 1 Then ActiveSheet.CommandButton1.Visible = True
End If
End If
End Sub
Hope this help.

Type 13 mismatch error

I'm receiving a type 13 mismatch error with Excel VBA. This script checks two columns and locks cells in a column once a change is made, or doesn't lock it if the user clicks the cell and clicks off without any changes. Line 5 is apparently the culprit. Any help is much appreciated.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range
Set A = Union(Range("I:I"), Range("J:J"))
If Intersect(Target, A) Is Nothing Then Exit Sub
If Target.Value = "" Then Exit Sub
ActiveSheet.Unprotect Password:="YourPassword"
Target.Locked = True
ActiveSheet.Protect Password:="YourPassword"
End Sub
Target is the cell or cells that have been changed. If Target is more than a single cell (e.g. pasted block of values, etc) then Target does not have a .Value. Add If Target.Count > 1 Then Exit Sub to the top of the code or loop through Target, examining each cell within Target for the .Value.
Example of the latter,
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Union(Range("I:I"), Range("J:J"))) Is Nothing Then
On Error GoTo bm_Safe_Exit
ActiveSheet.Unprotect Password:="YourPassword"
Application.EnableEvents = True
Dim ij As Range
For Each ij In Intersect(Target, Union(Range("I:I"), Range("J:J")))
If ij.Value <> "" Then
ij.Locked = True
End If
Next ij
End If
bm_Safe_Exit:
ActiveSheet.Protect Password:="YourPassword"
Application.EnableEvents = True
End Sub
Additionally, it is not considered a 'best practise' to use the ActiveSheet property in a Worksheet_Change event macro.

Resources