Consulting

Results 1 to 5 of 5

Thread: VBA dictionary concept problem

  1. #1
    VBAX Regular
    Joined
    Nov 2011
    Posts
    10
    Location

    VBA dictionary concept problem

    Hi all, I am following some sites to learn vba dictionary right now. However, some codes I can't understand. Please help to explain. Really appreciate your help.

    Questions are inside "" after the code.

    Please see codes below.

    Sub ProcessData1()
    Dim dict As Dictionary
    Dim i As Integer
    Dim targetRow As Integer
    Dim name As String
    Dim subject As String
    Dim score As Double
    Dim more As Boolean
    Set dict = New Dictionary
    more = True
    i = 2
    Worksheets("English").UsedRange.Clear
    Worksheets("Physics").UsedRange.Clear
    Worksheets("Biology").UsedRange.Clear
    While more
    name = Worksheets("Data").Cells(i, 1).Value
        subject = Worksheets("Data").Cells(i, 3).Value
        score = Worksheets("Data").Cells(i, 4).Value
    If dict.Exists(subject) Then
            targetRow = dict.Item(subject)      "At this time, dict.item(subject)=score or name, or both?"
        Else
            targetRow = 1                "Why does targetRow=1 here?"
        End If
    Worksheets(subject).Cells(targetRow, 1) = name
        Worksheets(subject).Cells(targetRow, 2) = score
        dict.Item(subject) = targetRow + 1     
    i = i + 1
    If Len(Worksheets("Data").Cells(i, 1)) = 0 Then more = False    "Does this code mean that go until the last filled row?"
    Wend
    End Sub
    Sample.JPG
    Last edited by Aussiebear; 10-01-2023 at 01:17 AM. Reason: Added code tags to supplied code

  2. #2
    the code somewhat is incomplete. how does the each element in the dict gets Added, that is missing.

  3. #3
    VBAX Regular
    Joined
    Nov 2011
    Posts
    10
    Location
    Thanks arnelgp! the code misses the part of create 3 sheets named "English", "Physics", "Biology". After creating these 3 sheets, it can run perfectly. I got the whole tutorial from a website and I believe that's all codes they provided.

    As an example here's a table of raw exam data by subject:

    Name Date Subject Score
    Alex 04/02/2009 Biology 60.00
    Alex 06/03/2009 English 60.00
    Angela 04/02/2009 Biology 47.00
    Angela 06/03/2009 Physics 47.00
    Bharat 02/02/2009 English 64.00
    Bharat 04/03/2009 English 64.00
    Christine 03/02/2009 Physics 52.00
    Christine 05/03/2009 Physics 52.00
    George 03/02/2009 Physics 71.00
    George 05/03/2009 English 71.00
    Gilbert 03/02/2009 Physics 14.00
    Gilbert 05/03/2009 Physics 14.00
    Jane 02/02/2009 English 56.00
    Jane 04/03/2009 Biology 56.00
    Peter 02/02/2009 English 45.00
    Peter 03/02/2009 Physics 52.00
    Peter 04/03/2009 English 45.00
    Peter 05/03/2009 Physics 52.00
    The VBA code to do the processing looks like this:

    Sub ProcessData1()
    Dim dict As Dictionary
    Dim i As Integer
    Dim targetRow As Integer
    Dim name As String
    Dim subject As String
    Dim score As Double
    Dim more As Boolean
    Set dict = New Dictionary
    more = True
    i = 2
    Worksheets("English").UsedRange.Clear
    Worksheets("Physics").UsedRange.Clear
    Worksheets("Biology").UsedRange.Clear
    While more
    name = Worksheets("Data").Cells(i, 1).Value
        subject = Worksheets("Data").Cells(i, 3).Value
        score = Worksheets("Data").Cells(i, 4).Value
    If dict.Exists(subject) Then
        targetRow = dict.Item(subject)
        Else
        targetRow = 1
        End If
    Worksheets(subject).Cells(targetRow, 1) = name
        Worksheets(subject).Cells(targetRow, 2) = score
        dict.Item(subject) = targetRow + 1
    i = i + 1
    If Len(Worksheets("Data").Cells(i, 1)) = 0 Then more = False
    Wend
    End Sub

    Running this allows us to process the data and produce a sheet that looks like this:

    Alex 60
    Bharat 64
    Bharat 64
    George 71
    Jane 56
    Peter 45
    Peter 45


    Thanks again!
    Last edited by Aussiebear; 10-02-2023 at 11:12 PM. Reason: Sigh... had to add code tags to supplied code again.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by arnelgp View Post
    how does the each element in the dict gets Added
    without actually opening the file to check, I think on this line:
    dict.Item(subject) = targetRow + 1
    (I think it's called implicit addition)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5

    you are correct!

Posting Permissions

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