+ Reply to Thread
Results 1 to 4 of 4

SumIF applied to a dynamic range

Hybrid View

  1. #1
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    SumIF applied to a dynamic range

    Hey all,

    I hope to apply a SumIF function to a range defined by a few variables (lastRow, lastCol ect.). I prefer the result left in the cells as just the number, but getting the result by applying a formula in the cell will work as well. I have tried a few things, but couldn't make it work, so I really hope someone can help me out. Please see attached file, as it shows better what I need done. Thanks a lot in advance for you effort!
    To make it easier I took most of the code out, leaving this:

    Sub makro1()
    
    Dim i As Variant
    Dim lastRow As Integer
    Dim actCol As Integer
    Dim actRow As Integer
    Dim lastCol As Integer
    
    'the variables are determined in the code I took out, but will vary from time to time changing according to the data.
    lastCol = 11
    lastRow = 10
    actCol = 5
    actRow = 6
    i = 3
    
    With Cells(lastRow + 3, actCol).Resize(lastRow - actRow, i)
    
    .Formula = Application.WorksheetFunction.SumIf(Range("$E$6:$K$6"), Range("E$12") & "*", Range("$E7:$K7"))
    
    End With
    
    End Sub
    Attached Files Attached Files
    Last edited by timtim89; 02-23-2013 at 11:27 PM.

  2. #2
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    Re: SumIF applied to a dynamic range

    I considered the .FormulaR1C1 method as well, but it didn't seem that I could determine the span of the formula with ranges. Any suggestions, please?

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: SumIF applied to a dynamic range

    I think what you're trying to do is this:

    Dim actCol As Integer
    Dim actRow As Integer
    Dim lastCol As Integer
    
    lastCol = 11
    lastRow = 10
    actCol = 5
    actRow = 6
    i = 3
    
    With Cells(lastRow + 3, actCol).Resize(lastRow - actRow, i)
      .Formula = "=SumIf($E$6:$K$6,E$12& ""*"", $E7:$K7)"
    End With
    
    End Sub
    If you're using WorksheetFunction.SumIF that's allowing you to run a SUMIF from within a macro, if you want to insert a formula you just write it into the text of the formula.

  4. #4
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    Re: SumIF applied to a dynamic range

    Thanks for the answer Andrew. The thing is that the formula depends on the parameters, which will change from time to time. Therefore, I need a formula where the ranges are given by the parameters. I tried something like the following, but had to give up cause I simply couldn't figure out how to do it.

    .FormulaR1C1 = "=SUMIF(R[" & -(lastRow - actRow + 3) & "]C[" & actCol & "]:R[" & actRow & "]C[" & lastCol & "],R12C&""*"",R[-6]C5:R[-6]C11)".......
    or

    .Formula = "=sumif(" & Cells(actRow, actCol).Address & ":" & Cells(actRow, lastCol).Address & ";" & Cells(lastRow + 3, _
    Application.Cells().Column).Value & "" * "" & ";" & Cells(actRow + 1, actCol).Address & ":" & Cells(actRow + 1, lastCol).Address & ")"
    Last edited by timtim89; 02-24-2013 at 01:01 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1