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
Related
I'm trying to show and hide sheets in excel using the if else syntax but i get this error in the first line
'SUB OR FUNCTION NOT DEFINED'
the code is in** sheet 3**
any help is appreciated!
Thank you/Faleminderit
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Me.Range("C1"), Target) Is Nothing Then Exit Sub
If Worksheets(Sheet3).Range(C1).Value = "INTERIM" Then
ShowHide (False)
Worksheets(Sheet1).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "ESTIMATED_BUDGET" Then
ShowHide (False)
Worksheets(Sheet5).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "Final" Then
ShowHide (False)
Worksheets(Sheet6).Visible = True
Worksheets(Sheet3).Visible = True
ElseIf Worksheets(Sheet3).Range(C1) = "ALL" Then
ShowHide (True)
End If
End Sub
Sub Macro1()
End Sub
I tried different ways to do it and i get the same error
When testing here I created 2 sheets so I had Sheet1, Sheet2, and Sheet 3.
I had hadded your code to the cose of sheet 1 initially and straight away was getting errors. I think it is important to note that sheet names and cell references or ranges so need to be encapsualted with Quote marks ("Sheet3").
Once the Sheets and Cells where quoted I was able to continue. I'm not sure on the purpose of "ShowHide". I will assume you wanted to track if the sheets are visible or not, this will need to be declared.
I also recommend setting 'Option Explicit' at the top of the module, this helps with error handling and debugging when you have problems.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ShowHide As Boolean
ShowHide = False
If Intersect(Me.Range("C1"), Target) Is Nothing Then Exit Sub
If Worksheets("Sheet3").Range("C1").Value = "INTERIM" Then
ShowHide = False
Worksheets("Sheet1").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "ESTIMATED_BUDGET" Then
ShowHide = False
Worksheets("Sheet5").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "Final" Then
ShowHide = False
Worksheets("Sheet6").Visible = True
Worksheets("Sheet3").Visible = True
ElseIf Worksheets("Sheet3").Range("C1") = "ALL" Then
ShowHide = True
End If
End Sub
This worked as I expected.
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
The scripts below bring up a Run-time error '13' Type mismatch if I have a value in AE49 or Z40 but then delete it. Ideally, if the value is deleted, it should 're hide' the rows (this is what I have attempted to achieve with the Case Else line).
Private Sub Script2(ByVal Target As Range)
If (Not Intersect(Target, Range("Z40")) Is Nothing) Then
Select Case Target.Value
Case "PowerPoint", "Verbal"
Range("A41").EntireRow.Hidden = False
Case Else
Range("A41").EntireRow.Hidden = True
End Select
End If
End Sub
Private Sub Script3(ByVal Target As Range)
If (Not Intersect(Target, Range("AE49")) Is Nothing) Then
Select Case Target.Value
Case 1
Range("A50:A52").EntireRow.Hidden = False
Range("A53:A55").EntireRow.Hidden = True
Case 2
Range("A50:A53").EntireRow.Hidden = False
Range("A54:A55").EntireRow.Hidden = True
Case 3
Range("A50:A54").EntireRow.Hidden = False
Range("A55").EntireRow.Hidden = True
Case 4
Range("A50:A55").EntireRow.Hidden = False
Case Else
Range("A50:A55").EntireRow.Hidden = True
End Select
End If
End Sub
I believe is nothing applies to objects.
Try
if nz(yourRange, "") <> "" then
Without knowing more specifically what you're trying to achieve, I'd us the WorkSheet_Change event:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
If (Not Intersect(Target, Range("Z40")) Is Nothing) Then
If Target.Value <> "PowerPoint" And Target.Value <> "Verbal" Then
Rows(41).Hidden = True
Else
Rows(41).Hidden = False
End If
End If
If (Not Intersect(Target, Range("AE49")) Is Nothing) Then
Select Case Target.Value
Case 1
Range("A50:A52").EntireRow.Hidden = False
Range("A53:A55").EntireRow.Hidden = True
Case 2
Range("A50:A53").EntireRow.Hidden = False
Range("A54:A55").EntireRow.Hidden = True
Case 3
Range("A50:A54").EntireRow.Hidden = False
Range("A55").EntireRow.Hidden = True
Case 4
Range("A50:A55").EntireRow.Hidden = False
Case Else
Range("A50:A55").EntireRow.Hidden = True
End Select
End If
With Application
.EnableEvents = True
.ScreenUpdating = False
End With
End Sub
Open the VBA IDE, double click the sheet you're like to run the code on and copy the entire block of code I posted above (you can omit the Option Explicit if you already have it set). You can only have one Worksheet_Change event per sheet, so if you're already using that event, you'll have to post that code and we can try and merge the two.
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.