Update Excel Pivot Table Fields - vba

I compile morning reports every day using a workbook with a number of different sheets and pivot tables. So far, I have been able to somewhat automate the data retrieval process, and getting the pivot table data to refresh was very simple. What I would like to do now is get the pivot table field filters to change automatically to include the data entries for thirty days prior up to and including today's date. I have seen some things out there that let you select just one date, but I need to select a range. I have tried using a relatively recorded macro, but it gives me this
Sub Update()
Sheets("Doorline Month").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveSheet.PivotTables(-1).PivotFields("Date").CurrentPage = "(All)"
With ActiveChart.PivotLayout.PivotTable.PivotFields("Date")
.PivotItems("1/14/2018").Visible = False
.PivotItems("2/15/2018").Visible = True
End With
End Sub
I believe that this would only deselect 1/14/2018 and make 2/15/18 visible. I want this code to work automatically every morning, so if I come in on 2/16/2018 it deselects 1/15/2018 and selects 2/16/2018. I have tried replacing the dates in the recorded macro with "ActiveCell" but to no avail. Any help is greatly appreciated. If it helps, the data for my pivot tables is in sheet 1 and the dates specifically are in column A. Unfortunately, the number of data entries per day is not consistent so I don't know if that complicates things or not.

Why dont you put a flag in the database/ data of origin that marks with 1 or 0 each line depending if the date is within the range you want or not? Then you can leave the 1 or the 0 selected in the pivot table's filter field and the relevant data would show automatically every time you refresh.

Related

Reference a field from Subform as a Variable in VBA

I have a form [form1] with a subform [subForm1].
[subForm1] contains a data table with fields "day", "month", and "year".
In [subForm1], I have 3 textbox "dayText", "monthText", and "yearText" that each have a control source from the corresponding fields from the data table.
I am trying to write a macro that uses the "year" field value from the row that is currently selected in the subform. So if I select the first row and run the macro, I want the macro to use the value "2019".
day month year
1 1 2019
2 3 2017
This is what I have tried so far and this is the error I get. I appreciate any inputs. Thanks
Declare yearStr as String
yearStr = Forms![form1].[subForm1].Form!yearText
Error - Cant't find the field '|1' referred to in your expression.
Edit: I found that Me.CurrentRecord will give me the index of the row, but I'm not sure how to use that to get the value of a specific column.
Me![year] ended up working. Was a lot easier than I expected.

VBA macro for excel. Mark row if cells in column A are the same but different in column C

I do not have any code yet as I dont know VBA that much or at all.
I got excell sheet with 4 columns. Column A is main group and C is subgroups within groups from column A.
i need to mark somehow rows where within same JobID, WFID is the same and where WFID is different within same JobID.
JobID TaskID WFID
39822 913914 Complete
39822 913915 no complete
37941 905439 Complete
37941 905440 Complete
Would you be able to help pleasE?
You do not really need macros to identify your rows.
Simply put this formula in a new column
=COUNTIFS($A$1:$A$8,A1,$C$1:$C$8,"<>"&C1)
Wherever there are different values in col C for same value of col A it will throw a 1 else 0
If you want to highlight, I recommend using conditional formatting based on the value of your new column with (0/1) values.
Hope this answers your query.

Cross worksheet Sql query taking time?

I have around 10 thousand records(rows) in one sheet A and around a thousand in sheet B. Both have 5 columns. I need to filter sheet A using column filters and then find everything which is not on sheet B using the ID in both the sheets. Eg: Sheet A has 10 thousand , apply filter and it comes to 1200. Now select the records which are not in Sheet B. I have written a query for it.
rs.Open "SELECT * FROM [A$] WHERE ([Class]<>'Other' OR...) AND ([Sub Class]='Good' OR...) AND [ID] NOT IN (SELECT [B$].ID FROM [B$])"
Query runs extremely slow and I don't know if I will even get any answer waiting hours, may be it is wrong too. I checked and found that if I exclude the last AND , the query executes in a second. Any help is appreciated. Thanks.
Why would you use a SQL query within an Excel workbook if you're not querying a database? Wouldn't VLOOKUP be much simpler?

Automatically sort data-bound pivot table in two dimensions in Excel 2007 using VBA

Here's an annoying issue. I've got a pivot table in an Excel spreadsheet which gets its data direct from a SQL server query.
The table has customers on the vertical axis, and dates on the horizontal access. Both need to be correctly sorted - i.e. customers as alphabetical top to bottom and dates in date order from left to right.
I've ensured that the data coming out of SQL is recognised by EXcel as a date field. You can sort the dates successfully using the manual A-Z function. But I need to do it automatically using VBA.
I had hoped (againt hope) that using two sorting parameters on the SQL query might do the trick:
sql = "SELECT * FROM myView ORDER BY Customer, Date"
Set pt = Worksheets("MyReport").PivotTables("MyPivot")
pt.PivotCache.CommandText = sql
pt.RefreshTable
But it doesn't.
I see than in Excel 2010 onwards there's a handy AutoSort function that should do what I need. But I'm stuck with 2007. Is there a way to sort my data in both dimensions?
Turns out there is an AutoSort feature in Excel 2007, but it belongs to the PivotFields object, rather than the pivot table itself.
Dim pt As PivotTable
Set pt = Worksheets("myWorksheet").PivotTables("myPivotTable")
pt.PivotFields("DATE").AutoSort xlAscending, "DATE"

Excel: Selecting Specific Columns and Rows in Pivot Table and Formatting

I'm using a Pivot Table in Excel 2010, and while searching posts I find that a lot of users are frustrated like me because it doesn't keep all formats.
So what I'm trying to do is run a macro that formats columns in a Pivot table, but limited to the last row and column in the table. I have the formatting info, but I just need to know how to apply it to specific columns and rows.
What I was thinking might work is finding the last column of the Values row, in this case "Stops per Rte" which is the last Values column; however, I have months listed at the top, so it repeats across months. If the user filters only certain months then the # of columns will decrease.
Same goes for the # of rows: of course, the user should be able to expand/collapse rows as needed, so I only want the column format to go to the last row or just above "Grand Total", if possible.
Hopefully, this makes sense. Thanks in advance! = )