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
697 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

775 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