web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to prepare the Piv...
Power Automate
Unanswered

How to prepare the Pivot table in Power automate desktop flow?

(0) ShareShare
ReportReport
Posted on by 203

Hi Everyone,

 

How to prepare the Pivot table, Add filter, columns, rows, value & also copy paste the pivot table into the new sheet  in Power automate desktop flow please help ?

I have the same question (3)
  • Verified answer
    Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Power Automate Desktop does not have any native actions for building or modifying Pivot tables in Excel. What you can do, however, is use the Run VBscript action to call a VBscript that creates/modifies your Pivot table.

     

    See this page for reference. This is part of the WinAutomation (predecessor of Power Automate Desktop) documentation, but the Run VBscript action works the same way and the script itself is valid.

    https://learn.microsoft.com/en-us/previous-versions/troubleshoot/winautomation/process-development-tips/excel-automation/pivot-table-using-vbscript

     

    It has a pretty detailed explanation, and also provides references for more details. 

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution.
    If you like my response, please give it a Thumbs Up.

    If you are interested in Power Automate, you might want to follow me on LinkedIn at https://www.linkedin.com/in/agnius-bartninkas/

  • Ahmedmuz Profile Picture
    203 on at

    its looks very tricky and confusion here,,, 😞

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    The explanation is quite detailed, so I don't see why it is confusing.

    There is no other way to do it, however, apart from, maybe, trying to do a script in PowerShell or Python, if you prefer those over VBscript. In any case, you will need scripting to create or modify an Excel Pivot table using Power Automate Desktop.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution.
    If you like my response, please give it a Thumbs Up.

    If you are interested in Power Automate, you might want to follow me on LinkedIn at https://www.linkedin.com/in/agnius-bartninkas/

  • Ahmedmuz Profile Picture
    203 on at

    Hi Agnius,

    By using above VBScript can you show one example n snap shot it will help me how to edit in code or in variables please for pivot ?

  • Ahmedmuz Profile Picture
    203 on at

     

    Hi I tried but its not creatting the pivot table please help ,, Flow is running but its not creating pivot table where i am doing wrong please help  below are snap shots

     

    Ahmedmuz_0-1693570400136.png

    Ahmedmuz_1-1693570422102.png

     

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Actually, I've just noticed that the script is a bit off. It initializes the object slightly incorrectly. Try this instead:

    Set objExcel = GetObject(, "Excel.Application")
    
    'Activate the workbook
    Set objWorkbook = objExcel.Workbooks("%FileName%").Activate
    
    'Add the Sheet name
    Set objData = objWorkbook.Sheets("%SheetName%")
    Set objSheet = objWorkbook.Sheets.Add(,objData)
    
    'Edit the Name of the sheet (Do not use spaces)
    objSheet.Name="%SheetNameForPivotTable%"
    
    'Add the Source data reference
    Const SrcData = "%SheetName%!R1C1:R%FirstFreeRow-1%C%FirstFreeColumn-1%"
    Const xlDatabase = 1
    Const Version = 5
    'Add the Destination
    Const Destination = "%SheetNameForPivotTable%!R3C1"
    'Add the Pivot table name
    Const TableName = "PivotTable1"
    
    Set pvtcache = objWorkbook.PivotCaches.Create(xlDatabase,SrcData,Version)
    Set pvtTable = pvtcache.CreatePivotTable(Destination,TableName)
    
    Const xlRowField = 1
    'Add the field's name for Row Field
    pvtTable.pivotFields("%RowFieldName%").orientation = xlRowField
    
    Const xlColumnField = 2
    'Add the field's name for Column Field
    pvtTable.pivotFields("%ColumnFieldName%").orientation = xlColumnField
    
    Const xlFilterField = 3
    'Add the field's name for Filter
    pvtTable.pivotFields("%FilterFieldName%").orientation = xlFilterField
    
    'Add the field's name and Alias for Sum Field
    Const xlSum = %AggregationMethod%
    pvtTable.AddDataField pvtTable.PivotFields("%AggregationFieldName%"), "%AggregationDisplayName%", xlSum
    
    'Save changes
    objWorkbook.Save

     

    This will run correctly when you already have the worksheet open - it will attach to the worksheet, instead of trying to open it again.

     

    Your flow is correct, except you no longer need the path to the file for the script, but you need the name of the file in %FileName% for the script to be able to attach to it.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Ahmedmuz Profile Picture
    203 on at

    Hi Agnius,

    As per your instructions, I have removed the path variable and created one with Filename. I have also copied and pasted the provided code. However, I am encountering an issue as the pivot table is not being created in Excel. I have attached a video of the Excel process for your reference. Please provide assistance in resolving this matter.

     

    Please let me know do i need to do any changes in code as i need pivot to get selected from sheet1!$A$1:$AC$4.

  • Ahmedmuz Profile Picture
    203 on at

    @Agnius Request you to please help on the above - i have attached a small video and also what went wrong please help asap it will be means a lot.

     

    Best Regards 

    Muzamil Ahmed

  • Ahmedmuz Profile Picture
    203 on at

    @Agnius 

    but you need the name of the file in %FileName% for the script to be able to attach to it.

    What i have to pass in the Filename Variable - Please help ??

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Hi. I have finally found the time to look into the issue.

    I've modified the script a bit. See this: 

    Set objExcel = CreateObject("Excel.Application")
    
    'Open the workbook
    Set objWorkbook = objExcel.Workbooks.Open("%FilePath%")
    
    'Add the Sheet name
    Set objData = objWorkbook.Sheets("%SheetName%")
    Set objSheet = objWorkbook.Sheets.Add(,objData)
    
    'Edit the Name of the sheet (Do not use spaces)
    objSheet.Name="%SheetNameForPivotTable%"
    
    'Add the Source data reference
    Const SrcData = "%SheetName%!R1C1:R%FirstFreeRow - 1%C%FirstFreeColumn - 1%"
    Const xlDatabase = 1
    Const Version = 5
    'Add the Destination
    Const Destination = "%SheetNameForPivotTable%!R3C1"
    'Add the Pivot table name
    Const TableName = "PivotTable1"
    
    Set pvtcache = objWorkbook.PivotCaches.Create(xlDatabase,SrcData,Version)
    Set pvtTable = pvtcache.CreatePivotTable(Destination,TableName)
    
    Const xlRowField = 1
    'Add the field's name for Row Field
    pvtTable.pivotFields("%RowFieldName%").orientation = xlRowField
    
    Const xlColumnField = 2
    'Add the field's name for Column Field
    pvtTable.pivotFields("%ColumnFieldName%").orientation = xlColumnField
    
    Const xlFilterField = 3
    'Add the field's name for Filter
    pvtTable.pivotFields("%FilterFieldName%").orientation = xlFilterField
    
    'Add the field's name and Alias for Sum Field
    Const xlSum = %AggregationMethod%
    pvtTable.AddDataField pvtTable.PivotFields("%AggregationFieldName%"), "%AggregationDisplayName%", xlSum
    
    'Save changes and close Excel
    objWorkbook.Save
    objWorkbook.Close
    objExcel.Quit

     

    Note that this requires the Excel file to be closed when you run the script. But it also requires the first free row and column for the script to work. So, you will need to Launch Excel, then Get first free column/row from Excel worksheet, then Close Excel and then Run VBScript.

     

    I've tested it with the following actions and it worked fine (a PivotTable was created):

    Agnius_0-1694677362885.png

     

    You can use this to copy and paste into PAD:

    SET FilePath TO $'''C:\\RPA\\Test.xlsx'''
    SET SheetName TO $'''Sheet1'''
    SET SheetNameForPivotTable TO $'''Pivot'''
    SET RowFieldName TO $'''Test'''
    SET ColumnFieldName TO $'''SomeColumnName'''
    SET FilterFieldName TO $'''Test'''
    SET AggregationMethod TO -4157
    SET AggregationFieldName TO $'''SomeOtherColumnName'''
    SET AggregationDisplayName TO $'''Sum of Something'''
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: FilePath Visible: True ReadOnly: False Instance=> ExcelInstance
    Excel.GetFirstFreeColumnRow Instance: ExcelInstance FirstFreeColumn=> FirstFreeColumn FirstFreeRow=> FirstFreeRow
    Excel.CloseExcel.Close Instance: ExcelInstance
    Scripting.RunVBScript.RunVBScript VBScriptCode: $'''Set objExcel = CreateObject(\"Excel.Application\")
    
    \'Open the workbook
    Set objWorkbook = objExcel.Workbooks.Open(\"%FilePath%\")
    
    \'Add the Sheet name
    Set objData = objWorkbook.Sheets(\"%SheetName%\")
    Set objSheet = objWorkbook.Sheets.Add(,objData)
    
    \'Edit the Name of the sheet (Do not use spaces)
    objSheet.Name=\"%SheetNameForPivotTable%\"
    
    \'Add the Source data reference
    Const SrcData = \"%SheetName%!R1C1:R%FirstFreeRow - 1%C%FirstFreeColumn - 1%\"
    Const xlDatabase = 1
    Const Version = 5
    \'Add the Destination
    Const Destination = \"%SheetNameForPivotTable%!R3C1\"
    \'Add the Pivot table name
    Const TableName = \"PivotTable1\"
    
    Set pvtcache = objWorkbook.PivotCaches.Create(xlDatabase,SrcData,Version)
    Set pvtTable = pvtcache.CreatePivotTable(Destination,TableName)
    
    Const xlRowField = 1
    \'Add the field\'s name for Row Field
    pvtTable.pivotFields(\"%RowFieldName%\").orientation = xlRowField
    
    Const xlColumnField = 2
    \'Add the field\'s name for Column Field
    pvtTable.pivotFields(\"%ColumnFieldName%\").orientation = xlColumnField
    
    Const xlFilterField = 3
    \'Add the field\'s name for Filter
    pvtTable.pivotFields(\"%FilterFieldName%\").orientation = xlFilterField
    
    \'Add the field\'s name and Alias for Sum Field
    Const xlSum = %AggregationMethod%
    pvtTable.AddDataField pvtTable.PivotFields(\"%AggregationFieldName%\"), \"%AggregationDisplayName%\", xlSum
    
    \'Save changes and close Excel
    objWorkbook.Save
    objWorkbook.Close
    objExcel.Quit''' ScriptOutput=> VBScriptOutput ScriptError=> ScriptError
    

     

    You will obviously need to change all (or most of) the variable values and you might want to have different aggregation methods. Please see the article I shared earlier to figure out what you need.

     

    But this at least is verified to work fine.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 538 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 405 Moderator

#3
abm abm Profile Picture

abm abm 252 Most Valuable Professional

Last 30 days Overall leaderboard