Passing Argument to Excel Macro

i_planned
excel
activities
macro
r_18_1

#1

We have Execute Macro activity, We can have the option where we can pass arguments to Excel Macro directly. I have seen the functionality in other RPA tool.


How to execute Macro with arguments
Execute Macro activity should have input and output parameters
Can I pass Parameters in Execute macro Activity If Yes then how. Please help
Get Active Sheet Name
Execute Macro Activity Runs Twice
#2

Is this being looked into?
when will this functionality be available?


#3

Hi,

You can make a call to the macro as follows:

“Proc(”“Hello”",8)"

Considering Macro name is Proc and is accepting a string variable and integer variable.

Thanks,
Yuga


Calling Macro with Parameter using UiPath
Excel Automation for Shapes
#4

@yuga that’s extremely helpful - how did you work that out?!


#5

It’s the default macro syntax but with escaped quotations


#6

Yes simple enough but I wouldn’t have guessed it :slight_smile:


#7

adding it to documentation :wink:


#8

@richarddenton: Struggled initially to get the syntax right.


#9

help,

not it works


#10

Basically you should pass a string variable like that:
“change_sheet(”"" + sheet + “”")"

In Excel I could pop up a messagebox showing the string. However, for unknown reasons I couldn’t select the specified sheet via VBA. It run through the VBA code without error message and without processing two commands (Stop/Worksheets.Select). But anyway you can select you sheet directly from UiPath (Select Range activity).

My code in VBA was the following:

Public Sub change_sheet(ByRef sheetName As String)
Stop
MsgBox sheetName
Worksheets(sheetName).Select
End Sub

I hope that helps!


#11

hi,

I’ll try it :smiley:


#12

it work

thx :smiley: :grin::blush:


#13

Personally I always assign fairly complicated string concatenations to a variable and then log them. If something is wrong or goes wrong it’s then far easier to debug.

RD


#14

How could we get a value from Excel macro?


#15

No one can give bad answer like this

Run Macro

Write value to Cell in Macro (If you can)

Read Cell

Write Cell - String.Empty


#16

Do we need this? Since we have an workaround?


#17

I have the below as macro code to test argument passing

Sub max(a As Long, b As Long)
MsgBox a & " " & b
If a > b Then
Sheets(“Sheet1”).Range(“A1”).Value = a
Else
Sheets(“Sheet1”).Range(“A1”).Value = b
End If
End Sub

This is in my execute Macro: “max(7,8)”

Nothing happens when I run the execute. Any ideas?


#18

Try “max(”“10"”,"“15"”)"


#19

Thanks for the reply. I have tried that, but that doesn’t work.


#20

Hello everyone,

From what I’ve done and researched up until now, I saw that after creating a macro with parameters, UiPath is not running the macro anymore, or the modifications are not saved(which makes no sense). Is there some kind of bug? I mention the fact that i’m not receiving any kind of error.

my example:
//ading a new line

Sub newLine()

Dim value As Integer
value = int1
With ThisWorkbook.Worksheets(“Sheet1”)
Sheets(“Sheet1”).Range(“A” & CStr(1)).Select
ActiveCell.EntireRow.Insert shift:=xlDown
End With

End Sub

in execute macro i have “newLine”
=>working


Sub newLine(int1 As Integer)

Dim value As Integer
value = int1
With ThisWorkbook.Worksheets(“Sheet1”)
Sheets(“Sheet1”).Range(“A” & CStr(int1)).Select
ActiveCell.EntireRow.Insert shift:=xlDown
End With

End Sub

in execute macro i have “newLine(1)”
=>not working

Is this some kind of bug? Am I doing something wrong?
Here is the file macro_newLine.zip (14.7 KB)


Twice execute macro