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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How do I run a script ...
Power Apps
Answered

How do I run a script recorded in excel via power automate?

(0) ShareShare
ReportReport
Posted on by 15

I am trying to run a simple script I have recorded on Excel which I want to run as a step in power automate.
When I try and run the script I get this error message in power automate

We were unable to run the script. Please try again.
WorksheetCollection.getActiveWorksheet() is not allowed when run via Power Automate. Please modify your script to specify a target worksheet.  

 

The script it copied below - I suspect this is something very easy - but have limited experience of scripts / VBA other than using the record function and cant find the solution anywhere - so any help would be most appreciated.

Many thanks

Matt

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  // Replace Stongly Agree with 5 on sheet student data
  selectedSheet.replaceAll("Stongly Agree""5", {
    completeMatch: false,
    matchCase: false
  });
Categories:
I have the same question (0)
  • Drrickryp Profile Picture
    Super User 2024 Season 1 on at

    Hi @MattBrown 

    I don't think you can run an Excel script from within PowerApps.  However, you should consider using a PowerApps function to accomplish the same thing.  It looks like  you want to make some changes in the data.  Perhaps you could use Patch() or UpdateIf() to solve the problem.

  • MattBrown Profile Picture
    15 on at

     

     

     

     

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @MattBrown 

    You might consider re-reading the docs on the use of scripts:

    Power Automate can't use relative references like Workbook.getActiveWorksheet to access workbook components. So, we need a workbook and worksheet with consistent names that Power Automate can reference. 

    That is in fact the very thing you are doing in your script...so, you'll need to rewrite that accordingly.

  • MattBrown Profile Picture
    15 on at

    Thanks - that makes perfect sense.

     

    However, this is where my lack of expertise is really embarrassing - how do I do I stop the references being relative - I have played around with the script but just cant get it to work and everything I search for is too complex for me - I dont suppose you (or anyone else) can point me in the direction of something that can show me how in a fairly straight forward way? 

     

    Thanks

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @MattBrown 

    Take a close look at step #3 in the Create and Office Script of the link you referenced.

    The key is that you need to get things by their names.  So, instead of let selectedSheet = workbook.gertActiveWorksheet() replace that with let selectedSheet = workbook.getWorksheet("theNameOfYourWorksheet")

     

  • MattBrown Profile Picture
    15 on at

    Thanks @RandyHayes -you are a total star - who has saved me potentially hours of work.

     

    I had tried entering the sheets name beforehand- but suspect I hadn't removed the "active" from the line. 

     

    thanks ever so much

     

    Matt

  • Chipper Profile Picture
    116 on at

    Is there any other thoughts on this?  I am getting the same error still.

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Chipper 

    Since this post is quite old, I would recommend opening a new post with the issue you are experiencing. 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 530

#2
WarrenBelz Profile Picture

WarrenBelz 459 Most Valuable Professional

#3
Haque Profile Picture

Haque 314

Last 30 days Overall leaderboard