Output dictionary contents to sheet

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
Hi, I am trying to output the contents of a dictionary to a worksheet.

I need to populate my dictionary into columns A and B into the last unused rows in both columns.

The dictionary will have an even number of items, so for example item 1,3,5,7,9 would go into column A and 2,4,6,8,10 into column B.

Any suggestions please?

VBA Code:
For Each rng In Sht.Range("D6:BM" & Sht.Cells(Rows.Count, "B").End(xlUp).Row).Columns
        For Each c In rng.Cells
            If c.Value = 0 Then
                Cells(c.Row, c.Column).Interior.Color = 7303929

                x = x + 1
                dict.Add x, Cells(5, c.Column)
                x = x + 1
                dict.Add x, Cells(c.Row, 2)
            End If
        Next c
    Next rng
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
The only way I can see you doing that is using a loop.
VBA Code:
Dim rngDst As Range
Dim idx As Long

    Set rngDst = Range("A1").
  
    For idx = 1 To dict.Count Step 2
        rngDst = dict(idx)
        rngDst.Offset(,1) = dict(idx+1)
        Set rngDst = rngDst.Offset(1)
    Next idx
 
Upvote 0
How about
VBA Code:
   For Each rng In sht.Range("D6:BM" & sht.Cells(Rows.Count, "B").End(xlUp).Row).Columns
        For Each c In rng.Cells
            If c.Value = 0 Then
                Cells(c.Row, c.Column).Interior.Color = 7303929

                x = x + 1
                dict.Add x, Array(Cells(5, c.Column), Cells(c.Row, 2))
            End If
        Next c
    Next rng
    sht.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(dict.Count, 2).Value = Application.Index(dict.items, 0)
 
Upvote 0
Solution
Thanks, both of these suggestions work.

Fluff, I went with yours as it was a one line addition to my existing code. I do have a slight problem though where the dictionary output is converting my dates to American format. I have tried the below amendment but it doesn’t solve my issue.

dict.Add x, Array(Cells(5, c.Column), Int(CDate(Cells(c.Row, 2))))
 
Upvote 0
Try
VBA Code:
dict.Add x, Array(Cells(5, c.Column), CLng(Cells(c.Row, 2)))
 
Upvote 0
Still the same unfortunately.

I think it may be at the point where it outputs the data to the sheet when they are converted.
 
Upvote 0
Using Clng works for me, check that the column is formatted as general, before running the code.
 
Upvote 0
Thanks, by setting my column to general then back to date solved the issue.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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