Can anybody please explain what a line of code in my sub is actually doing

Bagsy

Active Member
Joined
Feb 26, 2005
Messages
467
Office Version
  1. 365
Platform
  1. Windows
I have some code I cobbled together which is working just great and I am using the dictionary with some code I believe Fluff wrote.
Can anybody please tell me what the offset is for in this line

VBA Code:
Dic(Cl.Value) = Array(Cl, Cl.Offset(, 1).Value)
If I remove it and put
VBA Code:
Dic(Cl.Value) = Array(Cl, Cl.Value)
The code does not work

Full working code below
VBA Code:
Sub CheckIfDispatched()
    Dim WB1 As Workbook
    Dim WB2 As Workbook
    Application.ScreenUpdating = False
    
'   Capture current workbook
    Set WB1 = ActiveWorkbook

    '***************************************
    Dim a As Integer   'Clearing old data from sheet 2 of the cert pack tracker workbook
    a = Sheets("Sheet2").Range("E2:E" & Rows.Count).End(xlUp).Row
    Sheets("Sheet2").Range("E2" & a).ClearContents
    '***************************************
'   Opening the Open SO Items register
    Workbooks.Open Filename:="L:\EMAX\EMAX REPORTS\Open SO Items.xlsx", ReadOnly:=True

'   Capture new workbook
    Set WB2 = ActiveWorkbook
            Sheets("Open SO Items").Select
        If Sheets("Open SO Items").FilterMode Then ActiveSheet.ShowAllData 'Clear All Filters for entire Table
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    
  '***************************************
    'Copying the SO numbers
    a = Sheets("Open SO Items").Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Open SO Items").Range("A2:A" & a).Copy
    
   '***************************************
    '   Go back to original workbook
    WB1.Activate
    'Pasting the SO numbers
    Sheets("Sheet2").Range("E2").PasteSpecial xlPasteValues
    Application.CutCopyMode = False
    
    '**************************************
    'comparing SO numbers, to see if the SO number is still on the open SO items register, otherwise it may have been cancelled or dispatched
    Dim Cl As Range
    Dim Dic As Object

    Set Dic = CreateObject("scripting.dictionary")
    With Sheets("Sheet2") 'adding all the SO numbers from the pasted values on sheet 2 from the open so items register to the Dic (dictionary)
        For Each Cl In .Range("E2", .Range("E" & Rows.Count).End(xlUp))
            Dic(Cl.Value) = Array(Cl, Cl.Offset(, 1).Value)
        Next Cl
    End With
    With Sheets("Sheet1")
        For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp)) 'comparing the SO numbers from sheet1 to the stored numbers in the Dic (Dictionary)
            If Not Dic.Exists(Cl.Value) Then   ' if it does not appear in the Dic (dictionary) then highlight green
               Cl.Offset(0, 1).Interior.Color = vbGreen
            End If
        Next Cl
    End With
    
 WB2.Close False ' closing the Open SO items register
  Application.ScreenUpdating = True

  End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
As you are not using the items in the dictionary, just the key, that isn't really doing anything so you can use
VBA Code:
Dic(Cl.Value) = Empty
 
Upvote 0
Solution
As you are not using the items in the dictionary, just the key, that isn't really doing anything so you can use
VBA Code:
Dic(Cl.Value) = Empty
Thanks Fluff
Is the Key for the dictionary the values in column E and that line of code is offsetting to next column and adding the value that is in that cell to the dictionary, which I am not actually using.

In that case, if I have data in columns E, F & G can I use column E as the key, offset and add the data from the next two columns (F & G) to the dictionary (which are actually dates) then add to sheet 1 offsets as below.

I have tried this code but obviously it doesn't work, but its an example of what I would like to achieve hopefully

VBA Code:
Sub Test()
    Dim Cl As Range
    Dim Dic As Object

    Set Dic = CreateObject("scripting.dictionary")
    With Sheets("Sheet2")
        For Each Cl In .Range("E2", .Range("E" & Rows.Count).End(xlUp))
            Dic(Cl.Value) = Array(Cl, Cl.Offset(, 1).Value)
            Dic(Cl.Value) = Array(Cl, Cl.Offset(, 2).Value)
        Next Cl
    End With
    With Sheets("Sheet1")
        For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
            If Dic.Exists(Cl.Value) Then
               Dic(Cl.Value) = Array(Cl, Cl.Offset(, 8).Value)
               Dic(Cl.Value) = Array(Cl, Cl.Offset(, 9).Value)
               
            End If
        Next Cl
    End With
End Sub
 
Upvote 0
That is a completely different question, so needs a new thread.
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,172
Members
449,071
Latest member
cdnMech

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