I have the following VBA code:
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
(don’t mind the very long SUMIF formula)
I’m looping through files using the following code:
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
For each file, say I have the following columns and what I want that formula to do is be placed on the next available cell in column 6 (as per my code, column 6 is Amount) and be dragged down until the last row of column 4 (again, as per my code, column 4 is RptLOB)
Ex:
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)
and so on for each file.
I thought my first code that actually places the SUMIF formula on the next available cell of the Amount column and dragging it down would work but it’s only placing the SUMIF formula on the first row after Amount (Amount is in F1 – so only the 20 is showing) and nothing else is being dragged down…I’m not quite sure what is wrong with my code, whether it’s the autofill or how it’s reading each file.
If anyone has any insight to this problem or needs any more clarification, feel free to comment/ask.
Any help will be appreciated!
EDIT:
Even if I only use one file and put in another similar code
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
…only one value in F2 shows up and nothing underneath
Is it possible that your Selection isn’t acting on the open worksheet? I’m not sure where BD is set, but make sure it’s all acting on what you think it’s acting on, so to speak.
I might revise the code to be sure of that like this:
I might also make sure that
is coming up correctly. Not sure how that’s calculated, but it could cause some trouble when AutoFill is called.