我有一组文件,我想用文件名循环:2015年预算,2016年预算,2017年预算到2022年 . 我使用Dir循环使用它们 .
Dim OpenWb as workbook
path = Dir("C:\pathtofile\Budget 20??.xlsx") 'Using the ? Dir Wildcard
filepath = "C:\pathtofile\" 'Since Dir returns file name, not the whole path
myHeadings = Split("Januari,Februari,Mars,April,Maj,Juni,Juli,Augusti,September,Oktober,November,December", ",")
j = 0
Do While Len(path) > 0
i = 0
If Dir = "Budget 2014.xlsx" Then
j=0
Else
For i = 0 To UBound(myHeadings)
Set openWb = Workbooks.Open(filepath & path)
MsgBox Len(path)
Set openWs = openWb.Sheets(myHeadings(i))
If openWs.Range("C34") = 0 Then
currentWb.Sheets("Indata").Cells(70, i + 27 + 12 * (j + 1)).Value = ""
Else
currentWb.Sheets("Indata").Cells(70, i + 27 + 12 * (j + 1)).Value = openWs.Range("C34")
End If
Next i
End if
path = Dir
j= j + 1
Loop
问题是在文件路径中还有一个名为Budget 2014的文件,我不想循环,因为1)没有必要,已经计算了值和2)因为它在循环中搞砸了我的索引
更新了我的代码 . 但是在for i = 0 ...循环中使用msgBox(path)返回“Budget 2014.xlsx”,我不想循环,因此这与我的j下标“混乱” .
3 回答
您可以使用Year方法 . 就像是,
你也可以试试这个: