次の 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は金額です)、列の最後の行までドラッグされます4 (ここでも、私のコードによると、列 4 は RptLOB です)
元:
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)
など、ファイルごとに。
Amount 列の次の使用可能なセルに SUMIF 数式を実際に配置し、それを下にドラッグする最初のコードは機能すると思いましたが、SUMIF 数式は Amount の後の最初の行にのみ配置されています (Amount は F1 にあるため、20 のみが表示)、他に何もドラッグされていません...オートフィルなのか、各ファイルの読み取り方法なのか、コードの何が問題なのかよくわかりません。
誰かがこの問題について洞察を持っている場合、またはさらに説明が必要な場合は、お気軽にコメント/質問してください。
どんな助けでも大歓迎です!
編集: 1 つのファイルのみを使用し、別の同様のコードを挿入しても
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 の 1 つの値のみが表示され、その下には何も表示されません