我的代码一直运行到今天。我不确定为什么在设置块变量时出现错误。错误发生在我的vlookup中。
Sub oversub()
Application.ScreenUpdating = False
'Latest File Code
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "C:\Users\TAmon1\Desktop\....."
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.csv", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
'Variables for Vlookup
Dim wbcsv As Workbook, wbplanning As Workbook
Set wbplanning = Workbooks("Planning_tool.xlsm")
Set wbcsv = Workbooks.Open(MyPath & LatestFile)
Dim wb As Workbook
Set wb = Workbooks.Open(MyPath & LatestFile)
Windows("Planning_tool.xlsm").Activate
#The error occurs here
wbplanning.Sheets(1).Range("N2").FormulaR1C1 = _
"=VLOOKUP(C[-13],'" & wbcsv.Name & "'!C1:C11,11,FALSE)"
Range("N2").Select
Selection.AutoFill Destination:=Range("N2:N2000")
wbplanning.Sheets(1).Range("o2").FormulaR1C1 = _
"=VLOOKUP(C[-14],'" & wbcsv.Name & "'!C1:C11,5,FALSE)"
Range("o2").Select
Selection.AutoFill Destination:=Range("o2:o2000")
Columns("N:O").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
wb.Close savechanges:=False
End Sub
我的vlookup对象变量未设置错误。该代码较早就可以正常工作,所以我不确定从昨天开始有什么变化。