How to compare two arrays and write the uniue elements of one on a third array on VBA

Posted on 2009-04-02
Last Modified: 2013-11-25
The attached workbook contains two lists of customers:last year purchasers company last and this year purchasers

I want to write a sub that captures the existing lists in two arrays and then creates three new arrays:

1) One with customers who purchased last year only
2) One with customers who purchased this year only    
3) One with customers who purchased in both years.

After these new arrays have been filled, need to write their contents in columns D, E and F of the worksheet.
Question by:ksarafi1
  • 3
LVL 45

Accepted Solution

aikimark earned 500 total points
ID: 24074086
For some reason, the command button I found on your worksheet didn't behave like a regular control.  I added another command button and added the following code to its click event:
Private Sub CommandButton1_Click()
  Dim colLastYearOnly As New Collection
  Dim colThisYearOnly As New Collection
  Dim colBothYears As New Collection
  Dim rngThing As Range
  Dim rngFound As Range
  Dim varItem As Variant
  Dim lngRowOffset As Long
  'This code assumes that the two ranges are named LastYear and ThisYear
  For Each rngThing In Range("LastYear").Cells
    Set rngFound = Range("ThisYear").Find(rngThing.Text)
    If rngFound Is Nothing Then
      colLastYearOnly.Add rngThing.Text
      colBothYears.Add rngThing.Text
    End If
  For Each rngThing In Range("ThisYear").Cells
    Set rngFound = Range("LastYear").Find(rngThing.Text)
    If rngFound Is Nothing Then
      colThisYearOnly.Add rngThing.Text
    End If
  Application.ScreenUpdating = False
  'Populate the three columns
  Set rngThing = Range("D3")
  For Each varItem In colLastYearOnly
    lngRowOffset = lngRowOffset + 1
    rngThing.Offset(lngRowOffset, 0).Value = varItem
  Set rngThing = Range("E3")
  lngRowOffset = 0
  For Each varItem In colThisYearOnly
    lngRowOffset = lngRowOffset + 1
    rngThing.Offset(lngRowOffset, 0).Value = varItem
  Set rngThing = Range("F3")
  lngRowOffset = 0
  For Each varItem In colBothYears
    lngRowOffset = lngRowOffset + 1
    rngThing.Offset(lngRowOffset, 0).Value = varItem
  Application.ScreenUpdating = False
End Sub

Open in new window

LVL 45

Expert Comment

ID: 24098620

How did this work for you?  

It has been three days since your last interaction with this discussion thread.  Please remain an active participant with your open questions.

Author Closing Comment

ID: 31566110
Thank you very much for your help. You solution was accurate and efficient.
LVL 45

Expert Comment

ID: 24099721

Glad I could help.

If you run this when either of the three lists shrinks, you might find some cells populated from the prior run.  To prevent this, add the following statement after the
    Application.ScreenUpdating = False


Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create a duplicate finder Application 9 122
Updates not working for MS Windows 7 12 181
Access query that references subform 5 47
MsgBox 4 68
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question