|
-
Sep 2nd, 2011, 11:07 PM
#1
Thread Starter
Addicted Member
find how many times a cirtain number falls in a Range of numbers
Hi,
Iam trying to create a small pick 4 program where i want to find out how many
times a numer falls whithing a group of numbers.
For example if I have numbers 0 to 9 and then divide this numbers in 4 groups
0 to 2, 3 to 4, 5 to 5 and 7 to 9. and if the number I want to use is 1224, what I would like for the program to show is: 0 to 2 = 3 and 3 to 4 = 1.
Here I include the code that Iam using.
Code:
Private Sub CheckForSeconds()
note: Iam getting the value from another Flexgrid on form1.
Dim i3 As Integer
Dim c3(10) As Integer
Dim d3 As Integer
c3(1) = 0
c3(2) = 0
c3(3) = 0
c3(4) = 0
With Form1.Grid1
For i3 = 1 To .Rows - 1
For d3 = 1 To 4
If .TextMatrix(i3, d3) <= 2 Then 'values 0-1-2
c3(1) = 1
Grid1.TextMatrix(i3, 4) = c3(1) + 1
End If
If .TextMatrix(i3, d3) = 3 Or .TextMatrix(i3, d3) = 4 Then 'values 3-4
c3(2) = 1
Grid1.TextMatrix(i3, 5) = c3(2) + 1
End If
If .TextMatrix(i3, d3) = 5 Or .TextMatrix(i3, d3) = 6 Then 'values 5-6
c3(3) = 1
Grid1.TextMatrix(i3, 6) = c3(3) + 1
End If
If .TextMatrix(i3, d3) > 6 Then 'values 7-8-9
c3(4) = 1
Grid1.TextMatrix(i3, 7) = c3(4) + 1
End If
Next
Next
End With
End Sub
-
Sep 3rd, 2011, 01:41 AM
#2
Re: find how many times a cirtain number falls in a Range of numbers
i m not using the grids, this is just an example of getting the counts of each group, just try and feedback.
Code:
Private Sub Command1_Click()
Dim Grp(3) As Boolean, GrpCnt(3) As Integer
Dim Myval As Long, num As Integer, i As Integer, j As Integer
Myval = 1224
'reset counts
For j = LBound(Grp) To UBound(Grp)
GrpCnt(j) = 0
Next
For i = 1 To Len(Myval)
num = Val(Mid$(Myval, i, 1))
'add more groups if need
Grp(0) = num >= 0 And num <= 2
Grp(1) = num >= 3 And num <= 4
Grp(2) = num >= 5 And num <= 5
Grp(3) = num >= 7 And num <= 9
For j = LBound(Grp) To UBound(Grp)
If Grp(j) = True Then
GrpCnt(j) = GrpCnt(j) + 1
End If
Next
Next
For j = LBound(Grp) To UBound(Grp)
Debug.Print "Grp"; j + 1; "="; GrpCnt(j)
Next
End Sub
-
Sep 3rd, 2011, 09:00 AM
#3
Thread Starter
Addicted Member
Re: find how many times a cirtain number falls in a Range of numbers
Thanks,Seenu 1st,
Your code works ok but it only prints to a form.
Iam trying to use it with my flexgrid. since
I use the flexgrid to compare to older values.
Monti124
-
Sep 3rd, 2011, 09:09 AM
#4
Re: find how many times a cirtain number falls in a Range of numbers
yes it's just an exampleto get the groups, try the logic with gids, if u stuck anywhere, me or someone will help u.
-
Sep 4th, 2011, 10:06 AM
#5
Thread Starter
Addicted Member
Re: find how many times a cirtain number falls in a Range of numbers
seenu,
I try to use the group cont in my fgrid but
it apparently the last part of the code
"Grp"; j + 1; "="; GrpCnt(j)
ony works with the print statement.
Monti124
-
Sep 4th, 2011, 10:34 AM
#6
Re: find how many times a cirtain number falls in a Range of numbers
where do u need to use that part of code?
-
Sep 4th, 2011, 11:07 AM
#7
Thread Starter
Addicted Member
Re: find how many times a cirtain number falls in a Range of numbers
this is the way I tried with your code
Code:
Private Sub CheckForSeconds()
Dim Grp(3) As Boolean, GrpCnt(3) As Integer
Dim Myval As Long, num As Integer, i As Integer, j As Integer
Dim R As Integer
With Grid1
For R = 1 To .Rows - 1
Myval = .TextMatrix(R, 1) ' This is what I added.
'reset counts
For j = LBound(Grp) To UBound(Grp)
GrpCnt(j) = 0
Next
For i = 1 To Len(Myval)
num = Val(Mid$(Myval, i, 1))
'add more groups if need
Grp(0) = num >= 0 And num <= 2
Grp(1) = num >= 3 And num <= 4
Grp(2) = num >= 5 And num <= 5
Grp(3) = num >= 7 And num <= 9
For j = LBound(Grp) To UBound(Grp)
If Grp(j) = True Then
GrpCnt(j) = GrpCnt(j) + 1
End If
Next
Next
For j = LBound(Grp) To UBound(Grp)
Debug.Print "Grp"; j + 1; "="; GrpCnt(j)
' this is where I need to add the code
.TextMatrix(R, 4) = Grp(1) count result
.TextMatrix(R, 5) = Grp(2) count result
.TextMatrix(R, 6) = Grp(3) count result
'.TextMatrix(R, 7) = Grp(4) count result
Next
End Sub
-
Sep 4th, 2011, 09:12 PM
#8
Re: find how many times a cirtain number falls in a Range of numbers
no need to use the for next if u dont want to loop, just try like this
Code:
.TextMatrix(R, 4) = GrpCnt(0) 'count result
.TextMatrix(R, 5) = GrpCnt(1) 'count result
.TextMatrix(R, 6) = GrpCnt(2) 'count result
.TextMatrix(R, 7) = GrpCnt(3) 'count result
and dont use R for the Row value, better put the number directly.
-
Sep 5th, 2011, 06:18 AM
#9
Thread Starter
Addicted Member
Re: find how many times a cirtain number falls in a Range of numbers
Thank you Seenu the code works as I spected.
-
Sep 5th, 2011, 06:22 AM
#10
Re: find how many times a cirtain number falls in a Range of numbers
happy to help, pls mark thread as resolved using thread tools above.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|