+ Reply to Thread
Results 1 to 5 of 5

VBA code error for assigning a value in "For Each"

  1. #1
    Registered User
    Join Date
    07-23-2021
    Location
    United States
    MS-Off Ver
    365
    Posts
    2

    VBA code error for assigning a value in "For Each"

    I am creating a macro that will take raw data on one sheet and create a pivot table with it on another, but I need to be able to sort by date on the pivot table.

    I originally thought I could fix the issue by applying the date format to the raw data before the pivot table was created:

    Please Login or Register  to view this content.
    but that didnt work, as the pivot table still created that column in general format.

    So after the pivot table was created, I have this mess that keeps getting messier the more I work on it:

    Please Login or Register  to view this content.
    I know that it's unfinished because I am not assigning a value properly, but I dont know what exactly it's asking for. This specific error code gives a 91 With statement error. All I want to do is set pvtTable as the table I am referencing named PivotTable1 and assign the date format to the END DATE field.

    I should mention I am brand new at VBA, like as in I just picked this project up 3 weeks ago. So any and all recommendations to the code and improvements to my process overall are welcome, but dumb it down as much as possible for me being a super newbie

    THANK YOU! I would not be anywhere near this level of understanding without these forums and the experts that lend a hand.

  2. #2
    Registered User
    Join Date
    08-15-2016
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    9

    Re: VBA code error for assigning a value in "For Each"

    maybe try:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-23-2021
    Location
    United States
    MS-Off Ver
    365
    Posts
    2

    Re: VBA code error for assigning a value in "For Each"

    Thanks for the reply,

    I tried that and got the error "Run Time 1004 Unable to set the NumberFormat property of the PivotField Class"

    My thought was that it was an error with .NumberFormat so I changed it to .DateFormat instead and I got error "438 object doesnt support this property or method"
    So I am still stumped on what to try from here..

  4. #4
    Registered User
    Join Date
    08-15-2016
    Location
    Australia
    MS-Off Ver
    2010
    Posts
    9

    Re: VBA code error for assigning a value in "For Each"

    That's odd, without seeing the data it's hard to suggest anything else.
    Uploading a sample sheet might help to pin this down.

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,619

    Re: VBA code error for assigning a value in "For Each"

    for sure this
    Please Login or Register  to view this content.
    is wrong as you try to assign a PivotField object to PivotTable variable.

    proper code shall be:
    Please Login or Register  to view this content.
    I don't want to produce dummy data to test your issue but may be (?) the problem is with data stored in enddate type? May be it's not date type?

    You may test it that way:


    Please Login or Register  to view this content.
    But general comment by gijsmo that with sample file we could test it is 100% valid
    Best Regards,

    Kaper

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 35
    Last Post: 01-13-2016, 02:16 AM
  2. = EMBED("Forms.ComboBox.1","") error "Reference not valid."
    By CatharinaCatharina in forum Excel General
    Replies: 2
    Last Post: 12-11-2014, 09:58 AM
  3. [SOLVED] If Not ActiveSheet.Range("A1").Value Like "apple" Then MsgBox "Error"
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-18-2014, 02:16 PM
  4. [SOLVED] Error Msg: "Compile error: Method or data member not found" in VBA code
    By nenadmail in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2012, 02:12 AM
  5. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM
  6. Macro Error with code "Range("A65536").End(xlUp).EntireRow.Insert"
    By lukasj13 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-30-2010, 08:48 PM
  7. Replies: 5
    Last Post: 08-08-2010, 03:03 PM

Tags for this Thread

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