我正在制作一个 VBA 程序来从 excel 文件中收集数据,所有名称:AAA- i 从 1 到 200,但跳过了一些数字。
我的代码运行良好但停止并要求我手动打开丢失的文件。我想跳过这些缺失的号码。我该如何进行?
另外,有时候我的日期是这样的:AAA-我 ddmmyyyy,我不知道如何忽略这部分。
`Sub proto()
Dim file As String
Dim i As Integer, nfile As Integer
Const DOSSIER = "C:\temp\FOLDER\"
Const chemin1 = "PROJECT- "
For i = 1 To 250
file = DOSSIER & "[" & chemin1 & i & ".xlsm" & "]"
Range("B" & i).Select
ActiveCell.Formula = "='" & file & "Sheet1'!K30"
Range("C" & i).Select
ActiveCell.Formula = "='" & file & "Sheet2'!K92"
Next i
end sub
`
谢谢,
程序创建和改进
试试这个:
Sub proto()
Dim file As String
Dim i As Long, o As Long
Const DOSSIER = "C:\temp\FOLDER\"
Const chemin1 = "PROJECT- "
o = 1
For i = 1 To 250
file = DOSSIER & "[" & chemin1 & i & ".xlsm" & "]"
If Dir(file) <> "" Then
Range("B" & o).Formula = "='" & file & "Sheet1'!K30"
Range("C" & o).Formula = "='" & file & "Sheet2'!K92"
o = o + 1
End If
Next i
End Sub
实际上是相同的代码,但它在创建链接之前测试文件是否存在,并且还使用
o
来跟踪数据被写入的行。 (如果文件不存在时你想要空行,你可以写到i
行)。