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 Automate / Running Excel VBA scri...
Power Automate
Answered

Running Excel VBA script through Power Automate generates "Subscript out of range"

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I have created a flow as follows:

 

Rand_Al_Thor_0-1628720049787.png

This flow basically opens SAP, opens an excel file and runs the VBA script in that excel file, and then closes the excel file.

 

When I run the script without Power Automate, the script runs perfectly without any errors. When I try to run the script through Power Automate, it always gives me an error "Subscript out of range".

 

I tried using the run macro in the Power Automate, but it gives the same result. I am not sure what exactly is the problem.

 

The error sub in VBA is this:

 

'Sub to close Workbook Inventory on Hand

Sub Close_WBIOH()

Application.DisplayAlerts = False

Dim wbIOH As Workbook
Dim wsIOH As Worksheet
Dim rngcpy As Range

Set wbIOH = Workbooks("Inventory on Hand.xlsx")
Set wsIOH = wbIOH.Worksheets("Sheet1")
Set rngcpy = wsIOH.Range("A2", Range("A2").End(xlDown).End(xlToRight))

rngcpy.Copy

wbIOH.Close

Application.DisplayAlerts = True

End Sub

The line on which the error occurs is:

 

Set wbIOH = Workbooks("Inventory on Hand.xlsx")

Anyone knows why is this happening? Am I doing something wrong here?

 

Thank you.

I have the same question (0)
  • Verified answer
    Community Power Platform Member Profile Picture
    Microsoft Employee on at

    In thread Running an excel macro in PAD - Power Platform Community (microsoft.com), fellow users discuss that it's not possible to run a macro in an excel file different than the one that contains the code without workarounds.

    I guess this is the case, as your macro triggers another workbook (the line on which the error occurs - 'Inventory on Hand').

    In  that thread, @MichaelAnnis suggests a workaround. Another one is 'transfer' the macro to 'Inventory on Hand' file (obviously with proper adjusts) and call this file/macro instead. 

  • Verified answer
    MichaelAnnis Profile Picture
    5,727 Moderator on at

    Is "Launch MA_Excel" your macro workbook?

    And then what?  You click on a button "Rectangle Rounded Corners 2" to activate the macro?

     

    Is the Inventory on Hand workbook already open when this macro starts?  If so, when Power Automate opens the Macro workbook, they are going to be in 2 different instances of Excel, and the macro will not be able to find the Inventory on Hand workbook.

     

    If so, the solution would be to make sure the Inventory on Hand workbook is closed when the macro start, and re-write the VBA macro to open, copy the range, and close the Inventory on Hand workbook, because if the macro opens it, then it will be in the same instance and accessible to the macro.

     

    You don't have this problem when manually doing it, because if you manually open a new workbook, it automatically attaches to an open instance of Excel, but Power Automate always opens a new instance when using "Launch Excel," even if you give it the same instance variable.

     

    Best of Luck!

     

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Hello @Anonymous, thank you for your reply.

     

    Unfortunately, I cannot add code to the file because the file is generated anew by a different code every time I run the macro. I will try the solution mentioned by @MichaelAnnis and see if it works for me.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Hello @MichaelAnnis, thank you for your reply. The "Inventory on Hand" workbook is downloaded by the VBA code and then it automatically opens. I found a way around this so will try that first and see if that helps me run the macro automatically.

     

    Thank you!

     

     

  • Betaboy Profile Picture
    29 on at

    revisit this line of code.

     

    Set rngcpy = wsIOH.Range("A2", Range("A2").End(xlDown).End(xlToRight))

    what is your use case for this line? 

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Hello @MichaelAnnis, I made changes in the code to close the open WO and then reopen it and it works.

     

    Even though this is very redundant, I guess until Microsoft makes updates this is the only way I can run the code.

     

    Thank you for your help!

  • Betaboy Profile Picture
    29 on at

    my suggestion is store the vba script as a addin and store with a short cut key.

     

    so whenever you open the excel, you can just use "send keys" method to trigger the vba script.

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 Automate

#1
Valantis Profile Picture

Valantis 641

#2
Vish WR Profile Picture

Vish WR 640

#3
Haque Profile Picture

Haque 495

Last 30 days Overall leaderboard