ExecuteExcel4Macro,包含撇号的路径
ExecuteExcel4Macro, path containing an apostrophe
这将是我在此站点上的第一个问题,请耐心等待。
所以,我正在尝试利用 ExecuteExcel4Macro 函数来引用不同工作簿中的值,而不必打开工作簿,因为它必须循环遍历目录中的许多工作簿,并引用每个工作簿上的相同单元格。
这一行出现的问题:
wbRef = "'" & folderName & "[" & myDir & "]" & thatSheet & "'!"
这会导致此行的 运行 时间错误 1004:
month = CStr(ExecuteExcel4Macro(wbRef & Range("D4").Address(, , xlR1C1)))
假设,
folderName = "C:\test\Accounts\O'Malley\Summary\"
,出现错误
由于 folderName 包含撇号,ExecuteExcel4Macro 无法将 wbRef 识别为文件夹路径,而是在路径字符串中过早地关闭该路径,因此导致错误。
所以我的问题是:
有没有办法绕过这个撇号,而不必更改文件夹名称,也不必打开子文件夹中的每个单独的工作簿?
我试过使用双引号,但似乎没有成功。
下面是我的代码草稿,或者至少是上下文。
Sub refMonth()
Dim thisWb as Workbook, folderName as String, myDir as String, wbRef as String, thatSheet as String, month as String
Set thisWb = ActiveWorkbook
folderName = SelectFolder(thisWb)
If folderName = vbNullString Then GoTo Done
myDir = Dir(folderName & "*.xls")
thatSheet = "Sheet1"
wbRef = "'" & folderName & "[" & myDir & "]" & thatSheet & "'!"
Do Until myDir = vbNullString
month = CStr(ExecuteExcel4Macro(wbRef & Range("D4").Address(, , xlR1C1)))
'Do a lot of stuff, which works when in a folder without an apostrophe
myDir = Dir()
wbRef = "'" & folderName & "[" & myDir & "]" & thatSheet & "'!"
Loop
Done:
End Sub
Function SelectFolder(thisWb As Workbook)
Dim diaFolder As FileDialog, DirName As Variant
' Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.InitialFileName = strFolder(thisWb.Path)
If diaFolder.Show = True Then
'diaFolder.Show
DirName = diaFolder.SelectedItems(1)
If Right(DirName, 1) <> "\" Then
DirName = DirName & "\"
End If
Else
Set diaFolder = Nothing
Exit Function
End If
Set diaFolder = Nothing
SelectFolder = DirName
End Function
Function strFolder(ByVal strFolder0) As String
strFolder = Left(strFolder0, InStrRev(strFolder0, "\") - 1) & "\"
End Function
感谢任何帮助,即使只是告诉我不可能绕过撇号。
我在这里找不到答案,但如果有的话,请指出正确的方向。
您需要将撇号加倍才能转义:
wbRef = "'" & Replace$(folderName & "[" & myDir & "]" & thatSheet, "'", "''") & "'!"
这将是我在此站点上的第一个问题,请耐心等待。
所以,我正在尝试利用 ExecuteExcel4Macro 函数来引用不同工作簿中的值,而不必打开工作簿,因为它必须循环遍历目录中的许多工作簿,并引用每个工作簿上的相同单元格。
这一行出现的问题:
wbRef = "'" & folderName & "[" & myDir & "]" & thatSheet & "'!"
这会导致此行的 运行 时间错误 1004:
month = CStr(ExecuteExcel4Macro(wbRef & Range("D4").Address(, , xlR1C1)))
假设,
folderName = "C:\test\Accounts\O'Malley\Summary\"
,出现错误
由于 folderName 包含撇号,ExecuteExcel4Macro 无法将 wbRef 识别为文件夹路径,而是在路径字符串中过早地关闭该路径,因此导致错误。
所以我的问题是:
有没有办法绕过这个撇号,而不必更改文件夹名称,也不必打开子文件夹中的每个单独的工作簿?
我试过使用双引号,但似乎没有成功。
下面是我的代码草稿,或者至少是上下文。
Sub refMonth()
Dim thisWb as Workbook, folderName as String, myDir as String, wbRef as String, thatSheet as String, month as String
Set thisWb = ActiveWorkbook
folderName = SelectFolder(thisWb)
If folderName = vbNullString Then GoTo Done
myDir = Dir(folderName & "*.xls")
thatSheet = "Sheet1"
wbRef = "'" & folderName & "[" & myDir & "]" & thatSheet & "'!"
Do Until myDir = vbNullString
month = CStr(ExecuteExcel4Macro(wbRef & Range("D4").Address(, , xlR1C1)))
'Do a lot of stuff, which works when in a folder without an apostrophe
myDir = Dir()
wbRef = "'" & folderName & "[" & myDir & "]" & thatSheet & "'!"
Loop
Done:
End Sub
Function SelectFolder(thisWb As Workbook)
Dim diaFolder As FileDialog, DirName As Variant
' Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.InitialFileName = strFolder(thisWb.Path)
If diaFolder.Show = True Then
'diaFolder.Show
DirName = diaFolder.SelectedItems(1)
If Right(DirName, 1) <> "\" Then
DirName = DirName & "\"
End If
Else
Set diaFolder = Nothing
Exit Function
End If
Set diaFolder = Nothing
SelectFolder = DirName
End Function
Function strFolder(ByVal strFolder0) As String
strFolder = Left(strFolder0, InStrRev(strFolder0, "\") - 1) & "\"
End Function
感谢任何帮助,即使只是告诉我不可能绕过撇号。
我在这里找不到答案,但如果有的话,请指出正确的方向。
您需要将撇号加倍才能转义:
wbRef = "'" & Replace$(folderName & "[" & myDir & "]" & thatSheet, "'", "''") & "'!"