每個文件我有以下VBA代碼:通過文件和自動循環填充公式使用VBA
BD.Sheets("Sheet1").Range("F" & Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row).Formula = "=SUMIF('" & spath & "[" & itm & "]Sheet1'!$D$13:$D$" & LastRow2 & ",D" & BD.Sheets("Sheet1").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row & ",OFFSET('" & spath & "[" & itm & "]Sheet1'!$D$13:$D$" & LastRow2 & ",0,MATCH(E" & BD.Sheets("Sheet1").Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row & ",'" & spath & "[" & itm & "]Sheet1'!$D$12:$R$12,0)-1))"
With BD.Sheets("Sheet1")
.Range("F" & .Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row).AutoFill Destination:=.Range("F" & .Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row & ":F" & .Cells(Rows.Count, 4).End(xlUp).Row)
End With
(不介意的很長SUMIF公式)
我通過循環使用下面的代碼文件:
spath = "P:\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 (code above included)
End If
Next itm
對於每一個文件,說我有以下的列和我想要的那個公式做的就是在列6放置在下一個可用電池(根據我的代碼,第6列是金額每個文件
RptLOB ECMAccount Amount
Disability GEP 20 ---> (=SUMIF formula for file 1)
MSL GEP ..
Contingency GEP .. ---> drag down to here (end of file 1)
Disability GEP 30 ---> (=SUMIF formula for file 2)
MSL GEP ..
Contingency GEP .. ---> drag down to here (end of file 2)
等:),並拖累,直到第4列(的最後一行再次,按我的代碼,第4列是RptLOB)
例。
我以爲我的第一個代碼實際上將SUMIF公式放在金額列的下一個可用單元格中並將其向下拖動可以工作,但它只是將SUMIF公式放在金額(金額在F1之後的第一行 - so只有20個正在顯示),沒有別的東西被拖拽下來......我不太清楚我的代碼有什麼問題,無論是自動填充還是它如何讀取每個文件。
如果有人對此問題有任何洞見或需要更多的澄清,請隨時發表評論/問。
任何幫助將不勝感激!
編輯: 即使我只用一個文件,並把在另一個類似的代碼
With BD.Sheets("Sheet1")
.Range("F" & .Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row).Select
Selection.AutoFill Destination:=.Range("F" & .Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row & ":F" & .Cells(Rows.Count, 4).End(xlUp).Row)
End With
...在F2只有一個值顯示出來,並沒有下
我將檢查'Rows.Count',看看它是否與此有關...公式不會進入打開的工作簿,它會出現在BD工作簿中,這就是爲什麼我有'BD.Sheets(「Sheet1」)'。 – Kristina
找到了解決方法,謝謝! – Kristina