Results 1 to 6 of 6

Add values from one column based on values in another column

Threaded View

  1. #1
    Registered User
    Join Date
    07-16-2014
    Location
    Seattle, WA
    MS-Off Ver
    Office 365
    Posts
    67

    Question Add values from one column based on values in another column

    I am trying to add the values in column A for all rows that have the same value in column B. My code will sum but the values are incorrect, off by a value of 2 to 3. See attached spreadsheet.

    Option Explicit
    
    Sub calc()
        Dim LastRow As Integer
        Dim sum As Integer
        Dim x As Integer
        Dim i As Integer
        
        LastRow = ActiveSheet.UsedRange.Rows.Count
        sum = 0
        x = 2
        
        For i = 2 To LastRow
            If Cells(i, "B") = Cells(i + 1, "B") Then
                    sum = sum + Cells(i, "A").Value
            Else
                Cells(x, "D").Value = sum + Cells(i, "A").Value
                sum = 0
                x = x + 1
            End If
        Next i
    
    End Sub
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro that compares Values in a Column to Based on Values in Different Column
    By rjw524 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2016, 01:26 PM
  2. [SOLVED] Sum values in column based on unique values in correlating column.
    By ericbinfet in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-03-2015, 11:41 AM
  3. Replies: 5
    Last Post: 06-12-2014, 06:34 AM
  4. Replies: 1
    Last Post: 05-14-2014, 05:09 PM
  5. Calculate mean of column B values based on date values in column A
    By up&down in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-05-2013, 12:26 PM
  6. Replies: 3
    Last Post: 06-10-2010, 11:54 AM
  7. How do I total only specific values in a column based on the values in another column
    By Needy McHelpson in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-23-2009, 12:17 PM

Tags for this Thread

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