+ Reply to Thread
Results 1 to 6 of 6

What's the difference

  1. #1
    Registered User
    Join Date
    07-08-2009
    Location
    Zurich, Switzerland
    MS-Off Ver
    Excel 2002
    Posts
    4

    What's the difference

    Hello...

    I've got a question to 2almost similar VBA-code lines, difference a) is with "With / End With", b) is without :"With / End With" ....

    a)

    sourceDataValue = "Data!R1C1:R" & ResultRows + 1 & "C" & ResultCols

    With ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase)
    .SourceData = sourceDataValue
    .CreatePivotTable TableDestination:="R" & CreatePivotAt & "C1", _
    TableName:=PivotName, _
    DefaultVersion:=xlPivotTableVersion10
    End With


    b)

    sourceDataValue = "Data!R1C1:R" & ResultRows + 1 & "C" & ResultCols

    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    sourceDataValue).CreatePivotTable TableDestination:="R" & CreatePivotAt & "C1", _
    TableName:=PivotName, DefaultVersion:=xlPivotTableVersion10


    In my VBA Code, a) is not working, b) is working perfectly.

    What is the reason for one is working and the other not?

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

    Re: What's the difference

    The SourceData argument is required for the Add method of the PivotCaches collection if the source type is not xlExternal, hence the first code should fail.
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: What's the difference

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  4. #4
    Registered User
    Join Date
    07-08-2009
    Location
    Zurich, Switzerland
    MS-Off Ver
    Excel 2002
    Posts
    4

    Re: What's the difference

    Thanks for the hint!

    sorry for not taking care the rules. i am a newbie here.
    well, i looked up the links, but still not sure how to set the tags correct for the VBA code above...

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: What's the difference

    Quote Originally Posted by Mr_Tigas View Post
    Thanks for the hint!

    sorry for not taking care the rules. i am a newbie here.
    well, i looked up the links, but still not sure how to set the tags correct for the VBA code above...

    Click Edit in your first post... then click Go Advanced... then select your code and click the # icon at the top of the reply box.

    Click Save Changes

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: What's the difference

    You ought to change the Thread's Title as well

    The How To button on the Forum ToolBar above contains links to Videos demonstarting how to add code tags, etc.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ 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