假设我有以下路径和文件名:
P:\...\Annual and Quarterly Budget Data\ECMQA 2012Q1.xls
我想编写一个执行以下操作的 if then 语句(不确定我的语句是否设置正确):
If InStr(1, "P:\...\Annual and Quarterly Budget Data\ECMQA 2012Q1.xls", "QA", vbTextCompare) > 0 Then
BD.Sheets("Sheet1").Range("C2").value = "2012Q1"
End If
我希望它自动从文件中读取,而不是仅仅输入“2012Q1”。问题是我实际上循环了 12 个左右的文件,并且有两种类型,“ECMQA 2012Q1.xls”(或 ECMQB 2012Q2.xls 等)和“ECM 年度预算 2012.xlsx”
如果我的文件是年度文件(如果文件包含“年度”),那么我想要:
BD.Sheets("Sheet1").Range("C2").value = "2012"
我希望它从实际文件中读取它,与另一个文件相同......不是我输入“2012”
有没有办法做到这一点?
任何帮助将不胜感激!
编辑:
这是循环:
Dim wb As Workbook, sFile As String, sPath As String
Dim itm As Variant
Dim strFileNames As String
sPath = "C:\Actuary\Cash Flow Forecast\Annual and Quarterly Budget Data\"
''Retrieve the current files in directory
sFile = Dir(sPath)
Do While sFile <> ""
strFileNames = strFileNames & "," & sFile
sFile = Dir()
Loop
''Open each file found
For Each itm In Split(strFileNames, ",")
If itm <> "" Then
Set wb = Workbooks.Open(sPath & itm)
''DO LOTS OF CALCULATIONS
'wb.Close True
End If
Next itm