2
這將是我在這個網站上的第一個問題,請耐心等待。ExecuteExcel4Macro,包含撇號的路徑
因此,我試圖利用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
任何幫助表示讚賞,即使它只是告訴我,不可能避開撇號。
我在這裏找不到答案,但是如果有的話,請指點我正確的方向。
我會測試,THX :) – Alex4336
你,我的朋友,是我的救命恩人:) – Alex4336