Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8546947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:09:09+00:00 2026-06-11T13:09:09+00:00

I have the following VBA code: BD.Sheets(Sheet1).Range(F & Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row).Formula = =SUMIF(‘ &

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:09:11+00:00Added an answer on June 11, 2026 at 1:09 pm

    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:

    ...
    ''Open each file found
    For Each itm In Split(strFileNames, ",")
        If itm <> "" Then
            Set wb = Workbooks.Open(spath & itm)
            Set ws = ws.sheets(whateversheet)
    
            ws.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 ws
                .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
        End If
    Next itm
    

    I might also make sure that

    Rows.Count
    

    is coming up correctly. Not sure how that’s calculated, but it could cause some trouble when AutoFill is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am have the following code for excel vba that will email a range
I have something of the following : Sheet 1 (Name Range : Code) Column
Win-XP / Excel 2003 / VBA .... I have the following piece of code
I have the following code: CurrentDb.Execute DROP TABLE [ & DatabaseName & ].[ &
I have the following in a piece of VBA code: For i = 1
I have the following code for handling userforms on my spreadsheet VBA macros: Option
I have the following vba-code in an MS-Access97-frontend which opens a word-document stored on
I have the following VBA code: mainFile = ActiveWorkbook.Name 'select all excel files in
I have the following VBA code and I am getting the error message stated
I have tried the following VBA code in the [A Report] event handlers as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.