我試圖打開一個文件中的第三個工作表,以獲取信息的來源。該.FileDialog
工作正常,我可以選擇一個文件,但由於線路Set wbR = wb.Worksheets(3)
附近的代碼,因爲wb.Worksheets(3) = <Object variable or With block variable not set>
本節的底部我不斷收到run-time error '91'
。這使我相信我的Set wb = Workbooks.Open(myFile)
不能正常工作,並返回Nothing
,而是通過其他人如何使用.FileDialog
打開的文件看後,我看不到我的怎麼是不同的,將無法正常工作。任何幫助或建議將不勝感激。VBA錯誤「91」 - 找不到錯誤
'Set variables
Dim wb As Workbook 'Workbook to open
Dim wbR As Worksheet 'This is the raw data on the new workbook
Dim wsL As Worksheet 'Worksheet in current file
Dim myFile As String 'File to open
Dim FilePicker As FileDialog
'Set light chain hit worksheet
Set wsL = ThisWorkbook.Worksheets(3)
'Optimizes Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'Retrieve target file
Set FilePicker = Application.FileDialog(msoFileDialogFilePicker)
'Opens folder-picking window
With FilePicker
.Title = "Select a file."
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myFile = Dir(.SelectedItems(1))
End With
'If folder is not selected
NextCode:
myFile = myFile
If myFile = "" Then GoTo ResetSettings
'Set variable equal to opened workbook
Set wb = Workbooks.Open(myFile)
'Ensure Workbook has opened before moving on to next line of code
DoEvents
'Sets worksheet for importing
Set wbR = wb.Worksheets(3)
*宏繼續過去的最後一行,但它並沒有得到它只因這錯誤
你有3個工作表?你可能是指'set wbR = wb.Worksheets(「Sheet3」)'?另外,在調試時,保持'ScreenUpdating'處於ON狀態,然後用'F8'遍歷代碼,看看你的'wb'工作簿是否實際打開。 – BruceWayne
嘗試像'如果不WB是沒什麼然後設置WBR = wb.Worksheets(3)'這將阻止它試圖在不將其放置在世界銀行工作。這將確認或否認這是問題的根源。 –
我的代碼沒有出現錯誤。確保兩個工作簿都有3個工作表。布蘭登在設置工作簿時對於安全是正確的,但由於它在我的機器上運行良好,我認爲這可能不是問題。 –