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

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.

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.

What are the reasons UnMerge will not work in Access VBA?

I have a VBA macro in Access 2016 (actually, written for earlier version, but right now I'm working with 2016). What it does is adjusting several Excel spreadsheets, and moving them to another location, while also writing some logs into SQL table.
One of adjustments is unmerging some merged cells. The weird part is, those excel files fail to process at times (not always), when this Access DB is triggered programmatically (from another macro in another Access DB). When I launch it manually, or go into debug step-by-step, it always works - so I am confused, and can't find the reason.
I tried to replace "MergeCells = False" for "Unmerge" - it's the same. I also tried to make new ACCDE file - no result.
This is part of the code, where the unmerging occurs.
xlsh.Select
xlsh.Range("A1:D1").Select
xlsh.Outline.ShowLevels RowLevels:=2
With Selection
.UnMerge
End With
xlsh.Range("A1").Value = "Alfa"
xlsh.Range("B1").Value = "Bravo"
xlsh.Range("C1").Value = "Golf"
xlsh.Range("D1").Value = "Tango"
xlsh.Range("A1").Select
If xlsh.Range("A1").Value = "" Then GoTo error
When it fails to unmerge the cell, the value won't get assigned, and at the end it's empty, thus, continues to label 'error'.
Try this one:
xlsh.("A1:D1").Unmerge
Instead of
xlsh.Select
xlsh.Range("A1:D1").Select
xlsh.Outline.ShowLevels RowLevels:=2
With Selection
.UnMerge
End With
The last 2 lines are unnecesary as:
If xlsh.Range("A1").Value = "" Then GoTo error
Will not be true as you are assigning a value a few lines before. (xlsh.Range("A1").Value = "Alfa")

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

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

Use VBA to suppress Analysis Toolpak Histogram function messege

Question overview:
I am using Excel VBA histogram function from 'Analysis Toolpak' to generate approximately 25 histograms automatically. When Histogram graph is generated, it is placed on top of cells that have values in it, effectively hiding them (Which is OK with me). Therefore a following message is generated "Histogram - some data will be hidden by embedded chart(s)" with "OK" & "Help" buttons. I don't want to press "OK" 25 times whenever I am running this macro.
What I have tried:
Application.DisplayAlerts = False/True is not working out for me. I have tried placing this in variaty of location in my code
Application.ScreenUpdating = False/True
Also tried playing with SetWarning function
Code (1/25):
Dim binrng As Range
Set binrng = Sheets("PSDreport").Range("P4:P64")
Dim outputrng As Range
Set outputrng = Sheets("PSDreport").Range("Q3")
Application.Run "Histogram", inprng, outputrng, binrng, False, False, True, False
My partial solution:
With Application
//CODE GOES HERE//
.SendKeys "{ENTER}"
End With
Problem with my current solution:
Note that all Histogram generating segments of code (1/25) are wrapped around the 'With'. For some reason the first Histogram generated still produces the pop-out (Not good). the remaining 24 successfully skip the pop-outs but the noise of the pop-outs is still produced (slight annoyance).
I am looking for a more elegant way to solve this problem
I had this warning too when I tried to output my embedded chart on the same worksheet that I had my source data (your "imprng"). Once I moved my output range (outputrng) to a different worksheet, the warning stopped.
Bit late to the party but anyone else looking at this should try
With Application
.SendKeys "{ENTER}"
//CODE GOES HERE//
End With

Excel VBA Function stops execution before completion without error message

I set out to write a simple function to determine the length of a string in points. Having googled around I decided to avoid the font metrics problem by having excel do the work for me.
Here is the code.
Option Explicit
Function txtWidthPts(MyText As String) As Integer
'A cell on a working worksheet has been named "WidthTest" for easy reference & to ensure data is not overwritten.
'set WidthTest word wrapping off so that strings placed in there aren't wrapped
Application.ScreenUpdating = False
With [WidthTest]
.WrapText = False
.Value = MyText
'autofit WidthTest
.Columns.AutoFit
'get the width of the column
txtWidthPts = .Width
.ClearContents
End With
End Function
I tested the function by placing it in a cell on a working worksheet thus:
=txtWidthPts("Test123")
When I have this working I will be using it in code not as a worksheet function.
My problem is that the function does not throw an error and stops execution on the line:
.Value = MyText
I have placed the code and name into an empty workbook to ensure no interaction with other workbook contents / code.
I have searched extensively and tried various suggestions (DoEvents, Application.Update = False, etc, etc.) to no result.
I have cleared all breakpoints, closed and opened the workbook & restarted. I have tested with options set to Break on All Errors.
No result.
I suspect I am missing something obvious but it has me beat at the moment.
Any and all suggestions will be most welcome.
So, #YowE3K was right on the money. After fixing the error in the original code (Corrected code above) this runs fine from vba. I knew I was missing something obvious.
Curiosity sub-question: the function works as desired and indeed, as #YowE3K observed, it does not modify the Excel environment. However the result returned is dependent on it appearing to have modified the Excel environment. Seriously WTF. Just wanting to understand.
Thanks again YoWE3K.

Resources