vba help - Dictionary Key - avoid #N/A and Blank cells

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

How to avoid #N/A and Blanks cells in Dictionary Keys.

Column A and B cells are keys after joining both the cells.
Column C are dictionary Items Date Columns.

Genuine Dictionary keys and Items shown in Column E and F.

Below is a Sample data expected output is in Column E and F.

Book6
ABCDEF
1InvoiceReceiptDateGenuine Dictionary KeyGenuine Dictionary Items
21000R_130/04/20191000|R_130/04/2019
32000R_207/05/20192000|R_207/05/2019
4Not availableNot available11/05/20195000|30011/01/2020
5Not availableNot available15/06/20196000|R_411/01/2020
63000#N/A05/08/2019
7#N/A#N/A05/08/2019
8#N/A8000005/08/2019
9400027/09/2019
1014/12/2019
11500030011/01/2020
126000R_411/01/2020
Sheet1
Cell Formulas
RangeFormula
E2:E3E2=A2&"|"&B2
E4:E5E4=A11&"|"&B11




Below is my attempted Code.
VBA Code:
Option Explicit

Sub Dictionary_Key_issue()

    Dim arr As Variant
    Dim dict As New Scripting.Dictionary
    
    arr = Range("A2:C12").Value
    
    Dim i As Long
    Dim skey As String
    
    'Store into Dictionary
    
    With dict
    For i = 2 To 12
        skey = arr(i, 1) & "!" & arr(i, 2)  'avoid #N/A , Blanks here
        
        If Not .Exists(skey) Then
            .Add skey, arr(i, 3)
                       
        End If
    Next i
End With


End Sub


Thanks
mg
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Does this help?

VBA Code:
Sub Dictionary_Key()
Dim Rg As Range
Dim Cell As Range

Set Rg = Range("A2:A12")

For Each Cell In Rg
 If Evaluate("ISBLANK(" & Cell.Address & ")") = False And Application.WorksheetFunction.IsNA(Cell) = False Then
        Range("C" & Cell.Row).Value = Range("A" & Cell.Row).Value & "!" & Range("B" & Cell.Row).Value
End If
Next

End Sub
 
Upvote 0
To ignore blank cells and cells that contain either "Not available" or #N/A, you can use the following code...

VBA Code:
            If Not IsError(arr(i, 1)) And Not IsError(arr(i, 2)) Then
                If Len(arr(i, 1)) > 0 And Len(arr(i, 2)) > 0 Then
                    If LCase(arr(i, 1)) <> "not available" And LCase(arr(i, 2)) <> "not available" Then
                        skey = arr(i, 1) & "!" & arr(i, 2)  'avoid #N/A , Blanks here
                        
                        If Not .Exists(skey) Then
                            .Add skey, arr(i, 3)
                        End If
                    End If
                End If
            End If

By the way, you'll get a subscript of range error in your For/Next loop since the lower and upper bounds are actually 1 and 11, respectively. To avoid such errors, use LBound and Ubound to automatically calculate these bounds...

Code:
For i = LBound(arr) To UBound(arr)

And, of course, to transfer the result to your worksheet...

VBA Code:
    With dict
        Range("E2").Resize(.Count).Value = Application.Transpose(.Keys)
        Range("F2").Resize(.Count).Value = Application.Transpose(.Items)
    End With

Here's the complete code...

VBA Code:
Option Explicit

Sub Dictionary_Key_issue()

    Dim arr As Variant
    Dim dict As New Scripting.Dictionary
    
    arr = Range("A2:C12").Value
    
    Dim i As Long
    Dim skey As String
    
    'Store into Dictionary
    
    With dict
        For i = LBound(arr) To UBound(arr)
            If Not IsError(arr(i, 1)) And Not IsError(arr(i, 2)) Then
                If Len(arr(i, 1)) > 0 And Len(arr(i, 2)) > 0 Then
                    If LCase(arr(i, 1)) <> "not available" And LCase(arr(i, 2)) <> "not available" Then
                        skey = arr(i, 1) & "!" & arr(i, 2)  'avoid #N/A , Blanks here
                        
                        If Not .Exists(skey) Then
                            .Add skey, arr(i, 3)
                        End If
                    End If
                End If
            End If
        Next i
    End With
    
    With dict
        Range("E2").Resize(.Count).Value = Application.Transpose(.Keys)
        Range("F2").Resize(.Count).Value = Application.Transpose(.Items)
    End With


End Sub

Hope this helps!
 
Upvote 0
Hi Domenic,

Superb !! This is really very nice piece of code. I was struggling a lot in this.

Millions of thanks for your help. Thanks ? (y)


one more query ,

if I want avoid #N/A , via cells/range method.

If Not IsError(arr(i, 1)) And Not IsError(arr(i, 2)) Then .... what will be code here


Thanks
mg
 
Last edited:
Upvote 0
If you were to reference the cells directly...

VBA Code:
    If Not IsError(Cells(2, "a").Value) And Not IsError(Cells(2, "b").Value) Then

Is this what you mean?
 
Upvote 0
Hi Domenic,

Right! this is what I was expecting, thanks once again for your help ! (y)


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

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