Solved

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

Posted on 2009-04-02
4
695 Views
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.
CustomerLists.xls
0
Comment
Question by:ksarafi1
  • 3
4 Comments
 
LVL 45

Accepted Solution

by:
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
    Else
      colBothYears.Add rngThing.Text
    End If
  Next
  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
  Next
  
  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
  Next
  Set rngThing = Range("E3")
  lngRowOffset = 0
  For Each varItem In colThisYearOnly
    lngRowOffset = lngRowOffset + 1
    rngThing.Offset(lngRowOffset, 0).Value = varItem
  Next
  Set rngThing = Range("F3")
  lngRowOffset = 0
  For Each varItem In colBothYears
    lngRowOffset = lngRowOffset + 1
    rngThing.Offset(lngRowOffset, 0).Value = varItem
  Next
  
  Application.ScreenUpdating = False
End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 24098620
ksarafi1,

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.
0
 

Author Closing Comment

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

Expert Comment

by:aikimark
ID: 24099721
@ksarafi1,

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
line:

Range("D4:F65536").Clear 

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now