+ Reply to Thread
Results 1 to 16 of 16

Add two cells when values in another cell match

  1. #1
    Registered User
    Join Date
    03-23-2011
    Location
    La Paz Mexico
    MS-Off Ver
    Excel 2010
    Posts
    17

    Add two cells when values in another cell match

    Hello, I am trying to create a Macro to Add Two values when the values in another cell Match

    Here is an example:


    A B
    Amount Invoice Number
    $509.00 #46402418 Same #
    $450.00 #46417335
    $450.00 #46604515
    $450.00 #46497732
    ($59.00) #46402418 Same #
    $600.00 #46651112
    $800.00 #46653760

    New Row $450.00 #46402418

    In this case I have two columns A with the Price and B with the invoice number. I need a Macro that when the Invoice number match in column B Both values from Column A get added. In this case $509 + ($-59) = $450.

    Additionally I would need to clean up the sheet, delete both values that have been added and only leave the new Column with the total.

    Thanks!

  2. #2
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Add two cells when values in another cell match

    is it possible that after you delete the values that were added, same invoice number could appear again?
    And do you want results in different columns than A and B?

    It would help if you can attach a sample file with your data structure so we can write the macro into it and test it.
    1. Click on the * Add Reputation if you think this helped you
    2. Mark your thread as SOLVED when question is resolved

    Modytrane

  3. #3
    Registered User
    Join Date
    03-23-2011
    Location
    La Paz Mexico
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Add two cells when values in another cell match

    Quote Originally Posted by modytrane View Post
    is it possible that after you delete the values that were added, same invoice number could appear again?
    Yes invoice number should remain the same

    Quote Originally Posted by modytrane View Post
    And do you want results in different columns than A and B?
    They need to remain in the same column. All amounts on same column, all Invoice number in the same column


    Quote Originally Posted by modytrane View Post
    It would help if you can attach a sample file with your data structure so we can write the macro into it and test it.

    I will atach a Google Drive file as I am not sure how to Attach something to the forum

  4. #4
    Registered User
    Join Date
    03-23-2011
    Location
    La Paz Mexico
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Add two cells when values in another cell match

    Here is the Excel file, Sheet 1 is exactly how I get the Data, Sheet number 2 is how it should look

    Thanks for the help!

    https://drive.google.com/file/d/1w0A...ew?usp=sharing

  5. #5
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Add two cells when values in another cell match

    Can't see two sheets. Attach an excel file with two tabs to show before and after.

    To attach a file: Click on Go Advanced and then follow prompts to browse and upload a file.

  6. #6
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Add two cells when values in another cell match

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  7. #7
    Registered User
    Join Date
    03-23-2011
    Location
    La Paz Mexico
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Add two cells when values in another cell match

    File has been attached
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Hi !

    Easier with a workbook attachment with a before context worksheet and an after worksheet for the expected result …

  9. #9
    Registered User
    Join Date
    03-23-2011
    Location
    La Paz Mexico
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Add two cells when values in another cell match

    I am sorry I attached the wrong file. Here is the correct one
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question


    What should happen in the same case if the dates are different, which one to keep ?

  11. #11
    Registered User
    Join Date
    03-23-2011
    Location
    La Paz Mexico
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Add two cells when values in another cell match

    The date should be from the first one, Thanks!

  12. #12
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool

    A starter demonstration to paste to the (before) worksheet module :

    PHP Code: 
    Sub Demo1()
            
    Dim L&, R&, VW
                L 
    Me.UsedRange.Rows.Count
        With CreateObject
    ("Scripting.Dictionary")
            For 
    2 To L
                    V 
    Me.UsedRange.Rows(R).Value2
                
    If .Exists(V(14)) Then
                    W 
    = .Item(V(14))
                    
    W(13) = W(13) + V(13)
                   .
    Item(V(14)) = W
                
    Else
                   .
    Add V(14), V
                End 
    If
            
    Next
            
    If .Count 1 Then
                Me
    .UsedRange.Rows(+ .Count ":" L).Clear
                Me
    .UsedRange.Rows(2).Resize(.Count).Value2 Application.Index(.Items0)
            
    End If
               .
    RemoveAll
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  13. #13
    Registered User
    Join Date
    03-23-2011
    Location
    La Paz Mexico
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Add two cells when values in another cell match

    I might not be doing it right but i get an error msg

    Compile Error

    "Invalid use of Me Keyword"

    Error is being detected on this line:

    L = Me.UsedRange.Rows.Count

  14. #14
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Add two cells when values in another cell match

    Try replacing
    Please Login or Register  to view this content.
    With
    Please Login or Register  to view this content.
    in the 4 lines in the macro where "Me." is found.

    Alf

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    Or just following the direction just before the code …

  16. #16
    Registered User
    Join Date
    03-23-2011
    Location
    La Paz Mexico
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Add two cells when values in another cell match

    Thank you so much Marc L. It works perfectly! I really appreciate it!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Sum cell values into another sheet IF values in cells from two sheets match
    By RaDoubleD in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-26-2018, 09:41 PM
  2. Flashing cells when 2 cell values don't match
    By tony1887 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-03-2017, 12:50 AM
  3. If 2 cells match then list values in a 3rd cell
    By showboat in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-16-2016, 07:45 PM
  4. Replies: 7
    Last Post: 05-07-2015, 07:29 AM
  5. Replies: 6
    Last Post: 09-13-2013, 01:29 AM
  6. [SOLVED] How can i match values from 4 cells then enter value from a 5th cell?
    By mikey42979 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-23-2013, 10:17 AM
  7. [SOLVED] Copy cell values if cells match on two diff sheets
    By dan in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-23-2006, 09:10 AM

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