adding +1 to occurence

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

adding +1 to occurence

Post by sal21 »

I have this case select:


'CHECK PER CODICE
Select Case COD
Case "VA", "VB", "VC", "VD"
CHECK_COD = True
Case Else
CHECK_COD = False
End Select
'CHECK PER CODICE

I need to add +1 for each occurence when the select case is true, how to

Similar:

selct case =VA
VA=VA+1
ecc...

note:
COD is a string part from the loop of txt file where i read line by line

i think with Dictionary object, but not for me:-(

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

Do you have variables VA, VB, VC and VD?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:Do you have variables VA, VB, VC and VD?
no, i check into the select case only this 4 elelment.

sorry me :grin:
COD is a Strng variable

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

Then I don't understand what you want to do.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:Then I don't understand what you want to do.
Based the Var COD

i need to fill a Dictionary Object, similar:

the var VA not exists into the Dictionary adding item VA with the Key value = 1

in effect i need to store in a Dictionary object how many occurrence for each var COD are found during the loop of txt file, but only if the var COD is true into the selct case...

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

You are only looking for 4 values, VA, VB, VC and VD. Why use a dictionary? You can use variables VA, VB, VC and VD, or if you prefer VA_Conta, VB_Conta, VC_Conta and VD_Conta:

Code: Select all

    Dim VA_Conta As Long, VB_Conta As Long, VC_Conta As Long, VD_Conta As Long
    'CHECK PER CODICE
    Select Case COD
        Case "VA"
            CHECK_COD = True
            VA_Conta = VA_Conta + 1
        Case "VB"
            CHECK_COD = True
            VB_Conta = VB_Conta + 1
        Case "VC"
            CHECK_COD = True
            VC_Conta = VC_Conta + 1
        Case "VD"
            CHECK_COD = True
            VD_Conta = VD_Conta + 1
        Case Else
            CHECK_COD = False
    End Select
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:You are only looking for 4 values, VA, VB, VC and VD. Why use a dictionary? You can use variables VA, VB, VC and VD, or if you prefer VA_Conta, VB_Conta, VC_Conta and VD_Conta:

Code: Select all

    Dim VA_Conta As Long, VB_Conta As Long, VC_Conta As Long, VD_Conta As Long
    'CHECK PER CODICE
    Select Case COD
        Case "VA"
            CHECK_COD = True
            VA_Conta = VA_Conta + 1
        Case "VB"
            CHECK_COD = True
            VB_Conta = VB_Conta + 1
        Case "VC"
            CHECK_COD = True
            VC_Conta = VC_Conta + 1
        Case "VD"
            CHECK_COD = True
            VD_Conta = VD_Conta + 1
        Case Else
            CHECK_COD = False
    End Select

i need to loop the Dictionary elelment, for othe code :grin:

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

In the declaration section:

Code: Select all

    Dim dic As Variant
    Set dic = CreateObject(Class:="Scripting.Dictionary")
and in the loop:

Code: Select all

    'CHECK PER CODICE
    Select Case COD
        Case "VA", "VB", "VC", "VD"
            If dic.Exists(COD) Then
                dic(COD) = dic(COD) + 1
            Else
                dic.Add COD, 1
            End If
            CHECK_COD = True
        Case Else
            CHECK_COD = False
    End Select
    'CHECK PER CODICE
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:In the declaration section:

Code: Select all

    Dim dic As Variant
    Set dic = CreateObject(Class:="Scripting.Dictionary")
and in the loop:

Code: Select all

    'CHECK PER CODICE :thankyou: 
    Select Case COD
        Case "VA", "VB", "VC", "VD"
            If dic.Exists(COD) Then
                dic(COD) = dic(COD) + 1
            Else
                dic.Add COD, 1
            End If
            CHECK_COD = True
        Case Else
            CHECK_COD = False
    End Select
    'CHECK PER CODICE
:thankyou: :thankyou: :thankyou:

I L**** you :grin:

i beelive you have lost the loop of dictionary items ... :grin: :grin:

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

sal21 wrote:i beelive you have lost the loop of dictionary items ... :grin: :grin:
Huh? :scratch:
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:
sal21 wrote:i beelive you have lost the loop of dictionary items ... :grin: :grin:
Huh? :scratch:

now, when finish to read the txt file, i need to loop all items filled into the Dictionary....

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

You can use a loop like this:

Code: Select all

    Dim key As Variant
    Dim lngValue as Long
    For Each key In dic.Keys
        lngValue = dic(key)
        ' Do something with the key and value, for example
        MsgBox "The item " & key & " occurred " & lngValue & " time(s)"
    Next key
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:You can use a loop like this:

Code: Select all

    Dim key As Variant
    Dim lngValue as Long
    For Each key In dic.Keys
        lngValue = dic(key)
        ' Do something with the key and value, for example
        MsgBox "The item " & key & " occurred " & lngValue & " time(s)"
    Next key
Hans sorry if i post on old question... :grin:

but exists a method to retrive from the for each cycle, the number of occurences for a single Key of COD?

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

Please explain in more detail what you want.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:Please explain in more detail what you want.
aside from +1 i need to count how many occurence is matched the COD

peraphs i need to ad a new counter +1 in Dictionary Key?

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

But I thought the loop at the end

Code: Select all

    Dim key As Variant
    Dim lngValue as Long
    For Each key In dic.Keys
        lngValue = dic(key)
        ' Do something with the key and value, for example
        MsgBox "The item " & key & " occurred " & lngValue & " time(s)"
    Next key
did this. If not, you will have to explain in detail and clearly what you want.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:But I thought the loop at the end

Code: Select all

    Dim key As Variant
    Dim lngValue as Long
    For Each key In dic.Keys
        lngValue = dic(key)
        ' Do something with the key and value, for example
        MsgBox "The item " & key & " occurred " & lngValue & " time(s)"
    Next key
did this. If not, you will have to explain in detail and clearly what you want.
I'm sorry Hans but changed the initial you code in:

...
If DIC.Exists(CHIAVE) Then
DIC(CHIAVE) = DIC(CHIAVE) + TOT_GENERALE
Else
DIC.Add CHIAVE, TOT_GENERALE
End If
...

where TOT_GENERALE is aumont (and i need to summ that based CHIAVE), instead 1 similar a count.

Now i need to integrate the count +1 into the DIC routine, and i need to have, for example:

CHIAVE 100,00(70,00+30,00) 2 if CHIAVE is mathched for two occurence...
now i think is all clear, or not :grin:

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

Create a second dictionary object, say DIC2, and use the original code to increment the value of DIC2(CHIAVE) by 1.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: adding +1 to occurence

Post by sal21 »

HansV wrote:Create a second dictionary object, say DIC2, and use the original code to increment the value of DIC2(CHIAVE) by 1.
????? :scratch:

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: adding +1 to occurence

Post by HansV »

C'mon Sal, you can do this!
Best wishes,
Hans