Here is what I have. I am using a drop down list of 3 values in order to hide columns. Each value has specific columns that are unique to it and when a value is selected, I need the other columns that are not associated with it to be hidden.
I have used the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Value
Case "Marine"
Columns("T:X").EntireColumn.Hidden = True
Columns("Z").EntireColumn.Hidden = True
Case "Inland"
Columns("S").EntireColumn.Hidden = True
Columns("U").EntireColumn.Hidden = True
Case Else
Columns("T:X").EntireColumn.Hidden = False
Columns("Z").EntireColumn.Hidden = False
Columns("S").EntireColumn.Hidden = False
Columns("U").EntireColumn.Hidden = False
End Select
End Sub
This works when I select the values from the drop down but as soon as I click on another cell in the worksheet then the hidden columns reappear. I want to be able to select a value in the drop down and for the cells to remain hidden until I select another value in the drop down. Can anyone help me with this? I have tried to use WorkSheet_SelectionChange but this doesn't work.
Wrap the code with Target.Address.
For example, the Drop down is in the Cell "B2" then,
the code would be as follows:
If Target.Address(True, True) = "$B$2" Then
Select Case Target.Value
Case "Marine"
Columns("T:X").EntireColumn.Hidden = True
Columns("Z").EntireColumn.Hidden = True
Case "Inland"
Columns("S").EntireColumn.Hidden = True
Columns("U").EntireColumn.Hidden = True
Case Else
Columns("T:X").EntireColumn.Hidden = False
Columns("Z").EntireColumn.Hidden = False
Columns("S").EntireColumn.Hidden = False
Columns("U").EntireColumn.Hidden = False
End Select
End If
Additionally, guessing the purpose of your code, I have tweaked it further.
The simplified version would look like:
If Target.Address(True, True) = "$B$2" Then
Select Case Target.Value
Case "Marine"
Columns("S:Z").EntireColumn.Hidden = False
Columns("T:X").EntireColumn.Hidden = True
Columns("Z").EntireColumn.Hidden = True
Case "Inland"
Columns("S:Z").EntireColumn.Hidden = False
Columns("S").EntireColumn.Hidden = True
Columns("U").EntireColumn.Hidden = True
Case Else
Columns("S:Z").EntireColumn.Hidden = False
End Select
End If
Add this (you'll need to adjust it) at the beginning of your code, to check the range that was clicked and eventually abort the sub when user clicks outside your special range.
Dim isect As Range
Set isect = Intersect(Target, Me.Range("$a$8:$a$48"))
If isect Is Nothing Then Exit Sub
You could also check the address:
If Target.Range Like "$X$*" Then...
Update:
On the other hand, if the columns must be shown/hidden depending on where you click in column A, for example, then I would rather use the SelectionChange event. Here is a sample:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim isect As Range
Set isect = Intersect(Target, Me.Range("$a$8:$a$48"))
If Not isect Is Nothing Then
select case Target.Value
.....
end select
End If
End Sub
I think the problem is your Case Else statement. The worksheet change event will be triggered when you go to other cells, and because the value is neither "Marine" nor "Inland," this Else statement gets executed and all the columns are set to Hidden = False.
Since you have 3 options in the drop-down, you only need to make the third Case statement explicit instead of a catch-all.
Check out this code.
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Column
Case 2
If Target.Value = "Marine" Then
Columns("S").EntireColumn.Hidden = False
Columns("U").EntireColumn.Hidden = False
Columns("T:X").EntireColumn.Hidden = True
Columns("Z").EntireColumn.Hidden = True
ElseIf Target.Value = "Inland" Then
Columns("S").EntireColumn.Hidden = True
Columns("U").EntireColumn.Hidden = True
Columns("T:X").EntireColumn.Hidden = False
Columns("Z").EntireColumn.Hidden = False
Else
Columns("T:X").EntireColumn.Hidden = False
Columns("Z").EntireColumn.Hidden = False
Columns("S").EntireColumn.Hidden = False
Columns("U").EntireColumn.Hidden = False
End If
End Select
End Sub
Now in line 4 of the code where the case is selected, give the column number of the column number in which you have the drop-downs or validations enabled in the sheet, in the given case it's 2, which represents column 'B', and Boom!
I have made a little correction based on my past experiences in your code. Assuming that for the Marine entries have to be made in column 'S' and column 'U' and the Island entries are to be made in columns 'T:X' and 'Z'.
In your original code, if you have selected "Marine" first in a particular line item, and later corrected it to "Island", then you'll only end up having all the required columns hidden, but you would have wanted the Island columns un-hidden (to probably enter data in those fields) which I have corrected now.
Related
I am doing some changes in wb created by someone else and need do as less harm as possible as this excel will be used company wide. My issue is, that there are two form controls buttons which changes value in Z1 to 1 or 2, based on the selection.
Option 2 is for one row only, so I would need to hide several lines before this row. I am trying this through worksheet change, but without no luck as the value is not changed in proper way for VBA. The change macro works when I use F2+enter manually, but not when I change it by button only.
I tried to overpass F2+Enter through
Range("Z1").FormulaR1C1 = Range("Z1").FormulaR1C1
But with no luck. I tried several versions of code. My favourite one is this one
If Target.Address = "$Z$1" Then
Range("Z1").FormulaR1C1 = Range("Z1").FormulaR1C1
If Target = "2" Then
Rows("5:13").EntireRow.Hidden = True
Else
Rows("5:13").EntireRow.Hidden = False
End If
End If
My others are
If Not Intersect(Target, Range("Z1")) Is Nothing Then
Rows("5:13").EntireRow.Hidden = CBool(Range("Z1").Value = 2)
End If
========
If Not Application.Intersect(Target, Range(Target.Address)) Is Nothing Then ', Range(Target.Address)
Application.EnableEvents = False
Range("Z1").FormulaR1C1 = Range("Z1").FormulaR1C1
Select Case Target.Value
Case Is = "2": Rows("5:13").EntireRow.Hidden = True
Case Is = "1": Rows("5:13").EntireRow.Hidden = False
End Select
Application.EnableEvents = True
End If
========
If Intersect(Range("Z1"), Target) Is Nothing Then Exit Sub
Range("Z1").FormulaR1C1 = Range("Z1").FormulaR1C1
Select Case Range("Z1").Value
Case Is = 2
Set HideRows = Rows("5:13")
Set ViewRows = Nothing
Case Is = 1
Set ViewRows = Rows("5:13")
End Select
On Error Resume Next
HideRows.Hidden = True
ViewRows.Hidden = False
Neither one is working, I made a video and uploaded on YouTube
What am I missing? I need it for the user to be connected with form control selection. I am not able to pursue change of the form control.
Changes to "linked cell" values do not trigger the worksheet_change event.
You could make it trigger the Worksheet_Calculate event by placing (eg) =Z1 in Z2, then that formula would calculate whenever the value in Z1 is changed. That would mean you'd be responding to every calculation on the sheet though, so you can make sure you only hide/unhide when the value in Z1 has changed:
Private Sub Worksheet_Calculate()
Dim opt, cCache As Range
Set cCache = Me.Range("Z3") 'cell with last value
opt = Me.Range("Z1").Value 'get current value
If opt <> cCache.Value Then 'compare to last value
Debug.Print "Rows toggle"
Me.Rows("5:13").EntireRow.Hidden = (opt = 2) 'hide/unhide
cCache.Value = opt 'save this for next change
End If
End Sub
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
I want to hide/unhide if a specific value is selected from a drop down list. As long as it works, it could be under the worksheet code (when the value is selected) or when a button is pushed. Your help is greatly appreciated.
I have tried with this code unsuccessfully..
Application.EnableEvents = False
If DWR.Cells(4, 14) = "CANTI" Then
DWR.Activate
DWR.Range("10:49").EntireRow.Hidden = False
'must hide the empty rows
DWR.Activate
DWR.Range("50:89").EntireRow.Hidden = True
ElseIf DWR.Cells(4, 14) = "F100" Then
DWR.Activate
DWR.Range("50:89").EntireRow.Hidden = True
'must hide the empty rows
DWR.Activate
DWR.Range("10:49").EntireRow.Hidden = False
End If
Application.EnableEvents = True
Any suggestions?
Try something like this?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then
Application.EnableEvents = False
Range("10:89").EntireRow.Hidden = False '~~~> Default case: display ALL rows
If Target = "CANTI" Then
Range("10:49").EntireRow.Hidden = False
Range("50:89").EntireRow.Hidden = True
ElseIf Target = "F100" Then
Range("10:49").EntireRow.Hidden = True
Range("50:89").EntireRow.Hidden = False
End If
Application.EnableEvents = True
End If
End Sub
I wasn't sure how to perform this function with excel formulas, so I tried VBA. I'm putting together a dashboard and I want a user to only see certain columns depending on what they choose in a drop down list.
My code works fine for its purpose; pick one item from the list, hides certain columns. The issue I'm experiencing is, that when I put a number in a random cell, say 17B, and fill to the right;
I get a
run-time error '13' 'type mismatch'.
When I click out of the pop-up that informs me about the error, my code continues to work. But this constant pop up is a nuisance, what I am doing wrong? The run time error highlights line 2; the first part of the if/then statement. Thank you.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row = 3 And Target.Value = "car" Then
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = False
Application.Columns("J:BZ").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Column = 1 And Target.Row = 3 And Target.Value = "bike" Then
Application.Columns("J:P").Select
Application.Selection.EntireColumn.Hidden = False
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("Q:BZ").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("C:BZ").Select
Application.Selection.EntireColumn.Hidden = False
End If
End Sub
Hell again, here is how tried to solve the problem. The fix did not work; the vba code now does not run. But I wanted to incorporate some ideas that worked for others, and now wish your feedback on how to make what I have below work.
I created a comobox, and used '$A$15:$A$16' as input range; - it's a list that has 'bike' and 'car'
For cell link, I used "$A$3"
The combo box inputs 1 and 2 only into "$A$3", and those are assigned to the two strings I have; 1 - is car, and 2 is bike.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$3" Then
If Target.Value = "1" Then
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = False
Application.Columns("J:BZ").Select
Application.Selection.EntireColumn.Hidden = True
ElseIf Target.Value = "2" Then
Application.Columns("J:P").Select
Application.Selection.EntireColumn.Hidden = False
Application.Columns("C:I").Select
Application.Selection.EntireColumn.Hidden = True
Application.Columns("Q:BZ").Select
Application.Selection.EntireColumn.Hidden = True
Else
Application.Columns("C:BZ").Select
Application.Selection.EntireColumn.Hidden = False
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = 1 Then
If Target.Column = 1 And Target.Row = 3 Then
Application.ScreenUpdating = False
'start by unhiding all columns
Me.Range("C:BZ").EntireColumn.Hidden = False
'then hide any which need hiding...
If Target.Value = "car" Then
Me.Range("J:BZ").EntireColumn.Hidden = True
ElseIf Target.Value = "bike" Then
Me.Range("C:I,Q:BZ").EntireColumn.Hidden = True
End If
Application.ScreenUpdating = True
End If
End If
End Sub
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.