+ Reply to Thread
Results 1 to 3 of 3

Write Dictionary items to Worksheet

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2013
    Location
    India
    MS-Off Ver
    Office 2010, 2019
    Posts
    79

    Write Dictionary items to Worksheet

    I want to copy an array from one workbook (wbML) to another (wbDB) using the dictionary method.
    I want three columns of data from wbML: Ref, Name, Surname. It has many more columns of data and I can't always be certain where the data I want will be located.

    I'm not certain that I'm adding the range correctly to the dictionary item.
    Second, I would like to write the data to wbDB.

    Any help would be appreciated.

    Sub copy_range()
        Dim headers As Variant, header As Variant, headerCol As Integer, wbDB As Workbook, wbML As Workbook, _
        i As Integer, _
        Dict1 As Scripting.Dictionary
        
        Set Dict1 = New Scripting.Dictionary
        Set wbDB = ThisWorkbook                                 'user initiates macro from wbDB
        Set wbML = Workbooks("ML.xlsx")
        headers = Array("ref", "name", "surname")               'wbML columns to copy
        lr = Cells(Rows.Count, "A").End(xlUp).Row               'get last row
        i = 1
        wbML.Activate
        For Each header In headers
            headerCol = Rows("1").find(header).Column           'find header column
            With Dict1
                .item(i) = Range(Cells(1, headerCol), Cells(lr, headerCol))     'Does this add the range to key.item?
            End With
            i = i + 1
        Next header
        
        'How do I write the dictionary items to wbDB?
        
        
        
    End Sub

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,641

    Re: Write Dictionary items to Worksheet

    One way
    1)
            With Dict1
                .Item(i) = Application.Transpose(Range(Cells(1, headerCol), Cells(lr, headerCol)).Value)     'Does this add the range to key.item?
            End With
    Key is "i"

    2)
        wbDB.Sheets(1).[a1].Resize(lr , Dict1.Count).Value = Application.Transpose(Dict1.items)
    Last edited by jindon; 06-25-2017 at 02:41 AM.

  3. #3
    Registered User
    Join Date
    05-26-2013
    Location
    India
    MS-Off Ver
    Office 2010, 2019
    Posts
    79

    Re: Write Dictionary items to Worksheet

    Thank you, again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Retrive multiple items from dictionary
    By jaryszek in forum Excel General
    Replies: 34
    Last Post: 02-24-2017, 07:52 AM
  2. Add items to dictionary - VBA Help Needed
    By mitko007 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-26-2015, 07:26 AM
  3. [SOLVED] Macro to write dictionary keys to array not working
    By capson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-07-2015, 05:21 PM
  4. [SOLVED] Looping through dictionary items where items are an array
    By strud in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2014, 06:56 AM
  5. Moving dictionary items to an 2D array that can be placed into worksheet quickly
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-22-2014, 01:21 PM
  6. Write Items from listbox to worksheet
    By efernandes67 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-01-2011, 06:15 AM
  7. [SOLVED] looping through items in a dictionary
    By Abe in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-08-2006, 05:45 AM

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