简体   繁体   English

Excel VBA:子可以使用未分配的参数调用UDF吗?

[英]Excel VBA: Can a sub call a UDF with unassigned arguments?

I've written an Excel module to convert latitude/longitude to UTM coordinates. 我编写了一个Excel模块,将纬度/经度转换为UTM坐标。 The main procedure is a sub that defines the arguments and calls the function that in turn calls a DLL function. 主要过程是定义参数并调用该函数的子程序,该函数又调用DLL函数。 The idea is the function opens the function argument window for the user to provide the data (function is defined in another sub with Application.MacroOptions). 想法是该函数打开函数参数窗口供用户提供数据(函数在Application.MacroOptions的另一个子集中定义)。

Sub GeoToTM()

'Declare variables
Dim TM() As Variant
Dim Lat As Double
Dim Lon As Double
Dim TMProj As Integer
Dim Ellipsoid As Integer

'Call Excel function

TM = GeoConv(Lat, Lon, TMProj, Ellipsoid)

End Sub

'VBA function wrapper
Function GeoConv(Lat, Lon, TMProj, Ellipsoid) As Variant

Dim East As Double
Dim North As Double
Dim locTM(1 To 2) As Variant

'Call C++ function
GeodeticToTM Lat, Lon, TMProj, Ellipsoid, East, North

locTM(1) = East: locTM(2) = North
GeoConv = locTM

End Function

The function on its own performs as advertised but when I run the entire module the function is accessed but the function argument window does not appear; 该函数本身按广告执行,但是当我运行整个模块时,可以访问该函数,但不会出现函数参数窗口; the arguments are never populated so the function returns zeros. 参数从不填充,因此函数返回零。

How do I get this to work? 我该如何工作? Is "Option" arguments the trick here? “选项”参数是否有用?

Thanks, 谢谢,

Chris 克里斯

You should be able to pass in optional values as parameters that default to a value that would never be used. 您应该能够将可选值作为默认值传递给永远不会使用的参数。 If the values are those 'impossible' values then rewrite then with input boxes. 如果值是那些“不可能的”值,则用输入框重写。

Sub GeoToTM()

    'Declare variables
    Dim s As String
    Dim TM() As Variant
    Dim Lat As Double
    Dim Lon As Double
    Dim TMProj As Integer
    Dim Ellipsoid As Integer

    'Call Excel function

    'with Lat and Lon as values then
    TM = GeoConv(TMProj, Ellipsoid, Lat, Lon)

    'with Lat and Lon unassigned
    TM = GeoConv(TMProj, Ellipsoid)

End Sub

'VBA function wrapper
Function GeoConv(TMProj As Integer, Ellipsoid As Integer, _
            Optional Lat As Double = -1, Optional Lon As Double = -1) As Variant

    Dim East As Double
    Dim North As Double
    Dim locTM(1 To 2) As Variant

    If Lat < 0 Then _
        Lat = CDbl(InputBox("Latitude: ", "Supply Latitude", "0.000"))
    If Lon < 0 Then _
        Lon = CDbl(InputBox("Latitude: ", "Supply Longtitude", "0.000"))

    'Call C++ function
    GeodeticToTM Lat, Lon, TMProj, Ellipsoid, East, North

    locTM(1) = East: locTM(2) = North
    GeoConv = locTM

End Function

Well, here's how I got it to work: 好吧,这就是我的工作方式:

Rearranging the code, I ... 重新排列代码,我...

  • declared the "input" variables in the main sub, 在主子句中声明了“输入”变量,
  • selected the cell range, 选择了单元格范围
  • put the Excel formula in the range with Selection.FormulaArray, 使用Selection.FormulaArray将Excel公式置于范围内,
  • called the already populated function wizard with Application.Dialogs(xlDialogFunctionWizard).Show, 使用Application.Dialogs(xlDialogFunctionWizard).Show调用已经填充的功能向导,
  • then called the Excel function. 然后调用Excel函数。

So, the .Show allows the user to populate the arguments which then get passed to the Excel function/DLL function. 因此,.Show允许用户填充参数,然后将其传递给Excel函数/ DLL函数。

Sub GeoToTM()

'Declare variables
Dim TM() As Double
Dim Lat As Double
Dim Lon As Double
Dim TMProj As Integer
Dim Ellipsoid As Integer

Dim resultRng As Range
Dim Arg As Boolean

'Initialize Cells
ActiveCell.Offset(-1, 0).Value = "Easting"
ActiveCell.Offset(-1, 1).Value = "Northing"

'Call Excel function
Set resultRng = Range(ActiveCell, ActiveCell.Offset(0, 1))
resultRng.Select
Selection.FormulaArray = "= GeoConv()"
Arg = Application.Dialogs(xlDialogFunctionWizard).Show
If Arg = False Then Exit Sub

TM = GeoConv(Lat, Lon, TMProj, Ellipsoid)

End Sub

I actually had to remove "Option" from the arguments in the function so this would work, but it did! 实际上,我实际上必须从函数的参数中删除“ Option”,这样可以正常工作,但确实可以!

Chris 克里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM