Consulting

Results 1 to 3 of 3

Thread: VBA Formula with dynamic range. Please help

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Posts
    70
    Location

    VBA Formula with dynamic range. Please help

    Hi, I'm struggling with a formula in VBA. I need a formula for a dynamic range. This is the formula I have in my spreadsheet =SUM(P10:Q16)/(SUM(C10:C16)-SUM(D1016))

    I need help with the code below, which I need to put in the macro. I want it to calculate the cells from row 5 to the last row. The code below doesn't work, but should hopefully show you roughly what I need? I need it to work like the formula above, but with a dynamic range. Can anyone please help?

    With Sheets("RTA") 'put your sheet name here
        lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("U" & lLastRow + 2).Formula = "=Sum(P5:Q" & lLastRow & ")/(Sum(c5:c" & lLastRow & ")-Sum(d5:d" & lLastRow & "))"
    End With

  2. #2
    VBAX Regular
    Joined
    Feb 2018
    Posts
    70
    Location
    *This post has also been placed here https://www.mrexcel.com/forum/excel-...ml#post5211919

  3. #3
    VBAX Regular
    Joined
    Feb 2018
    Posts
    70
    Location
    Quote Originally Posted by leemcder View Post
    Hi, I'm struggling with a formula in VBA. I need a formula for a dynamic range. This is the formula I have in my spreadsheet =SUM(P10:Q16)/(SUM(C10:C16)-SUM(D1016))

    I need help with the code below, which I need to put in the macro. I want it to calculate the cells from row 5 to the last row. The code below doesn't work, but should hopefully show you roughly what I need? I need it to work like the formula above, but with a dynamic range. Can anyone please help?

    With Sheets("RTA") 'put your sheet name here
        lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("U" & lLastRow + 2).Formula = "=Sum(P5:Q" & lLastRow & ")/(Sum(c5:c" & lLastRow & ")-Sum(d5:d" & lLastRow & "))"
    End With
    Solved it myself - code below worked

    With Sheets("RTA") 'put your sheet name here
        lLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("U" & lLastRow + 2).Formula = "=Sum(P5:Q" & lLastRow & ")/(Sum(c5:c" & lLastRow & ")-Sum(d5:d" & lLastRow & "))"
    End With

Posting Permissions

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