Results 1 to 8 of 8
  1. #1
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i have a a for next

    similar:

    for x = 0 to 100

    my_var="500"

    next

    during this loop the variable my_vare is dinamiclly and it can assume for example 500,4784,9999....ecc

    Is possible to assign a counter COUNT=COUNT+1 only if my_var is a new value????...

    i have think to use array or collection or...:

    for x = 0 to 100

    my_var="500"

    array=500

    next

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Try this:

    Code:
    Dim col As New Collection
    Dim x As Long
    Dim my_var
    
    For x = 0 To 100
      my_var = ...
      On Error Resume Next
      col.Add my_var, CStr(my_var)
      On Error GoTo 0 ' or go to the error handler if you have one
    Next x
    
    MsgBox "The number of distinct values is " & col.Count

  3. #3
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='770851' date='16-Apr-2009 17:41']Try this:

    Code:
    Dim col As New Collection
    Dim x As Long
    Dim my_var
    
    For x = 0 To 100
      my_var = ...
      On Error Resume Next
      col.Add my_var, CStr(my_var)
      On Error GoTo 0 ' or go to the error handler if you have one
    Next x
    
    MsgBox "The number of distinct values is " & col.Count
    [/quote]

    tKS! "Big Brother"

  4. #4
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='770851' date='16-Apr-2009 17:41']Try this:

    Code:
    Dim col As New Collection
    Dim x As Long
    Dim my_var
    
    For x = 0 To 100
      my_var = ...
      On Error Resume Next
      col.Add my_var, CStr(my_var)
      On Error GoTo 0 ' or go to the error handler if you have one
    Next x
    
    MsgBox "The number of distinct values is " & col.Count
    [/quote]

    Hi Hans...
    But if existis a dupe how to know wich element key and name is?

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You could add some lines:

    Code:
    Dim col As New Collection
    Dim x As Long
    Dim my_var
    
    For x = 0 To 100
      my_var = ...
      On Error Resume Next
      col.Add my_var, CStr(my_var)
      ' *** Check whether an error occurred ***
      If Err Then
    	MsgBox "Duplicate value " & my_var
      End If
      ' ***************************************
      On Error GoTo 0 ' or go to the error handler if you have one
    Next x
    
    MsgBox "The number of distinct values is " & col.Count

  6. #6
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='771241' date='19-Apr-2009 13:16']You could add some lines:

    Code:
    Dim col As New Collection
    Dim x As Long
    Dim my_var
    
    For x = 0 To 100
      my_var = ...
      On Error Resume Next
      col.Add my_var, CStr(my_var)
      ' *** Check whether an error occurred ***
      If Err Then
    	MsgBox "Duplicate value " & my_var
      End If
      ' ***************************************
      On Error GoTo 0 ' or go to the error handler if you have one
    Next x
    
    MsgBox "The number of distinct values is " & col.Count
    [/quote]

    Work fine, but really my question is:wath is the number of key when code found dupe...

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='sal21' post='771259' date='19-Apr-2009 19:14']wath is the number of key when code found dupe...[/quote]
    If you want to keep track of the number, you can use the loop index x.

  8. #8
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='771268' date='19-Apr-2009 21:42']If you want to keep track of the number, you can use the loop index x.[/quote]

    Sorry i am stupid...work with X index

Posting Permissions

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