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
715 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 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…

728 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