Compile - Syntax Error: When toggling select filter with If Statement - excel

it's me again - I'll get to know this language better eventually.
Basically - I have a big table of data that has autofilter on - range "$B$5:$Z$1697"
However, there is an additional filter on the R column that I want to be toggled on or off.
Therefore I need an If statement that says when the additional filter is on, remove, whereas, if the filter is not on at the time you press the button - apply it.
I've played around with this and watched more videos that I care to admit. However, there must be something I'm overlooking or don't understand.
The code works up until the Else line, which returns:
"Compile Error, Syntax Error".
Can anyone explain what's happening?
If Sheets(4).Range("$B$5:$Z$1697").AutoFilter(Field:=17, Criteria1:="=") = True Then
'If there specific filter on column R is on then
Sheets(4).Range("$B$5:$Z$1697").AutoFilter Field:=17
'Turn off that filter.
Else: Sheets(4).Range("$B$5:$Z$1697").AutoFilter(Field:=17, Criteria1:="=")
'Else, if the filter is off, turn it on.
End If
End Sub
EDIT: I have corrected the code, amending this ELSE line to this
Else: Sheets(4).Range("$B$5:$Z$1697").AutoFilter(Field:=17, Criteria1:="=") = True
However, when I run this now it means that it turns the filter On and then Off again with one push of the button. How do I make it so it onl makes on change at a time.
I.e. if the filter is on when the button is pressed it ONLY turns it off.
And vice versa

The easiest way to toggle a filter on/off is to use an ActiveX Toggle button. When the toggle button is clicked (enabled) your filter will be applied, when clicked again (disabled) your filter is removed. Change the name of the toggle button and Criteria1 to meets your needs.
Private Sub ToggleButton1_Click()
'when selected(enabled) the filter for Col "Q" will be enabled
If ToggleButton1.Value = True Then
Sheets(4).Range("$B$5:$Z$1697").AutoFilter Field:=17, Criteria1:="2"
Else
'when selected again(disabled) the filter for Col "Q" will be Disabled
Sheets(4).Range("$B$5:$Z$1697").AutoFilter Field:=17
End If
End Sub

This is NOT a complete answer, but I can't fit all this into a comment...
I've found that continually using compound references to refer to different objects within Excel can really give me headaches. Using intermediate objects doesn't impose any significant performance penalty at all, so I am in the habit of breaking down the compound reference into a series of intermediate objects. Doing this gives me at least two advantages: 1) I can examine the intermediate objects to make sure of the data I think should be there, and 2) I get far fewer syntax errors because each step gets validated.
So, while I can't check if this is correct since I can't access your data, your logic could look like this
Sub Example()
Dim fourthWS As Worksheet
Dim filteredData As Range
Set fourthWS = ThisWorkbook.Sheets(4)
Set filteredData = fourthWS.Range("$B$5:$Z$1697")
Dim dataIsFiltered As Variant
dataIsFiltered = filteredData.AutoFilter(Field:=17, Criteria1:="=")
If dataIsFiltered Then
'--- turn off the filter
filteredData.AutoFilter Field:=17
Else
'--- turn on the filter
filteredData.AutoFilter Field:=17, Criteria1:="="
End If
End Sub

Related

VBA skipping code based on previous error

I think I am missing something obvious but have been looking at it for so long I think I may be blind to it.
I have a sheet coded to create a pivot table from a downloaded report and apply specific filters to the pivot table - which I am aware will have some instances where there is only 1 item in the filter list so cannot apply and returns an error.
I have managed to add 'On Error GoTo...' a line past the code I know will not be able to process.
However, I also have a second Pivot Table on the same sheet which applies the same filter but in reverse - i.e. the filter will usually have 2 items, so the 2 pivot tables end up showing the breakdown of the contents of each item.
The problem is 'On Error GoTo...' is not working on the second item.
I have the GoTo locations name differently - the first pivot GoTo = 'NoOKL:' and the second = 'NoOKS:'.
Because an error on the first Pivot will mean an error on the second every time I am trying to get around this by adding 'P = 1' to the error handling of the first Pivot and then added the code below for the second:
If P = 1 Then GoTo NoOKS
With ActiveSheet.PivotTables("PivotTable11").PivotFields("CATEGORY")
.PivotItems("OKL_CONTRACTS").Visible = False
End With
ActiveSheet.PivotTables("PivotTable11").PivotFields("CATEGORY"). _
EnableMultiplePageItems = True
NoOKS:
End If
I have tried moving the GoTo location 'NoOKS' both inside the If statement and outside but get the same result.
Any help would be appreciated.
Here's an example of what your code might look like if you omit all GoTo's.
If P = 1 Then
With ActiveSheet.PivotTables("PivotTable12").PivotFields("CATEGORY")
.PivotItems("OKL_CONTRACTS").Visible = False
.EnableMultiplePageItems = False
End With
Else
With ActiveSheet.PivotTables("PivotTable11").PivotFields("CATEGORY")
.PivotItems("OKL_CONTRACTS").Visible = False
.EnableMultiplePageItems = True
End With
End If
This code presumes that you have 2 pivot tables of which you want to hide one subject to the number of items it will display (presumed to be P) and set the EnableMultiplePageItems property differently. This doesn't make much sense in the above example but the objective is to show the use of If and Else instead of GoTo.
I point out that EnableMultiplePageItems = (P = 1) would also set the property to either True or False depending upon the evaluation of the statement (P = 1). In the above example the property belongs to different objects but if you have to set the same property for the same object to different values in your project depending upon the value of P that method will avoid even the use of If, not to mention GoTo and reduce the amount of code as well.

VBA Toggle Button For Filter not Running Second Condition

I have asked a very similar question to this before on another project, therefore I have already gone through their suggestions and attempted to implement them in the code here. However, none of it is working...
Here's the question for reference:
Compile - Syntax Error: When toggling select filter with If Statement
I don't know whether that's just an unnoticed error or something specific about the objects and syntax I don't know.
Basically, what I want here is to have a toggle button - when you press it it alternates between filtering the Q column to show only the not accepted jobs, and to reset that filter so it shows all jobs.
Somehow in my code it only plays the IF portion of the statement and not the Else.
It's not the filter code lines itself as if I move the other to be the first statement it runs without errors, other than it doesn't play the else statement on the next press.
Can anyone help
Private Sub ToggleButton1_Click()
If ShowRejected.Value = True Then
Sheets(1).ListObjects("Table1").Range.AutoFilter Field:=12, Criteria1:= _
"Not Accepted"
'when selected(enabled) the filter for Col "Q" will be enabled, showing only the jobs that have been rejected by partners
Else:
Sheets(1).ListObjects("Table1").Range.AutoFilter Field:=12
'when selected again(disabled) the filter for Col "Q" will be Disabled, therefore showing all jobs
End If
End Sub
Edit - After making changes to my sheet it works
Like say in comment, you have to ShowRejected by the name of your button.
The code below works fine :
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
MsgBox "if"
Else:
MsgBox "else"
End If
End Sub

Hide and unhide series in a chart based on their name and i have error 1104

I am trying to hide and unhide series in a chart based on their name using excel vba and I have a error 1004 invalid parameter after the first run of the for cycle.
Sub macroChart3()
'
' macroChart3 Macro test
'
Dim i, n As Integer
For i = 1 To 96 Step 1
If ActiveChart.SeriesCollection(i).Name = "*contracted*" Then
ActiveChart.SeriesCollection(i).IsFiltered = False
Else
ActiveChart.SeriesCollection(i).IsFiltered = True
End If
Next i
End Sub
SeriesCollection.IsFiltered method seemed to toggle this check box:
I couldn't use that. I wanted to record macro and see if any other method is used but the checkbox is gone in 2010:
So it might be not possible to use that anymore. If you are using a different version where this method exists you might have a problem that the series is not listed in in seriesCollection anymore:
Remarks from MSDN
When a user filters out a series, the series IsFiltered property switches to True, and the series is transferred out of its parent SeriesCollection.
See if you can use FullSeriesCollection instead when you change the visibility of series:
ActiveChart.FullSeriesCollection(2).IsFiltered = True
If that doesn't work you might add and remove ranges instead of hiding them.
UPDATE:
Run your macro in step mode (F8) so you have full visibility of the execution steps. Add your evaluated expressions (ones that are used within IFs) to see their result and you will find out if they are incorrect or are evaluated as FALSE and try to figure out why.
And don't forget to up vote the answer if you find it helpful.
thanks for the quick reply, the error is fixed, but still not all the series are processed by the if clauses in the for cycle, for example this statement:
Else
ActiveChart.SeriesCollection(i).IsFiltered = True
End If
Isn't executed and I don't understand why.

Automatically add to list

I'm having a small issue manipulating data.
I have a few check-boxes to select various items on the screen, which in turn inputs relevant data such as item name, price, etc. The issue is that these names have to go onto a invoice, and I cannot get the names to transfer onto the invoice nicely.
The invoice has to record the items, but it needs to check to see if a item has been already applied to the invoice and this is what I'm having problems with.
How can I get items to collectively display on a sheet without any additional interactions (I just click the relevant checkbox instead of manually typing it in), and display them nicely.
The items 'Underlay and Extended Warranty' need to be displayed one after another, and not 2-3 boxes away.
Example:
Underlay Extended Warranty
I don't want this:
Underlay
Extended Warranty
Is there a solution to this?
Update:
This happens if I do it the way I know:
I solved this by making a custom Excel VBA Macro that basically tells us the cell that is free which is closest to the top of the invoice:
Public Function GetApplicableCell() As String
Range("C20").Select
If (IsEmpty(Range("'Invoice Sheet'!C20").Value)) Then
GetApplicableCell = "C20"
End If
If (IsEmpty(Range("'Invoice Sheet'!C21").Value)) Then
GetApplicableCell = "C21"
End If
If (IsEmpty(Range("'Invoice Sheet'!C22").Value)) Then
GetApplicableCell = "C22"
End If
If (IsEmpty(Range("'Invoice Sheet'!C23").Value)) Then
GetApplicableCell = "C23"
End If
If (IsEmpty(Range("'Invoice Sheet'!C24").Value)) Then
GetApplicableCell = "C24"
End If
End Function
It can be called by a =GetApplicableCell() from within the worksheet, and we can work with this to produce a workable solution.

Lock Select Box Size In Excel

This seems like a simple thing, but I cannot figure it out, or find it online.
If I select 5 cells in a column(say A1:A5), and I would like to move this selection shape(column 1:5) over (to B1:B5); Is there shortcut to do this? Currently I hit the left arrow, and the select box changes size to just B1, and I have to hit shift and select B2:B5. Ideally I would like to discover a hot key that "locks" the shape of the select box.
It has been suggested by colleagues to write a macro, but this is ineffective in many cases. For example what if instead of a column I wanted to do the same thing with a row, or with a different sized shape. It seems likely that excel has this feature built in.
I'm not sure how a macro would be ineffective. I would write procedures similar to what's below, then assign them to hotkeys. Let me know if it works for you.
Option Explicit
Public rowDim As Long
Public colDim As Long
Public putSel as Boolean
Sub togglePutSel()
putSel = Not putSel
End Sub
Sub GetSelShape()
rowDim = Selection.Rows.Count
colDim = Selection.Columns.Count
putSel = True
End Sub
Sub PutSelShape()
Selection.Resize(rowDim, colDim).Select
End Sub
If you want to make it work for whenever you hit the arrow keys, then in your Sheet code, you can use this. You may want to do a quick check that rowDim and colDim aren't 0. The only issue with this is that you'd be stuck with that behavior unless you create a trigger to stop calling PutSelShape. So, I'd suggest one macro (hotkeyed to GetSelShape) to toggle it, and another hotkey for togglePutSel.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If putSel Then
Call PutSelShape
End If
End Sub

Resources