Trying to find missing pairs with macro or a manual process

sncb

Board Regular
Joined
Mar 17, 2011
Messages
168
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have a situation where I have a list of 17 cities and every item code needs to exist in exactly those 17 cities. The issue I have is my items do not exist in all the cities but in only some of them and I need a macro to identify only those cities (with the item code beside them) where the correction would need to be done. I dont know if a macro would be the best solution or another way to extract these but since I have to do these checks very regularly, I think a macro would be the way to go but any suggestions are welcome.

The number of records (rows) change each time based on the data Im trying to correct. Thanks in advance if anyone could advise or help with a macro code.

No headers
Col A are all the cities
Cols C&D is what my existing data looks like
Cols F&G is what I am trying to extract.

1682070514847.png
 

Attachments

  • 1682070191033.png
    1682070191033.png
    11.1 KB · Views: 8
  • 1682070252592.png
    1682070252592.png
    73.5 KB · Views: 8

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
So basically, you want the cities those doesn't exist in the first list for each item. Right?
 
Upvote 0
It is poosible both with formula and macro. Can you send the sample workbook please? I can't rewrite all the values.
 
Upvote 0
Can you send the sample workbook please? I can't rewrite all the values.
Note that if they use the "XL2BB" tool to post their data, you can easily copy and paste it from here to your workbook.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
It is poosible both with formula and macro. Can you send the sample workbook please? I can't rewrite all the values.
Thanks Flashbond. Here it is. Let me know if its usable.

FloridaFloridaItem1MichiganItem1
IllinoisIllinoisItem1NevadaItem1
IndianaIndianaItem1New YorkItem1
KentuckyKentuckyItem1North CarolinaItem1
MarylandMarylandItem1OhioItem1
MassachusettsMassachusettsItem1OklahomaItem1
MichiganTennesseeItem2OregonItem1
NevadaTexasItem2PennsylvaniaItem1
New YorkWashingtonItem2TennesseeItem1
North CarolinaKentuckyItem3TexasItem1
OhioMarylandItem3WashingtonItem1
OklahomaMassachusettsItem3FloridaItem2
OregonMichiganItem3IllinoisItem2
PennsylvaniaNevadaItem3IndianaItem2
TennesseeNew YorkItem3KentuckyItem2
TexasNorth CarolinaItem3MarylandItem2
WashingtonOhioItem3MassachusettsItem2
OklahomaItem3MichiganItem2
OregonItem3NevadaItem2
PennsylvaniaItem3New YorkItem2
TennesseeItem3North CarolinaItem2
TexasItem3OhioItem2
WashingtonItem3OklahomaItem2
IllinoisItem 4OregonItem2
FloridaItem 5PennsylvaniaItem2
IllinoisItem 5FloridaItem3
IndianaItem 5IllinoisItem3
KentuckyItem 5IndianaItem3
MarylandItem 5and so on....
MassachusettsItem 5and so on....
MichiganItem 5and so on....
and so on....
and so on....
 
Upvote 0
Note that if they use the "XL2BB" tool to post their data, you can easily copy and paste it from here to your workbook.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
Just saw this Joe...Will try this as well.
 
Upvote 0
Greetings to all. You can try with:

VBA Code:
Sub Macro8()
Dim a, b, i&, j&, d, R&
a = [a1].CurrentRegion
Range("D1").Insert Shift:=xlDown: Range("D1") = "F1"
Range("D1", [d1].End(xlDown)).AdvancedFilter 2, , [f1], True
b = [f1].CurrentRegion: [f1].CurrentRegion.Delete xlShiftUp
ReDim d(1 To UBound(a) * (UBound(b) - 1), 1 To 2)
For i = 1 To UBound(a)
  For j = 2 To UBound(b)
    R = 1 + R: d(R, 1) = a(i, 1): d(R, 2) = b(j, 1)
  Next
Next
[c1].CurrentRegion.Delete xlShiftUp: [c1:d1].Resize(UBound(d)) = d
End Sub
 
Upvote 0
I am sorry that I said it is achievable with formula. But that was a dead end for me. However I still believe it is possible and a master can do it. Here is a working macro:
VBA Code:
Sub test()
  Dim states As Variant, myRange As Variant, items As Variant, c As Long, exist As Boolean
 
  With ActiveSheet
  states = Intersect(.Range("A:A").CurrentRegion, .Range("A:A"))
  myRange = Intersect(.Range("C:D").CurrentRegion, .Range("C:D"))
  items = Intersect(.Range("D:D").CurrentRegion, .Range("D:D"))
 
  For i = LBound(items) To UBound(items) - 1
    For j = i + 1 To UBound(items)
      If items(i, 1) = items(j, 1) Then items(j, 1) = ""
    Next
  Next
  c = 1
  For Each itm In items
    If itm <> "" Then
      For Each ste In states
        For i = 1 To UBound(myRange)
          If ste = myRange(i, 1) And myRange(i, 2) = itm Then
            exist = True
          End If
        Next
        If Not exist Then
          .Cells(c, 6).Value = ste
          .Cells(c, 7).Value = itm
          c = c + 1
        End If
        exist = False
      Next
    End If
  Next
  End With
End Sub
 
Last edited by a moderator:
Upvote 1
Solution
VBA Code:
Sub test()
  Dim states As Variant, myRange As Variant, items As Variant, c As Long, exist As Boolean
 
  With ActiveSheet
  states = Intersect(.Range("A:A").CurrentRegion, .Range("A:A"))
  myRange = Intersect(.Range("C:D").CurrentRegion, .Range("C:D"))
  items = Intersect(.Range("D:D").CurrentRegion, .Range("D:D"))
 
  For i = LBound(items) To UBound(items) - 1
    For j = i + 1 To UBound(items)
      If items(i, 1) = items(j, 1) Then items(j, 1) = ""
    Next
  Next
  c = 1
  For Each itm In items
    If itm <> "" Then
      For Each ste In states
        For i = 1 To UBound(myRange)
          If ste = myRange(i, 1) And myRange(i, 2) = itm Then
            exist = True
          End If
        Next
        If Not exist Then
          .Cells(c, 6).Value = ste
          .Cells(c, 7).Value = itm
          c = c + 1
        End If
        exist = False
      Next
    End If
  Next
  End With
End Sub
Yes this one worked perfectly well. The first one wasnt and I was just writing when I got your second code...Thanks a ton Flashbond.
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,153
Members
452,891
Latest member
JUSTOUTOFMYREACH

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