Microsoft VBA Error "Sub or Function not defined" - excel

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.

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

Hide and unhide rows in Excel VBA when specific value in the one cell

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

Run-time error '13' on drop down column selector

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

Define a Static variable, initialize it and then use it in Excel VBA

I am quite new programming macros in Excel and I am not sure how to initialize a Static variable, initialize it once, and then use it keeping the value when I modify it during different function calls.
I have a cell with the caption "Hide rows", and I want to toggle the visualization of different rows when clicking that cell.
I tried with this code, but like I initialize the variable Hidden to False at the beginning, it only works hidding the rows, not showing them.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static Hidden As Boolean
Hidden = False
If Selection.Count = 1 Then
If Not Intersect(Target, Range("F8")) Is Nothing Then
If Hidden = False Then
Rows("1:1").EntireRow.Hidden = True
MsgBox "Aaaau!"
Hidden = True
Else
Rows("1:1").EntireRow.Hidden = False
MsgBox "Hola!"
Hidden = False
End If
End If
End If
End Sub
you set hidden to false at the beginning of the macro, so hidden is always false when entering the test instruction.
try this
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static Hidden As Boolean
If Selection.Count = 1 Then
If Not Intersect(Target, Range("F8")) Is Nothing Then
If Hidden = False Then
Rows("1:1").EntireRow.Hidden = True
MsgBox "Aaaau!"
Hidden = True
Else
Rows("1:1").EntireRow.Hidden = False
MsgBox "Hola!"
Hidden = False
End If
End If
End If
End Sub

excel vba slowing excel down, causing 10 second egg timer delay when clicking anywhere on sheet

I am using the following vba codes which im using to hide a set of rows and unhide rows depending on if a cell contains text or not, and they are causing my excel spreadsheet to be slow and unresponsive and causing the egg timer to show for about 10 seconds.
If I take the code out It speeds things up so what can I do to my codes to get them to speed up and not take so long? perhaps there is a better way of structuring the code but im really new to vba so am not sure what I would need to do, would appreciate someone's help thanks.
the reason I am using worksheet change and worksheet selection change is so that whether a user clicks on a cell or not the page still updates
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("K22").Value <> "" Then
Application.ScreenUpdating = False
Rows("25:38").EntireRow.Hidden = False
Rows("40:48").EntireRow.Hidden = True
ElseIf Range("K22").Value = "" Then
Rows("25:38").EntireRow.Hidden = True
Rows("40:48").EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("K22").Value <> "" Then
Application.ScreenUpdating = False
Rows("25:38").EntireRow.Hidden = False
Rows("40:48").EntireRow.Hidden = True
ElseIf Range("K22").Value = "" Then
Rows("25:38").EntireRow.Hidden = True
Rows("40:48").EntireRow.Hidden = False
End If
Application.ScreenUpdating = True
End Sub
The main issue is from the Worksheet_Change event, but it could be applied to any event.
The worksheet change is triggering each time you hide a column, so it's trying several times to hide the same columns, before (eventually) failing with an out of memory error:
Hide these columns... Oh, a worksheet change... Hide these columns... Oh, A worksheet change... Hide th...
To avoid this, you need to use
Application.EnableEvents = False
when you decide you are going to make changes, and
Application.EnableEvents = True
when done.
You may also want to put some error handling that turns the events on again, as if something else occurs that stops the code from running, the triggers will be turned off, and the spreadsheet will no longer update as you expect it to.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Application.EnableEvents = False
If Range("K22").Value <> "" Then
Rows("25:38").Hidden = False
Rows("40:48").Hidden = True
Else
Rows("25:38").Hidden = True
Rows("40:48").Hidden = False
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This works instantly for me:
Application.ScreenUpdating = False
Select Case Range("K22")
Case Is <> ""
Rows("25:38").Hidden = False
Rows("40:48").Hidden = True
Case Else
Rows("25:38").Hidden = True
Rows("40:48").Hidden = False
End Select
Application.ScreenUpdating = True

Resources