+ Reply to Thread
Results 1 to 59 of 59

Sub or Function not Defined: Generating Pivot tables using VBA Code

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Sub or Function not Defined: Generating Pivot tables using VBA Code

    Hi everyone. I am trying to create a pivot table using VBA code. I am reciveing an error, in the code though, but atually cannot pinpoint the problem. Any help would be greatly appreciated.

    Please Login or Register  to view this content.
    Last edited by AnthonyWB; 05-05-2010 at 11:29 AM. Reason: Corrections, updatin

  2. #2
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Generate Pivot tables using VBA Code

    The error is an Invalid Procedure call or argument.

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Generate Pivot tables using VBA Code

    I have modified the code, and fixed some additional errors. However I ham still recieving and Invalid Procedure or Call Argument:

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Exclamation Re: Generate Pivot tables using VBA Code

    I made some additional correction. The code below is where the error occurs:

    Please Login or Register  to view this content.
    I desperately needs some help here.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Generate Pivot tables using VBA Code

    Your tabledestination doesn't include a sheet name.
    Remember what the dormouse said
    Feed your head

  6. #6
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    How would I inlcude the sheet name into the code?

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    I'd use a range object:

    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    I am totally confused, you mean like this ?

    Please Login or Register  to view this content.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    The last line is incorrect:
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Thank you for your help. I am still recieving an error :Compile Error expresseion expected, variable not defined. According to the Immedaite window the problem is here:

    Please Login or Register  to view this content.
    I am assuming everything else is good?


    Please Login or Register  to view this content.

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    You appear to be trying to create a cache in one workbook using a range in another workbook, in which case you need to have the path and names in this format:
    'path here[workbook name here]sheet'!range_Address

    i.e. you need a leading apostrophe, then the path, then a [ then the workbook name then ] then the sheet name then another apostrophe and then an exclamation mark followed by the range address. You appear to be missing most of the punctuation.
    There's nothing wrong with the Version bit.

  12. #12
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Uh ?

    Is there a better approach? The data is in one notebook, but I want the table in another.

  13. #13
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    The path is variable and will depend on fPath and fDate, which are global variables:

    fPath = "L\"

    and fDate is an input from the user.

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    I didn't say you couldn't. I just said your file paths are incomplete. For example, if your source is Sheet1!A1:C100 in Book1.xls in C:\Test, then your SourceData argument needs to be:
    Please Login or Register  to view this content.
    but you are currently using the equivalent of:
    Please Login or Register  to view this content.
    so you are missing all the punctuation. Without knowing exactly what your paths are, you need something like:
    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Thank you, romperstomper, but according to the immediate window

    Please Login or Register  to view this content.

    has an invalid character. It should work though, if corrected?

  16. #16
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    I've just noticed you have a 1 not an l in the enumeration value - it needs to be:
    Please Login or Register  to view this content.
    i.e. XL at the start not X1

  17. #17
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    I corrected a few more errors. The underscore before versions was erroring out in the code. That has been corrected but I am still getting a compile error variable not defined.

    Please Login or Register  to view this content.

  18. #18
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Do you have Option Explicit at the top of your module? If you do, you haven't declared PT or PC in your code as far as I can see.

  19. #19
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    I made the correction now it says that a Named argument is not found?


    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Thanks for all of your help on this ! Yes they have been declared.

    Please Login or Register  to view this content.
    This is a submacro of many others. Option Explicit was declared at the top.

  21. #21
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Oh, I just noticed you are using Add not Create when adding the cache - Add does not have the Version argument so you'll need to remove it.

  22. #22
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Remove it and replace it with Create, or just remove it as in:

    Please Login or Register  to view this content.

  23. #23
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Remove the Version argument:
    Please Login or Register  to view this content.

  24. #24
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    ok likewise with pt?

  25. #25
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Nope, that should still be valid.

  26. #26
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    well pc is now error free, but all of pt errors.

    Please Login or Register  to view this content.

  27. #27
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    What error?

  28. #28
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Subscript out of range, run time error 9

  29. #29
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Then the workbook or worksheet name is incorrect (or both).

  30. #30
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Yep it's incorrect, the path is:

    Please Login or Register  to view this content.
    and the file name is

    Please Login or Register  to view this content.
    with sheet

    name "Borrowings_Portfolio_Bonds"

  31. #31
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Is the sheet name Borrowing_Portfolio_Bonds or Borrowings_Portfolio_Bonds?
    One or other name is wrong and only you can figure out which.

  32. #32
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Funny - there is no s at the end Borrowing_Portfolio_Bonds.

  33. #33
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Again, either the workbook name is wrong (assuming the workbook is open) or the sheet name is wrong.

  34. #34
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    The workbook is not open. Are you saying I need to have it open? Thanks for all your help !

  35. #35
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Variable Not Defined: Generating Pivot tables using VBA Code

    Yes - you can't create a pivot table in a closed workbook!

  36. #36
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I have been working on this for some time, and I am still recieving errors. I now have a sub or function not defined error at the CreatePivotTable line, please see the code below:

    Please Login or Register  to view this content.

  37. #37
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    CreatePivotTable is a method of the PivotCache object, which you have now removed for some reason:
    Please Login or Register  to view this content.

  38. #38
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I am almost there: I now have an invalid procedure call or argument:

    Please Login or Register  to view this content.

  39. #39
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    You don't need the path in the TableDestination since the workbook is open.

  40. #40
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    Man this is tough, so are you saying:

    Please Login or Register  to view this content.

  41. #41
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    Quote Originally Posted by AnthonyWB View Post
    Man this is tough
    To be honest, I'm not sure why you keep completely altering your code - it makes it tricky to work out what got fixed and what got broken in between.

    For the destination it's easier to use a range object, IMO:

    Please Login or Register  to view this content.

  42. #42
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    Creating the pivot is much harder than expected: I now have
    an error showing the a Reference is not valid. I have doubled checked the file path and file name, and sheet name and all are correct.

    Please Login or Register  to view this content.

  43. #43
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    What is the exact error? Does it help if you change A1 to A3?

  44. #44
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I am still getting an error. When I goto Debug run to curser, I am told that "Methid CreatePivotTable" of object "PivotCache' failed.

    When I step into the the error I am told that the "a reference is invalid"

  45. #45
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I think I need to see the workbooks, as we seem to be going round in circles. Can you strip them down a bit, censor any confidential info, and post (with code) please?

  46. #46
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    Sure they are two workbooks.

  47. #47
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I am blocked from uploading files.

  48. #48
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    Who by? Your company, or the site?
    Can you email them?

  49. #49
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I am told that a security token is broken, I am assuming it's my company. Anyways the post is getting out of hand. I'll post the entire code again, and see if there are other errors. It could have been smarter to have recorded a macro, and go from there?

    Please Login or Register  to view this content.

  50. #50
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    Since you have workbook variables, why not use them:
    Please Login or Register  to view this content.
    And yes, it's usually easier to start with a recording and then edit it!

  51. #51
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I recorded a macro and it works, however it fails to incorporate the global variables fpath and fDate which are crucial to the automation.

    Please Login or Register  to view this content.

  52. #52
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    Something like:
    Please Login or Register  to view this content.

  53. #53
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I think we almost got it, we still have an error though: an invalid procedure call or argument.

    Please Login or Register  to view this content.

  54. #54
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    Oops - missing an apostrophe:
    Please Login or Register  to view this content.

  55. #55
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    It's almost done, and I appreciate your help, tremendously. There is an error "Cannot Open Pivot Table Source File "L:\30-apr-2010105'"

    Please Login or Register  to view this content.
    the correct path should be "L\30-apr-2010_157\105_Reports\105.xlsm or as it has already been declared,

    Please Login or Register  to view this content.

  56. #56
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    I based the code on your recorded macro, which uses:
    Please Login or Register  to view this content.
    Since that is (apparently) a sheet in the same file, you should not need a path.

  57. #57
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    The correct path and file name for the source is

    sPath1 = fPath & fDate & "_157\105_Reports\"
    Const sFileInp1 As String = "105.xlsm"

    where we have set Set wb1 = Workbooks.Open(sPath1 & sFileInp1). We then want the range to be R1C1:R11500C48" on a worksheet called 105.

  58. #58
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,257

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    That is not what your recorded code did. If you just need to use a sheet called 105, then use:
    Please Login or Register  to view this content.

  59. #59
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Sub or Function not Defined: Generating Pivot tables using VBA Code

    We Got It !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1