Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 36 of 36

Thread: Getting max value from a dictionary

  1. #21
    Quote Originally Posted by snb View Post
    Why do you ignore all apparently not so much appreciated suggestions in this thread ?
    I haven't ignored any of them , in fact I have tried everything suggested yet I still have a dictionary that is overwritten each line and can't get a max value as a result.

    The problem is where to create the dictionary so that I do not create a new one each time I write to it?

  2. #22
    Quote Originally Posted by snb View Post
    Why do you ignore all apparently not so much appreciated suggestions in this thread ?
    I have not ignored the suggestions , in fact I have tried each and everyone extensively without success

  3. #23
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    There is no looping at all in what you posted - why did you remove it? It makes it much harder to help you if you won't post your code. Declare and instantiate the dictionary before whatever your loop is, then add the items to it inside the loop.
    Be as you wish to seem

  4. #24
    Quote Originally Posted by Aflatoon View Post
    There is no looping at all in what you posted - why did you remove it? It makes it much harder to help you if you won't post your code. Declare and instantiate the dictionary before whatever your loop is, then add the items to it inside the loop.
    Thanks for the reply , where should I declare and insttantiate the dictionary exactly ? If i declare it like this :-



    Public Sub collz()
    Dim dict As Dictionary
    Set dict = New Dictionary
    End Sub
    The sub where I want to write to it and access



    dict.Add inputer, y says object not defined etc

  5. #25
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Declare it in the routine with the loop that populates it.
    Be as you wish to seem

  6. #26
    Quote Originally Posted by snb View Post
    Why do you ignore all apparently not so much appreciated suggestions in this thread ?
    But is the whole problem ,it then creates a new dictionary each time where it only retains 1 line. It is then not possible to get a max value with only one line

  7. #27
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Da Capo al Fine:

    Sub M_snb()
       with createobject("scripting.dictionary")
           for j=1 to 25
              x0=.Item(j*20)
           next
    
           msgbox application.max(.keys)
      end with
    End Sub

  8. #28
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by cblake843 View Post
    But is the whole problem ,it then creates a new dictionary each time where it only retains 1 line. It is then not possible to get a max value with only one line
    Just taking a wild guess here, I'd suspect that you're doing something wrong


    My post 16 has two different approaches depending on what you're trying to do


    Both will add new lines to the dictionary and both will print out the max


    If you can't get it to work, you'd better post a sample workbook with whatever code you have and a description of what it is you are trying to accomplish
    Last edited by Paul_Hossler; 12-03-2018 at 12:01 PM.
    ---------------------------------------------------------------------------------------------------------------------

    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

  9. #29
    Quote Originally Posted by snb View Post
    Da Capo al Fine:

    Sub M_snb()
       with createobject("scripting.dictionary")
           for j=1 to 25
              x0=.Item(j*20)
           next
    
           msgbox application.max(.keys)
      end with
    End Sub
    I’ve already tried something similar(see previous post) , I’m trying to write the value of y to a dict? Currently it overwrites the dict so there is only 1 line in the dict. All I need to do is create the dict in another sub but can’t quite get the code working. Any suggestions ?

  10. #30
    Quote Originally Posted by Paul_Hossler View Post
    Just taking a wild guess here, I'd suspect that you're doing something wrong


    My post 16 has two different approaches depending on what you're trying to do


    Both will add new lines to the dictionary and both will print out the max


    If you can't get it to work, you'd better post a sample workbook with whatever code you have and a description of what it is you are trying to accomplish
    Yeah thanks, it was very good example by didn’t work for my issue. How can I declare the dict outside of the sub so it doesn’t just write one line overwriting each time?
    Last edited by Paul_Hossler; 12-03-2018 at 12:01 PM.

  11. #31
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Did you try making it a Module Level variable?

    This version keeps a max number of keys = 10

    If you want to keep on adding, then use the second approach in my #16

    Option Explicit
    
    
    'module level variable
    Dim dict As Object
    
    Sub test()
        Dim v As Variant
        Dim iMax As Long
        If dict Is Nothing Then
            Set dict = CreateObject("Scripting.Dictionary")
        End If
        
        Call test2(10)
        Call test2(12)
        Call test2(15)
        
        For Each v In dict.items
            If v > iMax Then iMax = v
        Next
        MsgBox iMax
        
        Set dict = Nothing
           
    End Sub
    
    
    
    Private Sub test2(N As Long)
        
        Dim K As Long, D As Long
        
        ' K is the Key, D is the Data
        For K = 1 To 10
            D = N * Rnd
            If dict.exists(K) Then
                If D > dict(K) Then dict(K) = D
            Else
                dict.Add K, D
            End If
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  12. #32
    Quote Originally Posted by Paul_Hossler View Post
    Did you try making it a Module Level variable?
    fyi, related:

    http://www.vbaexpress.com/forum/show...l=1#post386060

  13. #33
    Quote Originally Posted by Paul_Hossler View Post
    Did you try making it a Module Level variable?

    This version keeps a max number of keys = 10

    If you want to keep on adding, then use the second approach in my #16

    Option Explicit
    
    
    'module level variable
    Dim dict As Object
    
    Sub test()
        Dim v As Variant
        Dim iMax As Long
        If dict Is Nothing Then
            Set dict = CreateObject("Scripting.Dictionary")
        End If
        
        Call test2(10)
        Call test2(12)
        Call test2(15)
        
        For Each v In dict.items
            If v > iMax Then iMax = v
        Next
        MsgBox iMax
        
        Set dict = Nothing
           
    End Sub
    
    
    
    Private Sub test2(N As Long)
        
        Dim K As Long, D As Long
        
        ' K is the Key, D is the Data
        For K = 1 To 10
            D = N * Rnd
            If dict.exists(K) Then
                If D > dict(K) Then dict(K) = D
            Else
                dict.Add K, D
            End If
        Next
    End Sub










    Thanks , I can see how it will work but not for my issue. I want the dictionary to be populated by the below sub and also accessed each pass in the same sub. I need it to take the value of y from the callcoordinates sub below

    
    
    
    
    Private Sub CalculateCoordinates(sThread, node As IXMLDOMNode)
    
    
    Dim parent_node As IXMLDOMNode, x As Integer, y As Integer, y_max_branch As Integer, nList As IXMLDOMNodeList
    Dim StepId As String, strXpath As String
    Dim inputer, inputer2, inputer3, inputer4 As String
    Dim stry As String
    
     
    
    
    Call AddCoordinates(node, x, y)
    End Sub

  14. #34
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Thanks , I can see how it will work but not for my issue. I want the dictionary to be populated by the below sub and also accessed each pass in the same sub. I need it to take the value of y from the callcoordinates sub below
    I'm struggling to see why it would NOT work

    Again, if you attach a sample workbook showing what you start with and what you want to do it will be easier
    ---------------------------------------------------------------------------------------------------------------------

    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

  15. #35
    So for the routine I posted in 33 , where should i define the dicitonary (i.e not in the SUB as it creates a new one each time) and where to write to it and where to access it? When I define the dictionary outside of the sub , i get errors like object required etc etc . I can't post the whole workbook.

  16. #36
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by cblake843 View Post
    So for the routine I posted in 33 , where should i define the dicitonary (i.e not in the SUB as it creates a new one each time) and where to write to it and where to access it? When I define the dictionary outside of the sub , i get errors like object required etc etc . I can't post the whole workbook.
    1. Yes
    2. Use CreateObject() in the right place to create a single instance of dict
    3. Make a small sample workbook with your macros and some sample data that does show the problem and post that
    ---------------------------------------------------------------------------------------------------------------------

    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

Tags for this Thread

Posting Permissions

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