Basically I made a macro which does a few copy-paste, and change the filter on the pivot table. Now, there are quite a few pivots, with the same filter to be changed (a date), so I want to make a variable for them.
So the macro works without variables, but when I put:
Dim x,y as Date
x = "2020/9/1"
y = 2020/8/20"
And then put it into the pivot name -I cant copy the full code, nor the parts, because they are on virtual deskop for work.
Activesheet.PivotTables("pivottable3").PivotSelect "'2020/9/1' text" _
xlDataAndLabel, True
and I changed this to:
Activesheet.PivotTables("pivottable3").PivotSelect "'x' text" _
xlDataAndLabel, True
and I get an error saying this name doesn't exist. What did I do wrong?
Related
In Formulas->Define Name->Apply Names, I have thousands of custom named cells in the listbox. It is inefficient to select them allone by one. Is there a way, by setting in Excel or VBA code, to select them all? CTRL+A and SHIFT PgUp/Dn doesn't work.
In UserForm_Initialize(), I applied Cells.ApplyNames but error says 'Runtime error 1004, Microsoft Excel cannot find references to replace'
The documentation at msdn.microsoft.com says
Names: An array of the names to be applied. If this argument is omitted, all names on the sheet are applied to the range.
see https://msdn.microsoft.com/en-us/library/office/ff196578(v=office.15).aspx
Following on that try this:
Selection.ApplyNames _
IgnoreRelativeAbsolute:=True, UseRowColumnNames:=True, _
OmitColumn:=True, OmitRow:=True, Order:=1, AppendLast:=False
This should work depending on the Scope of the Defined Names and their visibility. Try that and let us know the results.
I found that combining a for loop with the ApplyNames Function worked for me.
Sub ApplyNames()
On Error Resume Next
Count = ActiveWorkbook.Names.Count
For i = 1 To Count
Name = ActiveWorkbook.Names.Item(i).Name
Cells.applynames Names:=Array(Name), IgnoreRelativeAbsolute:=False
Next i
End Sub
It counts the number of names then loops through them individually.
For some reason leaving the names variable empty didn't work in my situation, I think it was because there were errors in some of the names.
I am extremely new to vba code/coding of any sort, and am trying to develop a loaning system for equipment within my discipline using excel sheets and forms.
Essentially, I want a cmdbutton_click to update the availability status of the specific piece of equipment to "On Loan" within the inventory list spreadsheet. The problem I am having however, is with the fact that the form user will enter the ItemID (number) which obviously will always be different. The item ID is the value of which I am looking to use the Match function with.
I have tried to refer to the form's ItemID text box using the match function, but it does not work whatsoever - I understand this is probably completely incorrect.
Set ws2 = Worksheets("MajorInventoryList")
ws2.Cells(WorksheetFunction.Match(me.txtID.value), 10) = "On Loan"
This brings about the Compile error: 'Argument not optional'
Is there any way to ameliorate this?
Use Find like this:
Set ws2 = Worksheets("MajorInventoryList")
Set fnd = ws2.Range("A:A").Find(Me.txtID.Value)
If Not fnd Is Nothing Then ws2.Cells(fnd.Row,10).Value = "On Loan"
This will look for Text Input in Column A and if found, will place "On Loan" in column J of that row in which the value exists.
I am enhancing a work template that requires users to create a new file each week to be able to track progress from week to week, so the file will change name each week.
This file has about 3 charts in a "Summary" worksheet.
In this "Summary" worksheet, bar chart #2 currently has 5 series. I am writing a script to be able to add a 6th series.... this I have no problem.
I would like the new Series values to use the values from a Name Range of cells the template currently has defined ("software") in a different worksheet.
I would also like the Horizontal Axis Labels to use a different Name Range of cells that template also has defined ("dates") in a different worksheet.
Here is the code I have so far:
Sub add_software()
'Update software Trend in Summary
Sheets("Summary").SelectActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.SeriesCollection.NewSeriesActiveChart.FullSeriesCollection(6).Name = "='Summary'!$C$44"
'MY PROBLEM IS HERE
ActiveChart.FullSeriesCollection(6).Value = ActiveWorkbook.Sheets("Summary").Range("Software")
ActiveChart.FullSeriesCollection(6).XValues = ActiveWorkbook.Sheets("Summary").Range("dates")
End sub
I would expect that the script will use the values in these Name Ranges (which are the same values used in the other 5 series) and populate with the values from the name range.
This is the error I get is:
Run-Time error '438': Object doesn't support this property or method
error
Try this other method to get the range. Not tested but it should work.
ActiveChart.FullSeriesCollection(6).Value = ThisWorkbook.Names("Software").RefersToRange
ActiveChart.FullSeriesCollection(6).XValues = ThisWorkbook.Names("dates").RefersToRange
Edit: Had an extra dot after "Names".
The accepted solution by #RicardoA works fine, but it assumes a static definition of the names, and places the current address of each name into the SERIES formula of the series. If the names are dynamic, the SERIES formula will not keep up with the changes. The following puts the names not addresses of the names into the series formula:
ActiveChart.SeriesCollection(6).XValues = "='" & ThisWorkbook.Name & "'!dates"
ActiveChart.SeriesCollection(6).Values = "='" & ThisWorkbook.Name & "'!software"
u have a typo. u have miss a "
where u have
.Range("Software)
replace to
.Range("Software")
good luck
My goal is to change the filter of 3 pivot tables, all with the same field with the click of a button attached to a macro.
Here is my code:
Dim pickedDate As String
Dim shift As String
pickedDate = Worksheets("Report").Range("C1").Value
shift = Worksheets("Report").Range("C2").Value
Worksheets("Report").PivotTables("PivotTable2").PivotCache.SourceData = Worksheets("Fullness").Range("A1").CurrentRegion.Address(, , xlR1C1, True)
Worksheets("Report").PivotTables("PivotTable3").PivotCache.SourceData = Worksheets("Backed").Range("A1").CurrentRegion.Address(, , xlR1C1, True)
Worksheets("Report").PivotTables("PivotTable4").PivotCache.SourceData = Worksheets("Hoist").Range("A1").CurrentRegion.Address(, , xlR1C1, True)
For Each PT In ActiveSheet.PivotTables
PT.PivotFields("Date").CurrentPage = CDate(pickedDate)
PT.PivotFields("Shift").CurrentPage = shift
Next
However when I get to my loop for the pivot tables, I get the error on PT.PivotFields("Date").CurrentPage = CDate(pickedDate)
:
Run time error 1004
Application Defined or Object Defined Error
I've verfied the source data for the tables is updated correctly.
I've tried not using the loop, and doing it manually like
Worksheets("Report").PivotTables("PivotTable4").PivotFields("Date").CurrentPage = CDate(pickedDate)
for each table, and it only works for one of the tables, my PivotTable3.
Even more curiously, when I comment out my code to change the date, and use either way (the loop or the manual change for the 3 tables) for the shift change it works perfectly with no error.
I've verfied the name of my pivot tables, that's not the issue. The name of the field I'm trying to change appears correct as well- it works for one of the tables and I've copied and pasted that cell "Date" into the others even.
What am I missing?
I'm not completely sure why this works now, but all I did to change my code was add into the loop a clear filter command for the "Date" field.
So now my code has this loop instead for making the date and shift change for all the pivot tables based on a cell value I enter manually, just once
For Each PT In ActiveSheet.PivotTables
PT.PivotFields("Date").ClearAllFilters
PT.PivotFields("Date").CurrentPage = CDate(pickedDate)
PT.PivotFields("Shift").CurrentPage = shift
Next
Again, I'm not sure why I didn't have to do this for the shift filter, but now it works. I suggest trying that first if anyone encounters this issue
I have code that creates pivot tables then populates another workbook with data. It opens a workbook that acts as a template for the data, populates it, saves and closes. It does this 8 times for 8 different sets of data. I've run this code many times before without issue and seemingly now it starts to error out with: "Cannot change part of a data table" on the second set of data (the first one works). The section that it is erroring out on is...
'Transfers Repairs Data
Range("D27").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-1],'[VBA Project.xlsm]Repairs Pivot'!R5C8:R60C11,4,FALSE)"
Selection.AutoFill Destination:=Range("D27:D30"), Type:=xlFillDefault
Range("D27:D30").Select
Range("D85").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-1],'[VBA Project.xlsm]Repairs Pivot'!R5C8:R60C11,4,FALSE)"
Any idea whats causing this?
Not really an answer but an improvement to help you.
'Transfers Repairs Data
Range("D27:D30").FormulaR1C1 = _
"=VLOOKUP(RC[-1],'[VBA Project.xlsm]Repairs Pivot'!R5C8:R60C11,4,FALSE)"
Range("D85").FormulaR1C1 = _
"=VLOOKUP(RC[-1],'[VBA Project.xlsm]Repairs Pivot'!R5C8:R60C11,4,FALSE)"
Also be sure you're on the right sheet and as others commented that there's no data table where you're trying to paste these formulas. To use a particular sheet you can alter the above:
Sheets("Sheet1").Range("D27:D30").FormulaR1C1