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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

708 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

20 Experts available now in Live!

Get 1:1 Help Now