I'm trying to write a macro for Excel in which I create a PivotTable from a set of source data. The only problem is the source data will be continually changing, so I have to let the macro know to always select the usedrange. Unforunatley, the way the PivotTable selects the information isn't based on a range, but rather rows and columns. I tried creating a variable that counted the number of rows, but I don't think that would work. I was wondering if anyone could give me a hint on how to make this work. I originally recorded the macro and thought I could just insert the usedrange/variable in it, but it didn't turn out to be that simple. Here's a copy of my code:
Sub PT()
'
' PT Macro
' Macro recorded 4/20/2006 by prainey
'
Dim p As Integer
p = Sheets("Sales and Profits").UsedRange.Rows.Count + 1
'
ActiveSheet.UsedRange.Select
Application.CutCopyMode = False
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"'Sales and Profits'!R1C1:R8C6").CreatePivotTable TableDestination:= _
"'[project.xls]Table and Chart'!R1C1", TableName:="PivotTable2", _
DefaultVersion:=xlPivotTableVersion10
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Date")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Product Name")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Profit"), "Sum of Profit", xlSum
Sheets("Sales and Profits").Select
End Sub
Any help would be appreciated.
P.s. I bolded the part of the code that is the source range for the PivotTable.
Damn VBA Macro!!! (Help Please)
- bassist_25
- Senior Member
- Posts: 6815
- Joined: Monday Dec 09, 2002
- Location: Indiana
Damn VBA Macro!!! (Help Please)
"He's the electric horseman, you better back off!" - old sKool making a reference to the culturally relevant 1979 film.
- DirtySanchez
- Diamond Member
- Posts: 4186
- Joined: Tuesday Feb 14, 2006
- Location: On teh internetz
- Contact:
I would love to help but can't, so I will at least cheer you up with a story of how low tech I am. I saw a truck 4 sale that I really liked. I stopped looked at it and the owners # was on the windshield. I have a cell phone
and only know how to use the camera and make calls. I do not know how to enter #'s and save them. I had no pen on me so I took a picture of the windshield. I showed my wife (mistake) and haven't heard the end of it since. So it sounds like you are Quite technologically advanced, maybe I'll come to a BadDaze show so you can help me use my cell phone.
P.S. good luck with the pivot table macro with variables and if all else fails use duct tape, that stuff is amazing.
and only know how to use the camera and make calls. I do not know how to enter #'s and save them. I had no pen on me so I took a picture of the windshield. I showed my wife (mistake) and haven't heard the end of it since. So it sounds like you are Quite technologically advanced, maybe I'll come to a BadDaze show so you can help me use my cell phone.
P.S. good luck with the pivot table macro with variables and if all else fails use duct tape, that stuff is amazing.
"You are now either a clueless inbred brownshirt Teabagger, or a babykilling hippie Marxist on welfare."-Songsmith
- bassist_25
- Senior Member
- Posts: 6815
- Joined: Monday Dec 09, 2002
- Location: Indiana
*LOL* Not a problem. I actually got it to work. I just had to place my &'s and paranthesis in the rights places. Now I'm trying to get a macro to work that runs a solver for a linear program.
I'll definitely help you out on the cell phone, though.
I'll definitely help you out on the cell phone, though.

"He's the electric horseman, you better back off!" - old sKool making a reference to the culturally relevant 1979 film.
- ToonaRockGuy
- Diamond Member
- Posts: 3091
- Joined: Tuesday Dec 17, 2002
- Location: Altoona, behind a drumset.
- tornandfrayed
- Diamond Member
- Posts: 1761
- Joined: Tuesday Dec 23, 2003
- Location: The Jaded Empire
- Contact:
No really
No really Paul what do you do for fun!
Torn & Frayed
One World, One Voice, One God!
Music is LIFE!
One World, One Voice, One God!
Music is LIFE!
- bassist_25
- Senior Member
- Posts: 6815
- Joined: Monday Dec 09, 2002
- Location: Indiana
I'm afraid that Visual Basic is just too limited of a language to be able to program something so capital. I'd need to at least program that in Cobol.ToonaRockGuy wrote:Actually, Paul, you need to focus on a macro that can merge the outstanding properties of Beans, oLd sKool, and Snickers with the George Foreman Grill.
That's just freaky.Tornandfrayed wrote:No really Paul what do you do for fun!

"He's the electric horseman, you better back off!" - old sKool making a reference to the culturally relevant 1979 film.