Consulting

Results 1 to 8 of 8

Thread: CELL with or without .value

  1. #1

    CELL with or without .value

    Sub t4()Dim dic As Object
    Dim i As Long
    Dim irow  As Long
    
    Worksheets("sheet3").Select
    Set dic = CreateObject("scripting.dictionary")
    irow = Cells(Rows.Count, 1).End(xlUp).Row
    Range("f2:F7").ClearContents
    
    For i = 2 To irow
        dic.Add Cells(i, 1).Value, Cells(i, 2).Value
    Next i
    
    For i = 2 To irow
        Cells(i, 6).Value = dic.Item(Cells(i, 5).Value)
    Next i
    
    Set dic = Nothing
    End Sub
    dic.Add Cells(i, 1).Value, Cells(i, 2).Value
    Cells(i, 6).Value = dic.Item(Cells(i, 5).Value)

    dic.Add Cells(i, 1), Cells(i, 2)
    Cells(i, 6) = dic.Item(Cells(i, 5))

    With or without Value makes different results,
    Cells(i, 1).Value is not equal to Cells(i, 1)?

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    Could you supply a spreadsheet with such values? Only need a handful of values which demonstrate the anomaly.
    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.

  3. #3

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    That's not a spreadsheet, it's a picture of one.
    I can't even begin to get the results you have in column F with your code.

    With
    dic.Add Cells(i, 1).Value, Cells(i, 2).Value
    you're adding keys and items to the dictionary which are values, which I suspect is what you want.

    With
    dic.Add Cells(i, 1), Cells(i, 2)
    you're adding the cell object (range object) to the dictionary, both as item and key. This is because you can do this with dictionaries.

    You probably want this combo:
    dic.Add Cells(i, 1).Value, Cells(i, 2).Value
    Cells(i, 6) = dic.Item(Cells(i, 5).Value)

    where the Cells(i, 6) is using the default property of Value! [actually, the default property is the Item object, but never mind!]
    All other references without .Value were referring to the cell (range) itself.

    Adding the lines:
    aaa = dic.keys
    bbb = dic.items
    somewhere in the code after the dictionary has finished being added to, then examining the contents of aaa and bbb in the Locals pane of the vbe will show you what's in the dictionary.
    Last edited by p45cal; 06-26-2017 at 06:05 AM.
    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
    I probably know what you mean, I also know that I may be a VBA beginners, so I have a lot of code do not know, thank you for your explanation, I hope to have the opportunity to further ask you about Dictionary.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I suspect you were looking for something like this

    Without the value, it's the Range that is in the dictionary, not the value of the cell

    Look at the watch window in the screen shot


    Option Explicit
    
    Sub t4()
        Dim dic As Object
        Dim i As Long
        Dim irow  As Long
         
        Worksheets("sheet1").Select
        Set dic = CreateObject("scripting.dictionary")
        
        irow = Cells(Rows.Count, 1).End(xlUp).Row
        Range("f2:F7").ClearContents
         
        For i = 2 To irow
            If dic.Exists(Cells(i, 1).Value) Then
                dic(Cells(i, 1).Value) = dic(Cells(i, 1).Value) + Cells(i, 2).Value
            Else
                dic.Add Cells(i, 1).Value, Cells(i, 2).Value
            End If
        Next I
         
        irow = Cells(Rows.Count, 5).End(xlUp).Row
        For i = 2 To irow
            If dic.Exists(Cells(i, 5).Value) Then
               Cells(i, 6).Value = dic.Item(Cells(i, 5).Value)
            End If
        Next I
         
        Set dic = Nothing
    End Sub
    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    According to your explanation, sometimes there are the same with or without Value, but a few cases are not the same,right?

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can output Keys and Items without looping

     Cells(1, 10).Resize(dic.Count) = Application.Transpose(dic.Keys)
        Cells(1, 11).Resize(dic.Count) = Application.Transpose(dic.Items)
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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