Consulting

Results 1 to 6 of 6

Thread: Solved: Dynamic Sum Macro

  1. #1
    VBAX Regular
    Joined
    Feb 2011
    Posts
    26
    Location

    Solved: Dynamic Sum Macro

    I'm trying to write a macro that will search a row i.e the column headers for a text string i.e. sales, based on the column that sales is found in i would like for it to find the last used row in this column and sum this column and put the results in the same column and 1 cell below the last row found in the particular column. I'm close but can't seem to tweak things just right.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub Test()
    Dim cell As Range
    Dim Lastrow As Long

    On Error Resume Next
    Set cell = Rows(1).Find("Sales")
    On Error GoTo 0
    If Not cell Is Nothing Then

    Lastrow = Cells(Rows.Count, cell.Column).End(xlUp).Row
    cell.Offset(Lastrow, 0).FormulaR1C1 = "=SUM(R2C" & cell.Column & ":R[-1]C" & cell.Column & ")"
    End If
    End Sub[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Feb 2011
    Posts
    26
    Location
    what does the ".formulaR1C1" do?

  4. #4
    VBAX Regular
    Joined
    Feb 2011
    Posts
    26
    Location
    The code works perfect, but its not putting the results in the appropriate place since i changed the row where my columns headings are located What part of your code selects the cell to enter the formula? Thanks for the quick response and help!



    [vba]Public Sub Test()
    Dim cell As Range
    Dim Lastrow As Long

    On Error Resume Next
    Set cell = Rows(4).Find("*Sales*")
    On Error GoTo 0
    If Not cell Is Nothing Then

    Lastrow = Cells(Rows.Count, cell.Column).End(xlUp).Row
    cell.Offset(Lastrow, 0).FormulaR1C1 = "=SUM(R5C" & cell.Column & ":R[-4]C" & cell.Column & ")"
    End If
    End Sub[/vba]
    Last edited by Aussiebear; 09-18-2011 at 07:19 PM. Reason: Added vba tags to code

  5. #5
    VBAX Regular
    Joined
    Feb 2011
    Posts
    26
    Location
    i got it to work, but thank you for the all of your help on the code!

    [VBA]Public Sub Test()
    Dim cell As Range
    Dim Lastrow As Long

    On Error Resume Next
    Set cell = Rows(4).Find("*Sales*")
    On Error GoTo 0
    If Not cell Is Nothing Then

    Lastrow = Cells(Rows.Count, cell.Column).End(xlUp).Row
    cell.Offset(Lastrow - 3, 0).FormulaR1C1 = "=SUM(R5C" & cell.Column & ":R[-1]C" & cell.Column & ")"
    End If
    End Sub[/VBA]

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You should have mentioned that at the start
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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