Consulting

Results 1 to 4 of 4

Thread: VBA pivot with no range

  1. #1
    VBAX Newbie
    Joined
    Aug 2012
    Posts
    5
    Location

    VBA pivot with no range

    Hello,

    I'm just learning VBA and i got to the point that i need often pivottable's
    But when you record it always take a range!

    So i thought that the following will work but it doesn't

    Can anyone have a quick look and see what i did wrong or forgot?

    Thx a lot for your help



    [VBA]
    Private Sub()
    Dim LR As Long
    LR = ActiveSheet.UsedRange.Rows.Count
    Sheets.Add
    ActiveSheet.Name = "test"
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "Pivot!R1C1:R" & LR, Version:=xlPivotTableVersion14).CreatePivotTable _
    TableDestination:="test!R3C1", TableName:="Draaitabel5", DefaultVersion _
    :=xlPivotTableVersion14
    Sheets("test").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("Draaitabel5").PivotFields("a")
    .Orientation = xlRowField
    .Position = 1
    End With
    With ActiveSheet.PivotTables("Draaitabel5").PivotFields("b")
    .Orientation = xlColumnField
    .Position = 1
    End With
    ActiveSheet.PivotTables("Draaitabel5").AddDataField ActiveSheet.PivotTables( _
    "Draaitabel5").PivotFields("c"), "Som van c", xlSum
    With ActiveSheet.PivotTables("Draaitabel2").PivotFields("d")
    .Orientation = xlPageField
    .Position = 1
    End With
    End Sub[/VBA]

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi.

    taken from your code:
    [VBA]SourceData:="Pivot!R1C1:R" & LR[/VBA]

    it must be like this:
    [VBA]SourceData:="Pivot!R1C1:R250C10"[/VBA]

    so maybe
    [VBA]SourceData:="Pivot!R1C1:R" & LR & "C10"[/VBA]

    an article from msdn.
    http://msdn.microsoft.com/en-us/libr.../hh243933.aspx
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Newbie
    Joined
    Aug 2012
    Posts
    5
    Location
    So you say that i need to put also howmany columns?
    But i don't know that

    so i need to create for example:

    Dim LC As Long
    LC = ActiveSheet.UsedRange.Columns.Count


    "Pivot!R1C1:R" & LR & "C" & LC

    or do i understand it wrong?
    Last edited by Lesly; 09-26-2012 at 06:11 AM.

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    yes.

    but defining a dynamic named range will make it easier.

    from Defined Names group in Formulas tab, click Name Manager + New or directly Define Name.

    assuming your database is in worksheet named "Pivot", write a name for your named range, eg, "pvtRange", in Name box, then copy below formula to RefersTo box.
    [vba]=OFFSET(Pivot!$A$1,0,0,COUNTA(Pivot!$A:$A),COUNTA(Pivot!$1:$1))[/vba]

    ps: dont forget to change formula argument seperator to ; from , if it is so.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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