Grouping by year in pivot vba - vba

I'm trying to create a pivot table in vba with a field that groups dates by month/year; the Date field runs successfully, but when I add the code to group it by month/year, it gives me an "object required error". How should I fix this?
With ActiveSheet.PivotTables("APivotTable").PivotFields("Date")
.Orientation = xlColumnField
'Error is here
.LabelRange.Group Start:=True, End:=True, _
Periods:=Array(False, False, False, False, True, False, True)
.Position = 1
End With

try this after End with line.
'Change Range("A5") as per your requirement
Range("A5").Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, True, False, True)

I ran into this problem myself and was looking for a solution. While Maddy's answer is correct, I really dislike the idea of using a hard coded range instead of the OP's approach of using names.
OP's approach was almost correct. Some additional sleuthing led me to realize the issue is that the Group method must be used on the data of the field you are trying to group, and the OP was using the method on the label of the field.
The solution is very easy. Just move the range being grouped by one cell - from the label to the data using Cells:
.LabelRange.Cells(2,1).Group Start:=True, End:=True, _
Periods:=Array(False, False, False, False, True, False, True)

Related

What should be the answer 'true and (true or false) and false'?

this statement 'true and (true or false) and false' should return 'false' but I can see the answer as 'true'.
the result of the (TRUE OR FALSE) is TRUE and then you continue to do the TRUE AND TRUE (which is resulting TRUE) the last operation is TRUE AND FALSE, which lead to FALSE
>>> True and (True or False) and False
False
In short answer is False.
If you are wondering why ? Then here is how it is evaluated:
(True or False) = True (Since if either is True, result will be True)
Now you are left with True and True and False = False (since "and" is used, if all are True, then only complete statement can be True, but here, one of them is False, so the whole statement will be False.
For better understanding you can use python interactive mode and experiment various different combinations to understand how it work.

Creating a Pivot table macro - Need to remove the marked rows through code

I want the marked rows (subtotals) gone, here is the code for my Pivot table (I am coding a macro) any suggestions?
See picture here
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=ActiveSheet.UsedRange).CreatePivotTable TableDestination:="", TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(1, 1)
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="Kostenstellen-Nr"
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="Konto-Nr"
ActiveSheet.PivotTables("PivotTable1").AddFields RowFields:="Betrag Brutto"
ActiveSheet.PivotTables("PivotTable1").PivotFields("Kostenstellen-Nr").Orientation = xlRowField
ActiveSheet.PivotTables("PivotTable1").PivotFields("Konto-Nr").Orientation = xlRowField
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables("PivotTable1").PivotFields("Betrag Brutto"), "Sum of Betrag Brutto", xlSum
You are talking about subtotals, you record a macro and change it via the top ribbon (PivotTable Tools/Design).
Add this line to your code :
ActiveSheet.PivotTables("PivotTable1").PivotFields("Kostenstellen-Nr").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)

Excel disconnects from clients on autofilter

In my Excel spreadsheet I have a table of data set up
I also have two buttons
One filters the fourth column to anything above 0
With ActiveSheet
.Range("A138:H246").Select
Selection.AutoFilter Field:=4, Criteria1:=">0", Operator:=xlAnd
End With
The other (is supposed to) remove the filter
With ActiveSheet
.Range("A138:H246").Select
Selection.AutoFilter Field:=4
End With
However, the button to remove the filter doesn't work, I keep receiving the following message
"The object invoked has disconnected from its clients autofilter"
I have tried recording a macro and then using that code but even that fails if i run the code on a button press
Any ideas?
I suggest you use the below statement when you've finished with the filtered data.
Hope this helps?
Activesheet.AutoFilterMode = false
As you say this can give errors if autofilter is already false when you try and make it false, however if you position this line in your code at the right place the filter will always be true when you want to set it to false. as below
ActiveSheet.Range("someRange").AutoFilter Field:=27, Criteria1:="test"
' do something with filtered data
ActiveSheet.AutoFilterMode = False
This executes with no errors for me...
Mine says it should be as simple as this :
ActiveSheet.ShowAllData
It just clear all filters, but it doesn't deactivate the AutoFilter mode, is it enough?
Here is the code to scan all used columns and store there hidden state before displaying all of them to use ShowAllData clear filters and rehide previously hidden columns.
Dim TH()
ReDim TH(ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column)
For j = LBound(TH) + 1 To UBound(TH)
TH(j) = ActiveSheet.Columns(j).EntireColumn.Hidden
ActiveSheet.Columns(j).EntireColumn.Hidden = False
Next j
ActiveSheet.ShowAllData
For j = LBound(TH) + 1 To UBound(TH)
ActiveSheet.Columns(j).EntireColumn.Hidden = TH(j)
Next j
If you need to change the AutoFilter mode :
Activesheet.AutoFilterMode = False
Activesheet.AutoFilterMode = True
But this can easily generate errors regarding the current state of that mode in your sheet. (if it is already in the state you are trying to set, excel don't like that...)

shapes.addpicture gives 1004 error

I have got a fairly simple sub, but it doesn't seem to work:
Sub Test()
Dim p as object
strpath = "http://www.someurl.com/sample.jpg" ' some jpg
Set p = ActiveSheet.Shapes.AddPicture(strpath, False, False, 100, 100, 86, 129)
End Sub
When I do this it gives me runtime error 1004, specified value out of range. However, if I change the second "false" to "true", it works fine and inserts the picture. According to this the second true/false affects whether the picture will be saved with the document. I would not like this to happen.
Anyone have any idea why this happens?
This worked for me:
Sub sof20301212ShapesAddpicture()
Dim strPath
Dim p As Object
strpath = "http://www.google.com/images/srpr/logo11w.png"
Set p = ActiveSheet.Shapes.AddPicture(strPath, msoCTrue, msoTrue, 100, 100, 86, 129)
End Sub
Ref Shapes.AddPicture method:
'Declaration
FunctionAddPicture ( _
Filename As String, _
LinkToFile As MsoTriState, _
SaveWithDocument As MsoTriState, _
LeftAsSingle, _
TopAsSingle, _
WidthAsSingle, _
HeightAsSingle _
) AsShape
Filename: The file from which the OLE object is to be created.
LinkToFile: Microsoft.Office.Core.MsoTriState
The file to link to. Can be one of these MsoTriState constants:
msoCTrue
msoFalse To make the picture an independent copy of the file.
msoTriStateMixed
msoTriStateToggle
msoTrue To link the picture to the file from which it was created.
SaveWithDocument
Type: Microsoft.Office.Core.MsoTriState
Required MsoTriState. To save the picture with the document.
LinkToFile and SaveWithDocument cannot be both false, as nowhere to save image.
LinkToFile and SaveWithDocument pair can be:
true, true
true, false
false, true
I would only like to add another possible reason and solution. I also got the Error 1004, specified value out of range.
I had object library enabled and all this stuff. Finally I realized that earlier in my code there is a line which locks the worksheet. After unlocking it everything worked fine.

Microsoft.Office.Interop.Excel VB.NET How to disable select locked values in a worksheet

I am using the Microsoft.Office.Interop.Excel library in VB.Net on an excel sheet that is protected. All of the protected settings are working fine except that I cannot set Select Locked Cells which shows when you are using the Excel protect sheet interface but I cannot see how to set this to false using the library. I have tried all of the different values in the protect method but none of them toggle the value.Excel protected sheet menu
Below is the code snippet I am using to get my workSheet, populate some values, and then protect the sheet. GetWorkSheet is an internal method which will return a WorkSheet object, and invoiceData is a datatable which has the data which is being added to the spreadsheet.
Dim newSheet As Worksheet = getWorkSheet(newSheetName)
' Make the current Work Sheet active so that it will be accepting the data.
newSheet.Activate()
newSheet.Unprotect(sheetPassword)
For i As Integer = 1 To invoiceData.Rows(0).ItemArray.Count
newSheet.Cells(transactionalRow, i) = invoiceData.Rows(0).ItemArray(i - 1)
Next
'work is done so protect it again
newSheet.Protect(sheetPassword, False, True, False, True, True, True, True, True, True, True, True, True, True, True, True)
excel.ActiveWorkbook.Save()
It's very confusing because that property is not set with the Protect statement.
After calling your protect statement, issue the following call:
ActiveSheet.EnableSelection = xlNoRestrictions
See this page for the other values for EnableSelection:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.enableselection(VS.80).aspx
You can try recording a macro and setting it to see what kind of code it generates. I used to do that when I was working with the Interop libs.