Consulting

Results 1 to 9 of 9

Thread: Select & drag all fields to Data area in Pivot in one key stroke

  1. #1

    Select & drag all fields to Data area in Pivot in one key stroke

    Hi,
    Issue:

    I've 38 field names given in each column. The user wants to calculate the sum only by Pivot report. In such case, the user has to manually drag all the 38 fields in to the Data area.

    Requirement:

    Is there any other way in which the user can select and drag all the 38 fields in the Data area in one key stroke?

    Please find attached the data and help me.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Dim i

    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    "'Raw data'!R1C1:R34C39").CreatePivotTable TableDestination:= _
    "'[Pivot_Select_drag_in one key stroke.xls]Raw data'!R39C23", TableName:= _
    "PivotTable1", DefaultVersion:=xlPivotTableVersion10
    For i = 1 To 38

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("A" & i)
    .Orientation = xlDataField
    End With
    Next[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Do i need to place this code in This workbook module?

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Add a Standard module and insert the code in a suitably named Sub routine
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    I'm getting compile error. would appreciate if you could send me a test file with the code set in it..

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi there,

    I know zip about pivots, but if you'll add the rest of the underscores to the wb's name, ie - "'[Pivot_Select_drag_in_one_key_stroke.xls]Raw data'!R39C23", I believe it works as desired.

    Mark

  7. #7
    Still getting error, can you send me the file which works for you?

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I am afraid I would not be emailing if that is what you mean. Here is your wb with the code installed as MD directed.

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This works for me, but it does seem "delicate". You may want to record your own table creation macro and add the looping code to it.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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