在子例程中保留 VBA 函数值

Posted

技术标签:

【中文标题】在子例程中保留 VBA 函数值【英文标题】:Keep VBA Function Value in Sub Routines 【发布时间】:2020-12-08 19:50:32 【问题描述】:

我有一个小问题,我不知道如何解决。我有 2 个功能,允许我的用户选择模板文件和文件夹,目标是让我保留模板文件和文件夹路径的位置,以便将其用于记录集,但子例程中的唯一问题是值这些路径在我完成选择后立即通过,因此我可以使用它们,所以这是我的代码

Function SaveExcelDialog() As String
Dim strSelectedFolder As String
Dim strGetFolder As String

'Choosing the location of the folder where i will save all my recordset
With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select a Folder" 'window title
    .AllowMultiSelect = False   'avoiding multiple selection
    .Show
    strSelectedFolder = .SelectedItems.Item(1) 'taking the selected path folder
End With
GetFolder = strSelectedFolder 'returning the path of the folder
End Function
Public Function ChooseTemplateFile() As String

' my variables
Dim strSelectedTemplateFile As String
Dim strTemplatePath As String

'selecting a template file
With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select a Template File" 'window title
    .AllowMultiSelect = False 'avoiding multiple selection
    .Filters.Add "Classeur Excel", "*.xls; *.xlsx; *.xlsm" 'filter all the file that are allow to be choosen
    .Show
    
    'storing the path of my excel file
   TemplateFile = .SelectedItems.Item(1) 'keeping the path
End With
strTemplatePath = strSelectedTemplateFile 'returning  the path string value
End Function

这两个例子有效,但是我遇到的问题是在我的子程序中。一旦我调用我的函数并且该函数已被执行,这两个函数的值就不会保留,以后我需要它们时就不能使用这些变量了。

Private Sub Run_Click()
'My Data Variables
Dim strCountry, strSelectedYear, strLink, strCountryLink, strDomain, strDomainLink, strDateLink, DOsSQL As String
Dim iSelectedYear As Integer
Dim strGetFolder, strTemplatePath As String
Dim i As Long
'Variables for query
Dim qdf As DAO.QueryDef
Dim qdfDOs As DAO.QueryDef

Dim rs As DAO.Recordset
Dim rsDOs As DAO.Recordset

strTemplatePath = ChooseTemplateFile() ' value are not kept they are erase as soon as i go to the next line
strGetFolder = SaveExcelDialog() ' value are not kept they are erase as soon as i go to the next line

'taking the value of the file and the folder picked
Dim db As DAO.Database
Dim arrReports(4, 3) As String
Dim strReportTitle As String....

【问题讨论】:

【参考方案1】:

您的值消失的原因是您将函数的结果分配给变量,但您没有从函数返回任何结果。

相反,您似乎正在尝试从函数内部为外部变量赋值。

这可以通过一些修改来工作,但执行以下操作更常见(也更安全):

Function SaveExcelDialog() As String
    'Choosing the location of the folder where i will save all my recordset
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder" 'window title
        .AllowMultiSelect = False   'avoiding multiple selection
        .Show
        'To return a value from a function, assign it to the function's name.
        SaveExcelDialog = .SelectedItems.Item(1) 
    End With
End Function
Public Function ChooseTemplateFile() As String
    'selecting a template file
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select a Template File" 'window title
        .AllowMultiSelect = False 'avoiding multiple selection
        .Filters.Add "Classeur Excel", "*.xls; *.xlsx; *.xlsm" 'filter all the file that are allow to be choosen
        .Show
        'Again, let's just assign the result to the name, in order to
        'return the value.
        ChooseTemplateFile = .SelectedItems.Item(1)
    End With
End Function

如果您愿意,您可以编写一个测试过程来确保您的值存储在变量中。

Public Sub TestFilePickers()

    Dim strGetFolder, strTemplatePath As String

    strTemplatePath = ChooseTemplateFile()
    strGetFolder = SaveExcelDialog()

    Debug.Print(strTemplatePath)
    Debug.Print(strGetFolder)

End Sub

【讨论】:

非常感谢您的回答,但它不起作用我仍然无法将值保持足够长的时间,以便在我完成后立即将其作为变量参数发送到我的记录集函数并继续它只是擦除值,所以每次我将值作为参数发送时它都是空的,它甚至不会调试。打印值 在您的问题中,能否请您在代码中包含您尝试使用这些值的位置?目前我看到您将它们分配给变量,但我看不出您要对这些变量做什么。 我在我的主子例程 RecordsetToExcel rs、“Invoicing_” & strCountry & “” & strSelectedYear & “” & arrReports(i, 0) & “.xlsx”中调用了另一个函数"、strGetFolder、strTemplatePath 以及我试图检索值以便将它们发送到这个函数的位置 Public Sub RecordsetToExcel(rs As Recordset, excelFileName, strGetFolder, strTemplatePath As String) 对不起,我对ChooseTemplateFile = .SelectedItems.Item(1) 并不小心,真的很抱歉

以上是关于在子例程中保留 VBA 函数值的主要内容,如果未能解决你的问题,请参考以下文章

在子例程中退出 Select Case

仅在子例程中需要时才使用 Perl 模块

模块化程序-子例程

将两个或多个数组传递给 Perl 子例程

在 FORTRAN 子例程中传递不同的变量集

如何在Perl子例程中使用$ a和$ b