VBA Scripting Dictionary - Basic Code Just Starting Out

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
426
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for any assistance provided by anyone.
I am new to Arrays and Scripting Dictionaries.
I would like to read the following values into a Scripting Dictionary and then run some code based on that data. If that data does not exist in my data set, I want to perform an operation.


Read the following into a dictionary
Stocks - Tool, Analysis - Get Data Macro Update - (Active).xlsm
A
1Region
2Region 1
3Region 2
4Region 4
5Region 6
6Region 7
Data


If any of those values exists in my data set, where my data set is as follows
Stocks - Tool, Analysis - Get Data Macro Update - (Active).xlsm
A
1Region
2Region 1
3Region 2
4Region 3
5Region 4
6Region 5
7Region 6
8Region 7
SA


I do not want to perform say an operation as such (sample code):

VBA Code:
Option Explicit
'***************************************************************************************************************
Sub MACRO_MXX_Script_Dic_Test()



  '_______________________________________________________________________________________________________________
  'Turn off alerts, screen updates, and automatic calculation
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Application.Calculation = xlManual
            
            
  '_______________________________________________________________________________________________________________
  'Dimensioning

    'Dim
     Dim i As Long, j As Long
     Dim aDictionary As Object
     

    
  '_________________________________________________________________________________________________________________
  'Code -

    Set aDictionary = CreateObject("Scripting.Dictionary")
    
    With Sheets("Data")
        For j = 2 To 6
            aDictionary(j, 1) = .Range(j, 1).Value
        Next j
    End With
    
    
    With Sheets("SA")
        For i = 2 To 8
            'code - If (values does not exist in the Dictionary) then MsgBox .Range(i, 1)
        Next j
    End With
    

  '_________________________________________________________________________________________________________________
  'Turn on alerts and screen updates, and calculate
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Calculate


 '_________________________________________________________________________________________________________________
 '
 
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You will save yourself a lot of time learning about scripting dictionaries by reading though the page below and trying some of the examples there.

 
Upvote 0
You will save yourself a lot of time learning about scripting dictionaries by reading though the page below and trying some of the examples there.

Thanks @rlv01. I did review that and other websites with similar info prior to starting this post. That's what I used for what I have thus far. Still looking for some additional guidance.
 
Upvote 0
How about
VBA Code:
    Set aDictionary = CreateObject("Scripting.Dictionary")
    
    With Sheets("Data")
        For j = 2 To 6
            aDictionary(.Cells(j, 1).Value) = Empty
        Next j
    End With
    
    
    With Sheets("SA")
        For i = 2 To 8
            If Not aDictionary.Exists(.Cells(i, 1).Value) Then MsgBox .Cells(i, 1)
        Next j
    End With
 
Upvote 0
Solution
How about
VBA Code:
    Set aDictionary = CreateObject("Scripting.Dictionary")
   
    With Sheets("Data")
        For j = 2 To 6
            aDictionary(.Cells(j, 1).Value) = Empty
        Next j
    End With
   
   
    With Sheets("SA")
        For i = 2 To 8
            If Not aDictionary.Exists(.Cells(i, 1).Value) Then MsgBox .Cells(i, 1)
        Next j
    End With
@Fluff Thanks! At a first quick check, it seems like it works. Once I get to look at it closer I will let you know and mark the solution. Just wanted to get back to you ASAP!
Thanks @Fluff for all the assistance you provide on this platform.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0
Glad to help & thanks for the feedback.
As I am learning and if you have time to answer the following, if not, no worries.

The following line of code just stores all the values that I do not want included. You set it to be empty since the compare mode is going to be used?
VBA Code:
aDictionary(.Cells(j, 1).Value) = Empty

So in the following line, the only thing that has to do with the dictionary is the:
VBA Code:
aDictionary

The
VBA Code:
.Cells(i, 1).Value
from
VBA Code:
 If Not aDictionary.Exists(.Cells(i, 1).Value) Then MsgBox .Cells(i, 1)
is the value in the Sheets("SA"). It's just saying if .Cells(i, 1).Value from Sheet("SA") is not in the dictionary, then perform the operation?

Now if I wanted to move the following line of code to a function,
VBA Code:
With Sheets("SA")
        For i = 2 To 8
            If Not aDictionary.Exists(.Cells(i, 1).Value) Then MsgBox .Cells(i, 1)
        Next j
End With

set it up as follows?

VBA Code:
Function TestDic(aDictionary As Dictionary) as Variant

Dim i as Long

        For i = 2 To 8
            If Not aDictionary.Exists(.Cells(i, 1).Value) Then MsgBox .Cells(i, 1)
        Next i
End Function
 
Upvote 0
The following line of code just stores all the values that I do not want included.
I have no idea what you mean by that.

That line adds all the data to the dictionary as the key & leaves the Item empty.
This aDictionary is not a line of code, so once again I have no idea what you mean.

I would suggest that you follow the link from post#2 & thoroughly read it.
 
Upvote 0

Forum statistics

Threads
1,216,209
Messages
6,129,517
Members
449,515
Latest member
lukaderanged

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