我正在使用此代码,它之前工作得很好,但现在出于某种原因,我得到一个错误,只是说“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