adding +1 to occurence
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
adding +1 to occurence
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:-(
'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:-(
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
no, i check into the select case only this 4 elelment.HansV wrote:Do you have variables VA, VB, VC and VD?
sorry me
COD is a Strng variable
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
Based the Var CODHansV wrote:Then I don't understand what you want to do.
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...
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: adding +1 to occurence
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
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
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
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: adding +1 to occurence
In the declaration section:
and in the loop:
Code: Select all
Dim dic As Variant
Set dic = CreateObject(Class:="Scripting.Dictionary")
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
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
HansV wrote:In the declaration section:
and in the loop:Code: Select all
Dim dic As Variant Set dic = CreateObject(Class:="Scripting.Dictionary")
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
I L**** you
i beelive you have lost the loop of dictionary items ...
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: adding +1 to occurence
Huh?sal21 wrote:i beelive you have lost the loop of dictionary items ...
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
HansV wrote:Huh?sal21 wrote:i beelive you have lost the loop of dictionary items ...
now, when finish to read the txt file, i need to loop all items filled into the Dictionary....
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: adding +1 to occurence
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
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
Hans sorry if i post on old question...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
but exists a method to retrive from the for each cycle, the number of occurences for a single Key of COD?
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
aside from +1 i need to count how many occurence is matched the CODHansV wrote:Please explain in more detail what you want.
peraphs i need to ad a new counter +1 in Dictionary Key?
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: adding +1 to occurence
But I thought the loop at the end
did this. If not, you will have to explain in detail and clearly what you want.
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
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
I'm sorry Hans but changed the initial you code in:HansV wrote:But I thought the loop at the end
did this. If not, you will have to explain in detail and clearly what you want.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
...
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
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: adding +1 to occurence
Create a second dictionary object, say DIC2, and use the original code to increment the value of DIC2(CHIAVE) by 1.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4362
- Joined: 26 Apr 2010, 17:36
Re: adding +1 to occurence
?????HansV wrote:Create a second dictionary object, say DIC2, and use the original code to increment the value of DIC2(CHIAVE) by 1.
-
- Administrator
- Posts: 78524
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands