3
$\begingroup$

I have a macro in a module within an Excel file (called "test.xlsm", saved on my desktop) that takes one argument as a string:

Sub macro1(str As String)
    Range("A1").Value = str
End Sub

Using Mathematic Link for Excel add-in (version 3.5.0), I would like to run this macro directly from Mathematica 10.3:

In[1]:= << ExcelLink`
        Needs["ExcelLink`"]
        ExcelOpen["\\\\Mac\\Home\\Desktop\\Test.xlsm"]
        ExcelRun["macro1", "string"]

However, when I run this code from within Mathematica, I get the following type mismatch error:

Out[2]:= -Book: Test.xlsm-

         ExcelRun::failed: Type mismatch >>
Out[3]:= $Failed

When I call the macro from within Excel from another macro, it runs without any error.

I would appreciate any pointers or possible solutions. Thank you.

$\endgroup$
  • $\begingroup$ @Yves Klett thank you for the quick response. As requested, I supplied all the code which reproduces the problem on my end. $\endgroup$ – Lucas Feb 13 '16 at 9:42
  • $\begingroup$ Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Feb 13 '16 at 10:38
  • $\begingroup$ @Lucas thank you for the edit, let´s hope someone can help you there. And also a (if somewhat belated) welcome to the site! $\endgroup$ – Yves Klett Feb 13 '16 at 10:50
  • $\begingroup$ try making and running a macro with no arguments. $\endgroup$ – george2079 Feb 13 '16 at 15:22
  • 2
    $\begingroup$ @george2079 The string type was the problem! I updated macro1 to have its argument "As Variant" and now ExcelRun["macro1", arg1] works. Thank you for the suggestion! $\endgroup$ – Lucas Feb 13 '16 at 18:04
2
$\begingroup$

Changing the Excel macro argument type from String to Variant resolved the error:

Sub macro1(str As Variant)
    Range("A1").Value = str
End Sub
$\endgroup$
  • $\begingroup$ That's very good to know. Thanks for taking the time to post an answer for posterity! (+1) $\endgroup$ – MarcoB Feb 15 '16 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.