Is it possible to create a macro which generates a new list based on a criteria list?

NewbieNoob

New Member
Joined
Feb 23, 2018
Messages
15
5GlXgm2
5GlXgm2
https://imgur.com/5GlXgm2

Assuming I have my list of cars in column A. I choose a certain type of cars in column C, would it then be possible to generate a list of the remaining cars in column E, which I did not choose?

I found something like this: http://www.excelfunctions.net/vba-filter-function.html I think Example 2 is what I exactly want, however I'm not sure how I assign this macro to a button to make it generate the list?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi & welcome to the board.
How about
Code:
Sub ListOthers()

   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      .comparemode = vbTextCompare
      For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Nothing
      Next Cl
      For Each Cl In Range("C1", Range("C" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then .Remove (Cl.Value)
      Next Cl
      Range("E1").Resize(.Count).Value = Application.Transpose(.keys)
   End With
End Sub
 
Upvote 0
Hi & welcome to the board.
How about
Code:
Sub ListOthers()

   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      .comparemode = vbTextCompare
      For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Nothing
      Next Cl
      For Each Cl In Range("C1", Range("C" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then .Remove (Cl.Value)
      Next Cl
      Range("E1").Resize(.Count).Value = Application.Transpose(.keys)
   End With
End Sub

Thank you! But how do I activate the macro to make it generate the list? Should I assign it to some kind of button where when I click it, it generates the list in column E?
 
Upvote 0
Hi & welcome to the board.
How about
Code:
Sub ListOthers()

   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      .comparemode = vbTextCompare
      For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Nothing
      Next Cl
      For Each Cl In Range("C1", Range("C" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then .Remove (Cl.Value)
      Next Cl
      Range("E1").Resize(.Count).Value = Application.Transpose(.keys)
   End With
End Sub

Wait, I think I got it! I created an ActiveX button and assigned it the following macro and it seems to generate the list now when I click the button!
Code:
Private Sub CommandButton1_Click()
Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      .comparemode = vbTextCompare
      For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Nothing
      Next Cl
      For Each Cl In Range("C1", Range("C" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then .Remove (Cl.Value)
      Next Cl
      Range("E1").Resize(.Count).Value = Application.Transpose(.keys)
   End With
End Sub

Thank you very much for the help. I will now study the code more thoroughly to understand each part. :)
 
Upvote 0
If your data has headings, as most data does, then another option either manually or macro might be to use Advanced Filter. For example, assuming columns D:E are initially empty.
Code:
Sub ExcludedList()
  Application.ScreenUpdating = False
  Range("D2").Formula = "=COUNTIF(C$2:C$" & Range("C" & Rows.Count).End(xlUp).Row & ",A2)=0"
  Range("A1", Range("A" & Rows.Count).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range("D1:D2"), CopyToRange:=Range("E1"), Unique:=True
  Range("D2").ClearContents
  Application.ScreenUpdating = True
End Sub


Book1
ABCDE
1ListExcludeList
2AudiAudiMercedes
3AudiFordFiat
4Mercedes
5Fiat
6Audi
7Mercedes
8Ford
9
New List
 
Upvote 0
Glad to help & thanks for the feedback

For help with dictionaries have a look here
https://excelmacromastery.com/vba-dictionary/

Thanks! I have a small question regarding the code that I'm not quite sure about. It's regarding this line:

Code:
[COLOR=#333333]         If Not .exists(Cl.Value) Then .Add Cl.Value, Nothing[/COLOR][CODE][COLOR=#333333]

[/COLOR]I'm trying to figure out why the Nothing argument is needed at the end? In the line with .Remove, we don't seem to have a similar syntax. I have also noticed that I can literally write anything in place of Nothing in that code and still get the same result.
 
Upvote 0
Unlike a Collection, which only requires only an Item (a Key is optional), a Dictionary requires both an Item and a Key. If you don't need the Item, it can be anything (and Nothing is as good as anything), but you'll get a compile error if you don't include one.
 
Last edited:
Upvote 0
Unlike a Collection, which only requires only an Item (a Key is optional), a Dictionary requires both an Item and a Key. If you don't need the Item, it can be anything (and Nothing is as good as anything), but you'll get a compile error if you don't include one.

But why do I not get a compile error in the other code when I don't define an item?
Code:
[COLOR=#333333]         If .exists(Cl.Value) Then .Remove (Cl.Value)
[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top