制作全局文件路径
Making A Global File Path
目标:使用打开函数允许用户对他们选择跨多个函数打开的文件执行 运行 命令(路径在 public 函数中)。
这是提示用户 select 文件的初始函数。该文件路径保存在变量 "path" 中。我制作函数 public 的目的是在多个领域(全球化)使用 "path"。
Public Function OpenFile1() As String
On Error GoTo Trap
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Open Sterling Shipment History" 'Name for file
.Filters.Clear
.ButtonName = " Open "
.AllowMultiSelect = False
End With
If fd.Show <> 0 Then OpenFile1 = fd.SelectedItems(1)
Leave:
Set fd = Nothing
On Error GoTo 0
Exit Function
Trap:
MsgBox Err.Description, vbCritical
Resume Leave
Dim path As String
path = OpenFile1() 'Calls in file
If path <> vbNullString Then Debug.Print path
Workbooks.Open (path)
'rename the path variable for each function
'so that I can call in different files with that name
End Function
这是试图从变量 "path" 调用文件路径的第二个函数的摘录,用它来打开工作簿并更改工作簿。
Sub Shipment_History()
Call OpenFile1
Dim sshist As Workbook
Set sshist = Workbooks.Open(path)
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
我也试过:
Sub Shipment_History()
Call OpenFile1
Workbooks.Open(path)
我的问题是它不允许我打开 "path"。
错误状态
"Run-time error '1004': Sorry, we couldn't find. Is it possible it was
moved, renamed or deleted?"
path 必须在任何函数或下标之外声明为 public 字符串,函数不需要是 public,它是变量。
在模块上试试这个:
Public path As String
Function setPathValue(ByVal dataPassed As String)
path = dataPassed
End Function
Sub givePathVal()
setPathValue ("This is path value")
End Sub
Sub showPathVal()
MsgBox path
End Sub
由于函数 returns 是一个字符串(路径)并且 public 仅可用,因此您不需要 public 变量来存储路径。
在本地声明路径变量并将其值设置为从函数返回的值(路径):
Sub Shipment_History()
Dim path as string
path = OpenFile1()
If path <> vbNullString Then Workbooks.Open(path)
End Sub
p.s。删除 Resume Leave
之后的所有内容,除了 End Function
语句。
目标:使用打开函数允许用户对他们选择跨多个函数打开的文件执行 运行 命令(路径在 public 函数中)。
这是提示用户 select 文件的初始函数。该文件路径保存在变量 "path" 中。我制作函数 public 的目的是在多个领域(全球化)使用 "path"。
Public Function OpenFile1() As String
On Error GoTo Trap
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Open Sterling Shipment History" 'Name for file
.Filters.Clear
.ButtonName = " Open "
.AllowMultiSelect = False
End With
If fd.Show <> 0 Then OpenFile1 = fd.SelectedItems(1)
Leave:
Set fd = Nothing
On Error GoTo 0
Exit Function
Trap:
MsgBox Err.Description, vbCritical
Resume Leave
Dim path As String
path = OpenFile1() 'Calls in file
If path <> vbNullString Then Debug.Print path
Workbooks.Open (path)
'rename the path variable for each function
'so that I can call in different files with that name
End Function
这是试图从变量 "path" 调用文件路径的第二个函数的摘录,用它来打开工作簿并更改工作簿。
Sub Shipment_History()
Call OpenFile1
Dim sshist As Workbook
Set sshist = Workbooks.Open(path)
Columns("E:E").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
我也试过:
Sub Shipment_History()
Call OpenFile1
Workbooks.Open(path)
我的问题是它不允许我打开 "path"。
错误状态
"Run-time error '1004': Sorry, we couldn't find. Is it possible it was moved, renamed or deleted?"
path 必须在任何函数或下标之外声明为 public 字符串,函数不需要是 public,它是变量。
在模块上试试这个:
Public path As String
Function setPathValue(ByVal dataPassed As String)
path = dataPassed
End Function
Sub givePathVal()
setPathValue ("This is path value")
End Sub
Sub showPathVal()
MsgBox path
End Sub
由于函数 returns 是一个字符串(路径)并且 public 仅可用,因此您不需要 public 变量来存储路径。
在本地声明路径变量并将其值设置为从函数返回的值(路径):
Sub Shipment_History()
Dim path as string
path = OpenFile1()
If path <> vbNullString Then Workbooks.Open(path)
End Sub
p.s。删除 Resume Leave
之后的所有内容,除了 End Function
语句。