Trying to find missing pairs with macro or a manual process

sncb

Board Regular
Joined
Mar 17, 2011
Messages
145
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: 7
  • 1682070252592.png
    1682070252592.png
    73.5 KB · Views: 7

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I know you already have a solution but I may as well post it anyway.

This is uses a dictionary so it might be a bit faster.

VBA Code:
Sub CompleteCityCombinations()

    Dim shtData As Worksheet
    Dim cityRng As Range, srcRng As Range, outRng As Range
    Dim cityArr As Variant, srcArr As Variant, outArr As Variant
    Dim srcDic As Object, ItemDic As Object
    Dim ItemKey As String, srcKey As String
    Dim iKey As Variant
    Dim i As Long, iOut As Long
    
    Set shtData = ActiveSheet
    With shtData
        Set cityRng = .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
        Set srcRng = .Range("C1:D" & .Cells(Rows.Count, "C").End(xlUp).Row)
        Set outRng = .Range("F1")
        cityArr = cityRng
        srcArr = srcRng
    End With
    
    
    Set ItemDic = CreateObject("Scripting.dictionary")
    Set srcDic = CreateObject("Scripting.dictionary")
    
    ' Load details range into Dictionary & get unique items list
    For i = 1 To UBound(srcArr)
        srcKey = srcArr(i, 1) & "|" & srcArr(i, 2)
        ItemKey = srcArr(i, 2)
        If Not srcDic.exists(srcKey) Then
            srcDic(srcKey) = i
        End If
        
        If Not ItemDic.exists(ItemKey) Then
            ItemDic(ItemKey) = i
        End If
    Next i
    
    ' Loop through unique items and check for every city-item combination
    ' if it doesn't exist put it in the output array
    ReDim outArr(1 To UBound(cityArr) * ItemDic.Count, 1 To 2)
    For Each iKey In ItemDic.Keys
        For i = 1 To UBound(cityArr)
            srcKey = cityArr(i, 1) & "|" & iKey
            If Not srcDic.exists(srcKey) Then
                iOut = iOut + 1
                outArr(iOut, 1) = cityArr(i, 1)
                outArr(iOut, 2) = iKey
            End If
        Next i
    
    Next iKey
    
    outRng.Resize(iOut, UBound(outArr, 2)) = outArr

End Sub
 
Upvote 1
I know you already have a solution but I may as well post it anyway.

This is uses a dictionary so it might be a bit faster.

VBA Code:
Sub CompleteCityCombinations()

    Dim shtData As Worksheet
    Dim cityRng As Range, srcRng As Range, outRng As Range
    Dim cityArr As Variant, srcArr As Variant, outArr As Variant
    Dim srcDic As Object, ItemDic As Object
    Dim ItemKey As String, srcKey As String
    Dim iKey As Variant
    Dim i As Long, iOut As Long
   
    Set shtData = ActiveSheet
    With shtData
        Set cityRng = .Range("A1:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
        Set srcRng = .Range("C1:D" & .Cells(Rows.Count, "C").End(xlUp).Row)
        Set outRng = .Range("F1")
        cityArr = cityRng
        srcArr = srcRng
    End With
   
   
    Set ItemDic = CreateObject("Scripting.dictionary")
    Set srcDic = CreateObject("Scripting.dictionary")
   
    ' Load details range into Dictionary & get unique items list
    For i = 1 To UBound(srcArr)
        srcKey = srcArr(i, 1) & "|" & srcArr(i, 2)
        ItemKey = srcArr(i, 2)
        If Not srcDic.exists(srcKey) Then
            srcDic(srcKey) = i
        End If
       
        If Not ItemDic.exists(ItemKey) Then
            ItemDic(ItemKey) = i
        End If
    Next i
   
    ' Loop through unique items and check for every city-item combination
    ' if it doesn't exist put it in the output array
    ReDim outArr(1 To UBound(cityArr) * ItemDic.Count, 1 To 2)
    For Each iKey In ItemDic.Keys
        For i = 1 To UBound(cityArr)
            srcKey = cityArr(i, 1) & "|" & iKey
            If Not srcDic.exists(srcKey) Then
                iOut = iOut + 1
                outArr(iOut, 1) = cityArr(i, 1)
                outArr(iOut, 2) = iKey
            End If
        Next i
   
    Next iKey
   
    outRng.Resize(iOut, UBound(outArr, 2)) = outArr

End Sub
Thanks to you as well. This worked like a charm too.
 
Upvote 0
... 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.

@sncb:
I ask you -only- out of curiosity: Did you observe what was suggested in post #8?...
 
Upvote 0
@sncb:
I ask you -only- out of curiosity: Did you observe what was suggested in post #8?...
You seem to have read the requirements differently.
• Our understanding is that we needed to keep the original list intact and produce a list of the missing city-item combination. Your code is creating all combinations and overwriting the original list.
• Your code is placing a temporary value of "F1" in cell D1, this ignores the statement that there are No headers.
If the first item only had one city in it, the code would not have picked up that item when it created a unique list of items.

Always happy to have another contributor on board. 👍
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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