+ Reply to Thread
Results 1 to 12 of 12

Running a variable module with parameters

  1. #1
    Registered User
    Join Date
    03-14-2014
    Location
    Fleet
    MS-Off Ver
    Excel 2013
    Posts
    22

    Running a variable module with parameters

    Hi All

    From looking at past posts this question has been asked and answered many times it seems. Believe me when I tell you I think I have tried them all and I am not getting anywhere

    I have two values which I retrieve from specific cells in a workbook and store in local variables. The first value is the module name and the second are the parameters associated with it.

    RipperExtractionModule = ExtractAmazonUKFormat (Module Name)
    RipperExtractionParameters = "C:\KarenRipper\ouput\AmazonUKRipper01.xls","AmazonUKRipper01" (Parameters)

    I have tried the following:

    Attempt #1
    SubName = RipperExtractionModule & "(" & RipperExtractionParameters & ")"
    x = Application.Find("(", SubName)
    ParseSubName = Left(SubName, x - 1)
    ParseSubNum = Val(Right(SubName, Len(SubName) - x))
    Run (ParseSubName & "(" & RipperExtractionParameters & ")")

    In this case x gets a value of 22 which I suppose means that it found the module but when running it tells me that the macro cannot be found.

    Attempt #2
    Evaluate (RipperExtractionModule & "(" & RipperExtractionParameters & ")")

    This doesn't give me an error but doesn't run the module either

    Attempt #3
    c = Evaluate (RipperExtractionModule & "(" & RipperExtractionParameters & ")")
    This doesn't give me an error except that the procedure does not get run. c returns a string stating "error 2029"

    Attempt #4
    Run(RipperExtractionModule & "(" & RipperExtractionParameters & ")")

    This just gives me an error saying the macro cannot be found in this workbook.

    Any ideas?

    Thanks
    Steve

  2. #2
    Registered User
    Join Date
    03-14-2014
    Location
    Fleet
    MS-Off Ver
    Excel 2013
    Posts
    22

    Re: Running a variable module with parameters

    Problem 1 Solved - I apologise, I was having a blond moment. I had the module and subname both the same

    Anyway, I still have a problem with it. I am using the:

    Run(RipperExtractionModule & "(" & RipperExtractionParameters & ")")

    but these seems to be running the module twice. At the moment all I have in the sub is a msgbox "I'm Here" but this message appears twice when I use the command above. I also need to have the function return a value so I tried

    c = run ....
    and
    c = evaluate ....

    Neither return a value

    Another strange thing is that neither will let me step through the code. I have placed breakpoints inside the sub but even though my msgbox gets displayed the code wont stop at that point.
    Last edited by UML; 04-01-2014 at 08:15 AM.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Running a variable module with parameters

    Steve

    Usually to return a value you would use a function rather than a sub.

    Not sure how using a function in another workbook, which appears to be what you have based on the use of Run, would work.

    Could you post the code you are trying to run and explain what you want it to do/return?

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    03-14-2014
    Location
    Fleet
    MS-Off Ver
    Excel 2013
    Posts
    22

    Re: Running a variable module with parameters

    Hi Norie

    Thanks for the reply.

    I apologise for not being very clear. I am trying to run a function which is in one of the modules in the same workbook.

    In Macro
    Sub StartRipper()
    Dim b as boolean
    Dim Ripperoutputpath as string
    Dim Worksheetname as string
    Dim RipperExtractionModule as string

    Dim c as string

    'These two lines lookup the parameters from a worksheet in the current workbook. These values
    'are being returned correctly but for the sake of this demo I am hardcoding them below
    'RipperExtractionParameters = GetRipperParameters("RipperExtractionParameters")
    'RipperExtractionModule = GetRipperParameters("RipperExtractionModule")

    Ripperoutputpath = "c:\tmp\AmazonUKOutput1.xls"
    Worksheetname = "UK"
    RipperExtractionModule = "ExtractAmazonUKFormat" 'Excuse the variable name containing 'module'. It is actually the function name that is being called.
    RipperExtractionParameters = Ripperoutputpath & "," & Worksheetname

    'This line is hardcoded and works. It allows me to step through the code like normal.
    c = ExtractAmazonUKFormat(RipperOutputPath, WorksheetName)

    'This is the one that I am trying to get working. Without the "C = " it runs the function correctly but wont let me step through it nor will it return a value

    Run (RipperExtractionModule & "(" & RipperExtractionParameters & ")")

    'I have also tried the following but with the same result.
    'c = Evaluate (RipperExtractionModule & "(" & RipperExtractionParameters & ")")

    End Sub

    In Module
    Public Function ExtractAmazonUKFormat(RipperOutputPath As String, WorksheetName As String) As string
    Msgbox "RipperOutputPath" & vbcrlf & "Worksheetname"
    ExtractAmazonUKFormat = "Done"
    End Function

    I hope this makes sense.

    Thanks
    Steve
    Last edited by UML; 04-02-2014 at 03:42 AM.

  5. #5
    Registered User
    Join Date
    03-14-2014
    Location
    Fleet
    MS-Off Ver
    Excel 2013
    Posts
    22

    Re: Running a variable module with parameters

    Just in case it makes any difference I'm running 2007

  6. #6
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Running a variable module with parameters

    I think you may use:
    Please Login or Register  to view this content.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  7. #7
    Registered User
    Join Date
    03-14-2014
    Location
    Fleet
    MS-Off Ver
    Excel 2013
    Posts
    22

    Re: Running a variable module with parameters

    Thanks but it is still throwing up an error. It's telling me that the argument is not optional. All three values are correct and when I run the module in a "hardcoded" fashion using the same values then it works.

    Secondly... is it possible to return a value using the "Run" command? The function returns a value so I would have to say something like :

    c = Run(ripperextractionmodule,ripperoutputpath,worksheetname)

    Of course I've tried the above but it obviously doesn't work

    Thanks
    Steve

  8. #8
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Running a variable module with parameters

    Code is correct for what you have described - but Application.Run is better than Run. Please provide sample workbook.

  9. #9
    Registered User
    Join Date
    03-14-2014
    Location
    Fleet
    MS-Off Ver
    Excel 2013
    Posts
    22

    Re: Running a variable module with parameters

    Hi again

    I have attached an example script to this post. I really hope you can figure out what I am doing wrong.

    Thanks
    Steve
    Attached Files Attached Files

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Running a variable module with parameters

    Please Login or Register  to view this content.
    It is better to always use explicit property.

  11. #11
    Registered User
    Join Date
    03-14-2014
    Location
    Fleet
    MS-Off Ver
    Excel 2013
    Posts
    22

    Re: Running a variable module with parameters

    Thanks Izandol but I'm still getting the same errror:
    1004 Cannot run the macro 'CalculateAmazon'. The macro may not be available in this workbook or all macros may be disabled.

    I have checked that macros are enabled and that the function is declared as public.

    Steve

  12. #12
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Running a variable module with parameters

    You must post workbook showing this please.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How can I get the name of the Workbook that contains running module?
    By Jeff_Yu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-22-2012, 11:31 AM
  2. Deleting parameters to save running time?
    By aca in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-08-2012, 07:52 AM
  3. Running a macro from a different module
    By kyrgyzstanart in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2008, 11:56 AM
  4. Running Balance Calculation - with timestamp parameters
    By Push Latency in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-30-2007, 04:19 PM
  5. [SOLVED] Running XY chart labeller using a macro's parameters and arguments.
    By Hari Prasadh in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 07-12-2005, 12:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1