VBA:将函数作为参数传递

VBA:将函数作为参数传递,vba,excel,Vba,Excel,我试图传递一个函数(实际上是两个函数)作为Sub的参数,行为: 调用OpenPullerReports(getFilePath(),getFileName()) 我不确定是否需要名称末尾的(),但我得到了一个参数非可选错误 以下是这两个功能: Function GetFilePaths(ByRef GetDateItems) As String Dim fp As String Dim cy2c As String cy2c = GetDateItems(currentYear2char)

我试图传递一个函数(实际上是两个函数)作为Sub的参数,行为:

调用OpenPullerReports(getFilePath(),getFileName())

我不确定是否需要名称末尾的
()
,但我得到了一个
参数非可选
错误


以下是这两个功能:

Function GetFilePaths(ByRef GetDateItems) As String

Dim fp As String

Dim cy2c As String
cy2c = GetDateItems(currentYear2char)

fp.PartMasterFilePath = "path1" & cy2c & "\" & currentMonth & "\"
fp.SupplierMasterFilePath = "path 2" & cy2c & "\" & currentMonth & "\"

GetFilePaths = fp

End Function
Function GetDateItems() As String

Dim d As String

d.currentMonth = Format(Date, "mmmm") 'July
d.currentDate = Format(Date, "mm-dd-yy") '06-09-15
d.currentYear2char = Format(Date, "yy") '15
d.currentYear4char = Format(Date, "yyyy") '2015
d.currentFiscalMonth = Format(DateAdd("m", 1, Date), "mm") '08
d.wsDate = currentFiscalMonth & currentYear4char '082015

GetDateItems = d

End Function


我想我会包括
GetDateItems()
,因为这两个函数都引用它:

Function GetFilePaths(ByRef GetDateItems) As String

Dim fp As String

Dim cy2c As String
cy2c = GetDateItems(currentYear2char)

fp.PartMasterFilePath = "path1" & cy2c & "\" & currentMonth & "\"
fp.SupplierMasterFilePath = "path 2" & cy2c & "\" & currentMonth & "\"

GetFilePaths = fp

End Function
Function GetDateItems() As String

Dim d As String

d.currentMonth = Format(Date, "mmmm") 'July
d.currentDate = Format(Date, "mm-dd-yy") '06-09-15
d.currentYear2char = Format(Date, "yy") '15
d.currentYear4char = Format(Date, "yyyy") '2015
d.currentFiscalMonth = Format(DateAdd("m", 1, Date), "mm") '08
d.wsDate = currentFiscalMonth & currentYear4char '082015

GetDateItems = d

End Function

我最初只是对每个
日期项
文件路径
文件名
项使用
ByRef
,但决定将它们放在各自的函数中,以清理我的代码

非常感谢您抽出时间

编辑:

@Brad我现在尝试使用对象而不是字符串

我现在在
d.currentMonth=…

Function GetDateItems() As String

Dim d As Object

d.currentMonth = Format(Date, "mmmm") 'July
d.currentDate = Format(Date, "mm-dd-yy") '06-09-15
d.currentYear2char = Format(Date, "yy") '15
d.currentYear4char = Format(Date, "yyyy") '2015
d.currentFiscalMonth = Format(DateAdd("m", 1, Date), "mm") '08
d.wsDate = currentFiscalMonth & currentYear4char '082015

GetDateItems = d

End Function

基于此:,我觉得我做得不对,但我不确定如何正确执行。

不幸的是,您无法在VBA中实际传递函数,OpenPullerReports()认为它传递了2个值。

因此,调用
openpullerdreports(getfilepath(),GetFileNames())
认为它是从
getfilepath()
GetFileNames()获取输出
由于没有将正确的输入传递给它们,因此会出现错误。

我不确定这是编码的好方法,但如果您希望以这种方式传递值,则应使用用户定义的类型。下面是一个模块,它提供了与您描述的类似的功能

如果运行此命令:

call OpenPulledReports(GetFilePaths(GetDateItems()), GetFileNames(GetDateItems()))
您将(在调试窗口中)获得以下信息:

路径218\April\SM-blah4-04-23-18.xls的部件主控

Option Compare Database

Type TDateItems
    currentMonth As String
    currentDate As String
    currentYear2Char As String
    currentYear4Char As String
    currentFiscalMonth As String
    wsDate As String
End Type

Type TFiles
    FargoPlant As String
    Logistics As String
    PES As String
    Torreon As String
    FargoSM As String
    TorreonSM As String
End Type

Type TFilePaths
    PartMasterFilePath As String
    SupplierMasterFilePath As String
End Type

Sub OpenPulledReports(ByRef GFP As TFilePaths, ByRef GFN As TFiles)
    ' do things with strings
    Debug.Print GFP.SupplierMasterFilePath & GFN.Torreon
End Function

Function GetFilePaths(ByRef GDI As TDateItems) As TFilePaths
    Dim fp As TFilePaths
    Dim cy2c As String
    cy2c = GDI.currentYear2Char
    fp.PartMasterFilePath = "path1" & cy2c & "\" & GDI.currentMonth & "\"
    fp.SupplierMasterFilePath = "path 2" & cy2c & "\" & GDI.currentMonth & "\"
    GetFilePaths = fp
End Function

Function GetFileNames(ByRef GDI As TDateItems) As TFiles
    Dim f As TFiles
    Dim cd As String
    cd = GDI.currentDate
    f.FargoPlant = "part master for SM - blah1 - " & cd & ".xls"
    f.Logistics = "part master for SM - blah2 - " & cd & ".xls"
    f.PES = "part master for SM - blah3 - " & cd & ".xls"
    f.Torreon = "part master for SM - blah4 - " & cd & ".xls"
    f.FargoSM = "Supplier Master - blah5 - " & cd & ".xls"
    f.TorreonSM = "Supplier Master - blah6 - " & cd & ".xls"
    GetFileNames = f
End Function

Function GetDateItems() As TDateItems
    Dim d As TDateItems
    d.currentMonth = Format(Date, "mmmm") 'July
    d.currentDate = Format(Date, "mm-dd-yy") '06-09-15
    d.currentYear2Char = Format(Date, "yy") '15
    d.currentYear4Char = Format(Date, "yyyy") '2015
    d.currentFiscalMonth = Format(DateAdd("m", 1, Date), "mm") '08
    d.wsDate = currentFiscalMonth & currentYear4Char '082015
    GetDateItems = d
End Function

您得到的参数不是可选的,因为
OpenPulledReports
认为您传递的是
GetFileNames
getfilepath
的结果,而不是函数本身。我认为在VBA中不能像那样传递函数。这是较新的语言功能。无法在VBA中传递函数。您可以使用
应用程序按名称调用函数。运行
或使用
CallByName
调用对象的方法,但无法传递实际函数。在我看来,您似乎不需要传递它们,为什么不直接传递函数的结果呢?@Brad和@GSerg好的,所以我试图让
GetFileNames()
接受
GetDateItems(currentDate)
作为参数,但我似乎无法获得正确的语法。既然函数参数不是一个选项,那么返回使用
ByRef
处理所有事情可能会更明智吗?编辑:@mumfyCallOpenPullerReports(getFilePath(GetDateItems)、getFileName(GetDateItems))并忽略因此放在末尾的任何破折号line@SteveRindsberg嗯,这是最接近工作的。没有编译器错误,但在输入函数时出现
类型不匹配错误