DIYnot
Local | Network
   DIYnot > Forums
Local | Network
DIYnot Network Local DIYnot Network Local  
  Forum IndexForum Index     RulesRules    HelpHelp     Join FREERegister Free     BookmarksBookmarks     Watched TopicsWatched Topics     SearchSearch     LoginLogin 

Excel VBA help


 
Post new topic   Reply to topic    DIYnot.com Forum Index > Software
  Bookmark and Share View previous topic :: View next topic  
Author Message
amatureleccy

from United Kingdom

Joined: 29 Dec 2003
Posts: 36
Location: London,
United Kingdom
Thanked: 0 times

PostPosted: Thu Jan 27, 2005 9:19 pm    Post Subject:
Excel VBA help
Reply with quote Thanks

I recorded a macro of the creation of a pivot table.

The bit of code that does the business is shown below.

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:="'ALL'!R1C2:R1655C7").CreatePivotTable TableDestination:="", _
TableName:="PivotTable1"

My problem is that the SourceData uses fixed cell addresses R1C2 and R1655C7.

I regularly add rows to the data and want rebuild the pivot table. I therefore want to have the code use the values from a variable containing the address of the last cell.

I don't know how to write the SourceData bit so that it uses the variables.

Any help would be greatly appreciated.
Back to top
 Alert Moderators

If you don't want to see this advert, click here to login or if you are new click here to join free.
TexMex

from United Kingdom

Joined: 08 Jul 2004
Posts: 1523
Location: Lincolnshire,
United Kingdom
Thanked: 0 times

PostPosted: Fri Jan 28, 2005 2:35 am    Post Subject:
Reply with quote Thanks

I would suggest that you choose the last entry on column 1 of your source data sheet to indicate the length of your table. You could then insert this into the rest of the hard coded range. So something like the following:


'Declare variables
Dim TableLength As String
Dim FullRange As String

'This next line returns the last row number of column 1
TableLength = Worksheets("'ALL'").Rows(1).End(xlDown).Row

'This line inserts the Row found above into the full Range that you want
FullRange = "'ALL'!R1C2:R" & TableLength & "C7"

'This line is a copy of your original line BUT
'Substitutes the variable "FullRange" for your original hard coding

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=FullRange).CreatePivotTable TableDestination:="", _
TableName:="PivotTable1"


Something that worries me, however, is that you appear to have appostrophies in the name of your worksheet (ie 'ALL' ) My version of excell (2000) doesn't allow this.
Back to top
 Alert Moderators
pipme

from United Kingdom

Joined: 24 Feb 2004
Posts: 4046
Location: Somerset,
United Kingdom
Thanked: 0 times

PostPosted: Fri Jan 28, 2005 3:32 am    Post Subject:
Reply with quote Thanks

Ever tried playing with 'array functions' ?
the function below effectively 'looks' down col from L4 to L1000, if the word 'pet' is found then the corresponding value in col M is added, else if not 'pet' then '0' is added, to form a sum of all 'pet' values ... I think 'pet' could be a fixed ref cell hence have a variable 'word'.

{ SUM ( IF ( (L4:L1000 ) = "pet" , ( M4:M1000 ) , 0 ) ) }

The 'curly brackets', denoting array function are inserted by <ctrl> <Enter> to complete the entry and insert into cell.

Not an answer to the problem set, but worth a thought when designing a spreadsheet.
P

__________________
Beware of little expenses; a small leak will sink a great ship. (Ben Franklin)
Back to top
 Alert Moderators
amatureleccy

from United Kingdom

Joined: 29 Dec 2003
Posts: 36
Location: London,
United Kingdom
Thanked: 0 times

PostPosted: Mon Jan 31, 2005 10:28 pm    Post Subject:
Reply with quote Thanks

Cheers TexMex you've carcked it for me.

And that array stuff - why didn't I think of that - more for me play with.

Cheers to you both.
Back to top
 Alert Moderators
pipme

from United Kingdom

Joined: 24 Feb 2004
Posts: 4046
Location: Somerset,
United Kingdom
Thanked: 0 times

PostPosted: Tue Feb 01, 2005 5:50 am    Post Subject:
Reply with quote Thanks

icon_wink.gif
Excel is a fab program ..... Loads of M$ help and links to be googled for the huge amount of freeby stuff which exists.

Drop in here :-
http://j-walk.com/ss/excel/index.htm
Follow the 'links'.

P icon_biggrin.gif

__________________
Beware of little expenses; a small leak will sink a great ship. (Ben Franklin)
Back to top
 Alert Moderators
Display posts from previous:   
  Bookmark and Share View previous topic :: View next topic  
Post new topic   Reply to topic    DIYnot.com Forum Index > Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Similar Topics   Replies   Views   Posted 
Excel: Lookup problem 6 680 Tue May 06, 2008 10:06 am
EXCEL 4 540 Thu Dec 27, 2007 12:26 pm
Undo read only excel file 6 600 Mon Nov 12, 2007 1:31 pm
Help with Excel macro please 2 440 Tue Nov 06, 2007 9:50 pm
MS Excel question 14 660 Sat May 31, 2008 9:43 am

Excel Satin Chrome 2 Gang 2 Way Switch - White Insert Excel Satin Chrome 2 Gang 2 Way Switch - White Insert£7.21Excel Satin Chrome 4 Gang 2 Way 400W Push Dimmer Switch
Excel Satin Chrome 4 Gang 2 Way 400W Push Dimmer Switch£42.31
Excel Satin Chrome 1 Gang Extension Telephone Socket - Black Insert Excel Satin Chrome 1 Gang Extension Telephone Socket - Black Insert£6.60Excel Satin Chrome 1 Gang Intermediate Switch - White Insert
Excel Satin Chrome 1 Gang Intermediate Switch - White Insert£9.86
Excel Satin Chrome 1 Gang Satellite Socket - White Insert Excel Satin Chrome 1 Gang Satellite Socket - White Insert£9.90Excel Satin Chrome 2 Gang Blanking Plate
Excel Satin Chrome 2 Gang Blanking Plate£7.60



DIYnot
Find an Expert | Find a Supplier | Search DIYnot.com
Network | Advertising | Newsletter
DIY | DIY How To | @home | DIY Wiki | DIY Forum
By using this site you agree to our Terms of Service / Disclaimer.
Please read our Privacy Policy.