Consulting

Results 1 to 7 of 7

Thread: Solved: Macro to Sum values in Range to new Column

  1. #1

    Solved: Macro to Sum values in Range to new Column

    So I started working with Macros last week and I'm still figuring out how they work to process my datasheets. I have most of it figured out but I'm stuck on one last calculation.
    I have several ranges of items in a sheet that take up multiple rows. I need to get the total of each group and put it in the first row of the group. I tried the build in "Subtotal" function but that puts the total in the wrong place.

    Here's the layout

    Column1 Column 2 Column3
    a Total A
    a 3
    a 4
    a 2
    b Total B
    b 3
    b 3
    b 3

    I know this on is probably an easy one but....

    Thanks in advance.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,455
    Location
    Do you mean overwrite the first value with the sum of that group? Sum with or without the first value?
    ____________________________________________
    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 Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    (Please delete Double-post)
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  4. #4
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,676
    Location
    Use the following Function- put it in a Module.

    [VBA]Function TotalA(rng1 As Range)

    Dim Cell As Range
    Dim Total As Double

    Application.Volatile '<<<Makes function run whenever sheet recalculates
    For Each cell In rng1
    Total = Total + Cell.Value
    Next

    TotalA = Total

    End Function[/VBA]

    Just put =Total(***Range***) wherever you want to get a total. Just be sure to replace '***Range*** with the actuall cell ranges you want to work with.
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


  5. #5
    oops...new to the forums too. That didn't format right.
    Column 1 has the repeating data that creates the group.
    Column 2 has the Value that needs to be totaled
    Column 3 - Row 1 of each group is where I need the total to go.
    Thanks

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

    Public Sub ProcessData()
    Dim i As Long
    Dim LastRow As Long
    Dim EndRow As Long
    Dim sh As Worksheet

    Application.ScreenUpdating = False

    With ActiveSheet

    .Rows(1).Insert
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = LastRow To 2 Step -1

    If .Cells(i, "A").Value <> .Cells(i - 1, "A").Value Then

    .Cells(i, "C").Formula = "=SUMIF(A:A,A" & i & ",B:B)"
    End If
    Next i
    .Rows(1).Delete
    End With

    Application.ScreenUpdating = True

    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

  7. #7
    You guys are awesome!!!

Posting Permissions

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