View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Doug Glancy[_6_] Doug Glancy[_6_] is offline
external usenet poster
 
Posts: 30
Default Count each group of values

JP,

I think this does what you want. Let me know:

Sub test()

Dim r, x_range, result_range As Range
Dim i As Long
Dim msg As String

msg = ""
For Each r In Selection
If UCase(r) = "X" Then
If IsEmpty(x_range) Then
Set x_range = r
Else
Set x_range = Union(x_range, r)
End If
End If
Next r

If Not IsEmpty(x_range) Then
Set result_range = ActiveSheet.Range("A2")
With result_range
.Cells(0, 1) = "Group"
.Cells(0, 2) = "X count"
.Cells(0, 3) = "Hours"
For i = 1 To x_range.Areas.Count
.Cells(i, 1) = i
.Cells(i, 2) = x_range.Areas(i).Cells.Count
.Cells(i, 3).Formula = "=B" & Rows(i).Row + 1 & "*6"
Next i
End With
Else
MsgBox "No Xs in selected area"
End If

End Sub

Doug

"JP" wrote in message
...
Hello Doug

Thanks for your colaboration.
Another help please:

Itīs possible fix the results in a range of cells. I need the values

always
in some cells to make calculations.
Sample:A1:A7 groups; B1:B7 x counts and C1:C7 where y put my needed
calculations to be used by entire Worksheet functions
The ranges are flexible, another ranges be Ok.
A B C
Group X count Hours
1 5 30
2 4 24
3 10 48
4 2 12
5 3 18
6 4 24


Im a Health worker and this Worksheet is intended to count the lacks of my
personal group of workers in the hospital.
1 "x" = 1/5 of the week of work(30 hours) = 6
2 "x" = 2/5 .... =12
3 "x" = 3/5 .... =18
4 "x" = 4/5 .... =24
5, 6 or 7 "x" = 5/5 .... =30

Thanks for your attention
Best Regards
Paulo Coelho




"Doug Glancy" wrote in message
...
JP,

Here's one that puts the results in rows in the Column A below any other
data. If you need something else, let me know. I changed a couple of

other
things too:

Sub test()
Dim r, x_range, result_range As Range
Dim i As Long
Dim msg As String

msg = ""
For Each r In Selection
If UCase(r) = "X" Then
If IsEmpty(x_range) Then
Set x_range = r
Else
Set x_range = Union(x_range, r)
End If
End If
Next r

If Not IsEmpty(x_range) Then
Set result_range = ActiveSheet.Range("A" & ActiveSheet.Range("A" &
Rows.Count).End(xlUp).Row + 3)
With result_range
.Cells(0, 1) = "Group"
.Cells(0, 2) = "X count"
For i = 1 To x_range.Areas.Count
.Cells(i, 1) = i
.Cells(i, 2) = x_range.Areas(i).Cells.Count
Next i
End With
Else
MsgBox "No Xs in selected area"
End If

End Sub

hth,

Doug
"JP" wrote in message
...
Thank you very much for your help ACW.

Thank you very much "Doug Glancy", your solution is the best for my

work.
Before this solution i need a lot of formulas to get the some results,

now
is very simple with your code.

It's possible get the msg results in a range of cells? I need the

values
number of counted "x" to calculate another things.
Sample: If a group of "x" = 7 then 7*another value

Thanks for your dedicated attention.I have excel experience but in vba

i'm
a
novice.
Excuse for my english.
Paulo Coelho





"Doug Glancy" wrote in message
...
woops, left in a couple of extra variables there, so for the sake of
neatness:

Dim r, x_range As Range
Dim i As Long
Dim msg As String

msg = ""

For Each r In Selection
If UCase(r) = "X" Then
If x_range Is Nothing Then
Set x_range = r
Else
Set x_range = Union(x_range, r)
End If
End If
Next r

For i = 1 To x_range.Areas.Count
msg = msg & "Group " & i & ":" & x_range.Areas(i).Cells.Count &

"
x"
&
vbCrLf
Next i

MsgBox msg

End Sub

hth,

Doug

"JP" wrote in message
...
I need a formula or vba code to count how many groups of "X"

values
and
how
many "X" in each group.
Sample:

a1
ah1
X X X X X X X A A A A X X X X X X B B B B B B X X X X X X X X X X

X
X

Result i need with the code or formula
1 group 7 "x"
2 group 6 "x"
3 group 12"x"

Thanks for help
Paulo