Application.Run and range parameter?

前端 未结 3 808
抹茶落季
抹茶落季 2021-01-15 19:03

If I have an excel function such as:

=my_function(A1:C4)

and I want to call this from VBA like:

Dim t as variant
t = Applic         


        
相关标签:
3条回答
  • 2021-01-15 19:29

    I haven't try it, but wouldn't

    t=myfunction(range)
    
    0 讨论(0)
  • 2021-01-15 19:35

    The simplest syntax is

    Application.Run("my_function", [A1:c4])
    
    0 讨论(0)
  • 2021-01-15 19:44

    Your range has to come from a worksheet, so if you define that range first and then pass it to the function in your other workbook (I assume that's what you want to do since you wouldn't use Application.Run otherwise). So the following code will do the trick:

    Sub RunFunctionInAnotherWorkbook()
        Dim rThisRange as Range
        Dim vResult as Variant
    
        Set rThisRange = ActiveSheet.Range("A1:C4")
    
        vResult = Application.Run("'MyOtherWorkbook.xls'!TheModuleName.TheSubName", rThisRange)
    End Sub
    

    And the function in the workbook "MyOtherWorkbook.xls":

    Function TheSubName(inputRange as Range) as Variant
    
        'Work your magic here
    
    End Function
    
    0 讨论(0)
提交回复
热议问题