+ Reply to Thread
Results 1 to 12 of 12

How to understand and use CreateObject("Scripting.Dictionary")

Hybrid View

Darthzo How to understand and use... 04-02-2013, 08:52 AM
stanleydgromjr Re: How to understand and use... 04-02-2013, 09:08 AM
stanleydgromjr Re: How to understand and use... 04-02-2013, 09:13 AM
Darthzo Re: How to understand and use... 04-02-2013, 10:22 AM
stanleydgromjr Re: How to understand and use... 04-02-2013, 10:53 AM
Darthzo Re: How to understand and use... 04-02-2013, 12:04 PM
stanleydgromjr Re: How to understand and use... 04-02-2013, 02:36 PM
stanleydgromjr Re: How to understand and use... 04-03-2013, 07:13 AM
Darthzo Re: How to understand and use... 04-03-2013, 01:21 PM
Darthzo Re: How to understand and use... 04-03-2013, 12:08 PM
stanleydgromjr Re: How to understand and use... 04-03-2013, 06:55 PM
stanleydgromjr Re: How to understand and use... 04-03-2013, 07:21 PM
  1. #1
    Registered User
    Join Date
    09-20-2012
    Location
    New york
    MS-Off Ver
    Excel 2003
    Posts
    45

    How to understand and use CreateObject("Scripting.Dictionary")

    I want to search an multi-dimensional array which would be several columns J,K,N and extract unique items I was hoping to accomplish this by learning how to understand the above title. If anyone could point me in the direction of a good source or guide I would greatly appreciate it!

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Darthzo,

    Please post your workbook/worksheet.

    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed/changed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.

    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

    To attach your workbook, scroll down and click on the Go Advanced button, then scroll down and click on the Manage Attachments button.

  4. #4
    Registered User
    Join Date
    09-20-2012
    Location
    New york
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    VBA Scripting.xlsThank you stanleydgromjr I created a sample sheet as this would be quicker than revising the original one. I would like to understand how to take the data on both list insert them into an array and extract only the unique item from that list.

  5. #5
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Darthzo,

    Thanks for the workbook.

    Can we have another workbook, with worksheet Unique List of items manually created by you for the results that you are looking for.

  6. #6
    Registered User
    Join Date
    09-20-2012
    Location
    New york
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Revised workbook thanks again for the assistance.VBA Scripting v2.xls

  7. #7
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Darthzo,

    Thanks.

    With your raw data in worksheet A List, the results will be put into worksheet Unique List of items.

    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    
    Option Explicit
    Sub GetUniqueList()
    ' stanleydgromjr, 04/02/2013
    ' http://www.excelforum.com/excel-programming-vba-macros/911706-how-to-understand-and-use-createobject-scripting-dictionary.html
    Dim rng As Range, c As Range, v
    With Sheets("A List")
      Set rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
      With CreateObject("Scripting.Dictionary")
        .CompareMode = vbTextCompare
        For Each c In rng
          If c <> "" Then
            If Not .Exists(c.Value) Then
              .Add c.Value, c.Value
            End If
          End If
        Next
        v = Application.Transpose(Array(.Keys))
      End With
    End With
    With Sheets("Unique List of items")
      .UsedRange.ClearContents
      With .Range("A1")
        .Value = "Trust List"
        .Font.Bold = True
        .Font.Underline = xlUnderlineStyleSingle
      End With
      .Range("A2").Resize(UBound(v)) = v
      .Range("A2:A" & UBound(v)).Sort key1:=.Range("A2"), order1:=1
      .Columns.AutoFit
      .Activate
    End With
    End Sub
    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the GetUniqueList macro.

  8. #8
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Darthzo,

    If you want to get the unique data from both the A List and the B List then:

    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    
    Option Explicit
    Sub GetUniqueListV2()
    ' stanleydgromjr, 04/03/2013
    ' http://www.excelforum.com/excel-programming-vba-macros/911706-how-to-understand-and-use-createobject-scripting-dictionary.html
    Dim rng As Range, rng2 As Range, c As Range, v
    With Sheets("A List")
      Set rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    End With
    With Sheets("B List")
      Set rng2 = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    End With
    With CreateObject("Scripting.Dictionary")
      .CompareMode = vbTextCompare
      For Each c In rng
        If c <> "" Then
          If Not .Exists(c.Value) Then
            .Add c.Value, c.Value
          End If
        End If
      Next
      For Each c In rng2
        If c <> "" Then
          If Not .Exists(c.Value) Then
            .Add c.Value, c.Value
          End If
        End If
      Next
      v = Application.Transpose(Array(.Keys))
    End With
    With Sheets("Unique List of items")
      .UsedRange.ClearContents
      With .Range("A1")
        .Value = "Trust List"
        .Font.Bold = True
        .Font.Underline = xlUnderlineStyleSingle
      End With
      .Range("A2").Resize(UBound(v)) = v
      .Range("A2:A" & UBound(v) + 1).Sort key1:=.Range("A2"), order1:=1
      .Columns.AutoFit
      .Activate
    End With
    End Sub
    Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

    Then run the GetUniqueListV2 macro.

  9. #9
    Registered User
    Join Date
    09-20-2012
    Location
    New york
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Stanleydgromjr it works nicely could you do one last thing and give me a breakdown of how it works I was stepping through the code and catch everything. Thanks again greatly appreciated.

  10. #10
    Registered User
    Join Date
    09-20-2012
    Location
    New york
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Thank you Stanleydgromjr I am gonna test it out now

  11. #11
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Darthzo,

    Thanks for the feedback.

    You are very welcome. Glad I could help.

    could you do one last thing and give me a breakdown of how it works
    Be back in a little while.

  12. #12
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: How to understand and use CreateObject("Scripting.Dictionary")

    Darthzo,

    See the BOLD instructions (I hope they help).

    
    Option Explicit
    Sub GetUniqueListV2()
    ' stanleydgromjr, 04/03/2013
    ' http://www.excelforum.com/excel-programming-vba-macros/911706-how-to-understand-and-use-createobject-scripting-dictionary.html
    Dim rng As Range, rng2 As Range, c As Range, v
    
    With Sheets("A List")
    
      
      'set the variable rng
      '  to sheet "A List", range A2:A20
      Set rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    
    
    End With
    
    With Sheets("B List")
    
      
      'set the variable rng2
      '  to sheet "B List", range A2:A14
      Set rng2 = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    
    
    End With
    
    With CreateObject("Scripting.Dictionary")
    
      
      'tell the Scripting.Dictionary to compare text
      .CompareMode = vbTextCompare
    
      
      'c is a range variable
      '  for each cell in the range A2:A20
      For Each c In rng
    
      
        'if c is not equal to a space
        If c <> "" Then
    
          
          'then check, if the value of c is not already a key in the dictionary object
          If Not .Exists(c.Value) Then
    
          
            'if it is Not in the dictionary object, then add value of c to a key
            .Add c.Value, c.Value
    
    
          End If
        End If
      Next
    
      
      'the same instructions above relate to rng2 cells
    
      For Each c In rng2
        If c <> "" Then
          If Not .Exists(c.Value) Then
            .Add c.Value, c.Value
          End If
        End If
      Next
    
      
      'Transpose(Array(.Keys)) to variable array v
      v = Application.Transpose(Array(.Keys))
    
    End With
    
    With Sheets("Unique List of items")
    
      
      'clear the used range in Sheets("Unique List of items")
      .UsedRange.ClearContents
    
      
      With .Range("A1")
    
      
        'write the title to cell A1
        .Value = "Trust List"
    
        
        'format cell A1, Bold, and underline the characters
        .Font.Bold = True
        .Font.Underline = xlUnderlineStyleSingle
    
      End With
    
      
      'write the contents of v to A2:A25
      .Range("A2").Resize(UBound(v)) = v
    
      
      'sort A2:A25 ascending
      .Range("A2:A" & UBound(v) + 1).Sort key1:=.Range("A2"), order1:=1
    
      
      'autofit the columns width
      .Columns.AutoFit
    
      
      'activate/select Sheets("Unique List of items")
      .Activate
    
    
    End With
    End Sub
    Last edited by stanleydgromjr; 04-03-2013 at 07:38 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1