Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建一个接受变量的过程,然后将另一个变量返回到Excel VBA中的调用过程_Excel_Vba_Procedure_Output Parameter - Fatal编程技术网

创建一个接受变量的过程,然后将另一个变量返回到Excel VBA中的调用过程

创建一个接受变量的过程,然后将另一个变量返回到Excel VBA中的调用过程,excel,vba,procedure,output-parameter,Excel,Vba,Procedure,Output Parameter,我想创建一个功能,允许我将一个变量传递给一个过程,让它基于该变量打开一个文件,然后将文件路径返回到调用过程中。我有打开文件等的代码,但由于过程中有多个地方可以调用它,我不希望它在那里,而只是将文件路径返回到变量中(然后可以使用该变量从打开的文件加载字段) 下面是加载我正在使用的文件的代码,我如何将其转换为一个过程来执行我需要的操作 'Open the file NameOfFile = ActiveWorkbook.Worksheets("Parameters")

我想创建一个功能,允许我将一个变量传递给一个过程,让它基于该变量打开一个文件,然后将文件路径返回到调用过程中。我有打开文件等的代码,但由于过程中有多个地方可以调用它,我不希望它在那里,而只是将文件路径返回到变量中(然后可以使用该变量从打开的文件加载字段)

下面是加载我正在使用的文件的代码,我如何将其转换为一个过程来执行我需要的操作

        'Open the file
        NameOfFile = ActiveWorkbook.Worksheets("Parameters").Cells(8, 2).Value
        PathToFile = Left$(NameOfFile, InStrRev(NameOfFile, "\") - 1)
        FileNameNoPath = Mid$(NameOfFile, InStrRev(NameOfFile, "\") + 1)
        NameOfFile = FileNameNoPath
        CompleteFilePath = PathToFile & "\" & NameOfFile

        On Error Resume Next
        Set File1 = Workbooks(NameOfFile)

        If Err.Number <> 0 Then
        'Open the workbook
          Err.Clear
          Set File1 = Workbooks.Open(CompleteFilePath, UpdateLinks:=False)
          CloseIt = True
        End If

        'Check and make sure workbook was opened
        If Err.Number = 1004 Then
            MsgBox "File is missing, please check your path!" _
            & vbNewLine & NameOfFile
            Exit Sub
        End If
        On Error GoTo 0
”打开该文件
NameOfFile=ActiveWorkbook.Worksheets(“参数”).Cells(8,2).Value
PathToFile=Left$(NameOfFile,InStrRev(NameOfFile,“\”)-1)
FileNameNoPath=Mid$(NameOfFile,InStrRev(NameOfFile,“\”)+1)
NameOfFile=FileNameNoPath
CompleteFilePath=PathToFile&“\”&NameOfFile
出错时继续下一步
Set File1=工作簿(NameOfFile)
如果错误号为0,则
'打开工作簿
呃,明白了
Set File1=Workbooks.Open(CompleteFilePath,UpdateLinks:=False)
CloseIt=True
如果结束
'检查并确保工作簿已打开
如果错误编号=1004,则
MsgBox“文件丢失,请检查您的路径!”_
&vbNewLine和NameOfFile
出口接头
如果结束
错误转到0
你是说像这样

Option Explicit

Dim FilePath As String

Sub Sample()
    Dim FileToOpen As String

    FileToOpen = "C:\Temp\Sample.xlsx"

    OpenFile FileToOpen

    Debug.Print FilePath
End Sub

Sub OpenFile(strFile As String)
    FilePath = Left$(strFile, InStrRev(strFile, "\") - 1)

    '
    '~~> Rest of the code
    '
End Sub

嗯,不确定我是否遵循了你的代码-从外观上看,你是在调用过程中定义flepath,然后在其他地方打开它,从而避免需要将文件传递回-还是我的理解有误?据我所知,你希望从另一个子系统返回文件路径。我的理解正确吗?嗯,我一定解释得很糟糕。我想将filepath的单元格引用传递给调用子例程,然后在文件打开时返回工作簿变量。但现在看看,我明白你做了什么,这是有道理的。只需在调用过程中设置工作簿变量并远程打开文件。