+ Reply to Thread
Results 1 to 4 of 4

Extracing a Single Instance of Each Name

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    839

    Extracing a Single Instance of Each Name

    Hi,

    In column A of Sheet2 I have a list of names and in columns B and C are the associated sales and country for each name. This is the “raw data”.

    For example, in A3 is the name “John” and B3 and C3 have “1,000” and “France”, respectively.

    I’m trying to create some VBA code that will summarise this data in Sheet1 (starting in A1), by putting a single instance of each country in column A and in column B the total sales for that country calculated from the data described above (much like a pivot table would calculate).

    To help me on my way could someone please suggest the code that will copy a single instance of each country in the raw data to the Sheet1?

    Thanks very much!

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Extracing a Single Instance of Each Name

    I'm sure there's a variety of ways; I went with an advanced filter of unique values of Column C and then just made a SUMIF of those values.

    Sub andrewc()
    
        'dimension variables
        Dim lngLastSource As Long, lngLastSummary
        Dim wsData As Worksheet: Set wsData = Sheets("data")
        Dim wsSummary As Worksheet: Set wsSummary = Sheets("Summary")
        
        'clear summary
        wsSummary.Range("A1").CurrentRegion.ClearContents
        
        'identify max row of source
        lngLastSource = wsData.Range("A" & Rows.Count).End(xlUp).Row
        
        'filter unique countries
        wsData.Range("C1:C" & lngLastSource).AdvancedFilter Action:=xlFilterCopy, _
            CopyToRange:=wsSummary.Range("A1"), Unique:=True
            
        'create total column header
        wsSummary.Range("B1") = "Total"
        
        'identify max row of uniques
        lngLastSummary = wsSummary.Range("A" & Rows.Count).End(xlUp).Row
        
        'loop through all uniques
        For i = 2 To lngLastSummary
        
          'sumif for each
          wsSummary.Cells(i, 2) = WorksheetFunction.SumIf(wsData.Range("C2:C" & lngLastSource), _
                                                          wsSummary.Cells(i, 1), _
                                                          wsData.Range("B2:B" & lngLastSource))
        Next
            
    End Sub


    A loop could to simply run a continuous sum would probably work as well.
    Attached Files Attached Files
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    839

    Re: Extracing a Single Instance of Each Name

    That's great, thanks very much!

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Extracing a Single Instance of Each Name

    As alternative, I have used the data from daffodil11, but the code will not work in Mac.
    Attached Files Attached Files

+ 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] Force all workbooks into a single instance of Excel always?
    By ks100 in forum Excel General
    Replies: 3
    Last Post: 01-08-2014, 03:30 PM
  2. Multiple Instances to Single Instance
    By jcholla in forum Excel General
    Replies: 2
    Last Post: 11-16-2011, 04:22 PM
  3. keep n duplicates not just a single instance - how
    By TopDogDave in forum Excel General
    Replies: 1
    Last Post: 11-13-2010, 10:55 PM
  4. Changing the condition of a For Each loop for a single instance
    By akh in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-23-2010, 11:23 AM
  5. Concatenate single instance of duplicates
    By Zaeguzah in forum Excel General
    Replies: 4
    Last Post: 12-01-2009, 07:04 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