Data Dictionary

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
215
Office Version
  1. 2016
Platform
  1. Windows
Hello guys,

I am trying to convert the left table to the right table where the code is counting the numbers of yellow cells by Supplier and date. I managed to get close to that :) using the code below, but still not correct, because I am not sure how to add two item columns in the dictionary


1592889419884.png


VBA Code:
Option Explicit

Sub GetTotals()
    
    ' Get worksheet
    Dim wk As Worksheet
    Set wk = ThisWorkbook.Worksheets("Sheet1")
    
    ' Get ranges
    Dim rng As Range
    Set rng = wk.Range("A1").CurrentRegion
    
    Dim Supplier As String, Header As String
    Dim Period As Long
    Dim Counter As Long

    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    Dim r As Long
    Dim c As Long
    
         For c = 1 To rng.Columns.Count
            For r = 1 To rng.Rows.Count
                Counter = 0
                Supplier = rng.Cells(r, 1).Value
                Header = rng.Cells(1, c).Value
                If rng.Cells(r, c).Interior.Color = 65535 Then
                    Counter = Counter + 1
                    dict(Header) = dict(Header) + Counter 'Adding the yellow values into the dictionary
                    dict(Supplier) = dict(Supplier) + Counter
                End If
            Next r
        Next c


    Dim LRow As Long
    LRow = Cells(Rows.Count, 8).End(xlUp).row
    
'    ClearData shReport
     Sheets(1).Range("G1:I" & LRow).Clear
     
     
     ' Write the Header
    Sheets(1).Range("G1").Resize(dict.Count, 1).Value = WorksheetFunction.Transpose(dict.Keys)
    
    ' Write the keys
    Sheets(1).Range("H1").Resize(dict.Count, 1).Value = WorksheetFunction.Transpose(dict.Keys)
        
    ' Write the Items
    Sheets(1).Range("I1").Resize(dict.Count, 1).Value = WorksheetFunction.Transpose(dict.Items)
    
    ' Clean up
    Set dict = Nothing
    
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this

VBA Code:
Sub Data_Dictionary()
  Dim rng As Range, dic As Object, supd As String
  Dim c As Long, r As Long
 
  Set rng = Range("A1", Range("D" & Rows.Count).End(3))
  Set dic = CreateObject("Scripting.Dictionary")
 
  For r = 2 To rng.Rows.Count
    For c = 2 To rng.Columns.Count
      supd = rng.Cells(1, c).Value & "|" & rng.Cells(r, 1).Value
      dic(supd) = dic(supd) + 0
      If rng.Cells(r, c).Interior.Color = 65535 Then dic(supd) = dic(supd) + 1
    Next c
  Next r
 
  With Range("G2").Resize(dic.Count, 1)
    .Value = Application.Transpose(dic.keys)
    .TextToColumns Range("G2"), 1, Other:=True, OtherChar:="|", FieldInfo:=Array(Array(1, 4), Array(2, 1)), TrailingMinusNumbers:=True
  End With
  Range("I2").Resize(dic.Count, 1).Value = Application.Transpose(dic.items)
End Sub
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,943
Members
448,534
Latest member
benefuexx

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