我正在使用此代碼,它之前工作得很好,但現在出於某種原因,我得到一個錯誤,只是說「400」,我沒想到我改變了什麼。Excel VBA - 代碼用於工作,現在找不到問題
Sub getdata()
Dim xcell As Range
Dim ycell As Range
Dim sheetname As String
Dim wblist() As String
Dim i As Integer
Dim wbname As String
Dim j As Integer
i = 0
j = 0
FolderName = "C:\Documents and Settings\shahzad.khan\Desktop\CRs\LOG"
wbname = Dir(FolderName & "\" & "*.xls")
Application.ScreenUpdating = False
Do While wbname <> ""
i = i + 1
ReDim Preserve wblist(1 To i)
wblist(i) = wbname
wbname = Dir
Set ycell = Range(Cells(i + 3, 2), Cells(i + 2, 28))
Set xcell = Range(Cells(2, 3), Cells(2, 28))
sheetname = "loging form"
ycell.Formula = "=" & "'" & FolderName & "\[" & wblist(i) & "]" _
& sheetname & "'!" & xcell.Address
Loop
Do While j < 100
Cells(j + 3, 1).Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[6],4)"
Cells(3 + j, 1) = Val(Cells(3 + j, 1))
Cells(3 + j, 2).Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-1],'[CR Status.xlsx]Sheet1'!R3C1:R189C3,3,FALSE)"
If Cells(3 + j, 1).Value = 0 Then
Cells(3 + j, 1).Value = ""
Cells(3 + j, 2).Value = ""
End If
j = j + 1
Loop
Application.CutCopyMode = False
Application.ScreenUpdating = True
Cells(1, 1).Select
End Sub
我知道現在的代碼效率不高,但它工作。它似乎粘貼了我想要的大部分信息,但由於某些原因,它並未捕獲我試圖從中拉出的excel文件的第一列和最後一列,而第二個循環甚至沒有開始。此外,它不再訪問文件夾中的每個文件,它似乎停止在目錄結束前的大約4個文件。任何幫助將不勝感激,謝謝!
什麼行會失敗? – MalibuCusser
我不確定,有沒有找到什麼方法? – user960358
在代碼中設置斷點,或在調試菜單中使用Step Into。 – MalibuCusser