VBA Count Error

Cowichandave

New Member
Joined
Jan 18, 2009
Messages
44
Office Version
  1. 2016
In the result below countA is counting the entire column and showing 7 . It should only show 6 . The first number in column 5 (2701.12) is the sum and should not be counted. What am I forgetting? Sheet starts in Column C

VBA Code
VBA Code:
Dim ws As Worksheet
    Dim rngFull As Range
    Dim arr As Variant
    Dim rowLast As Long, i As Long
    Dim empRowFirst As Long, empRowLast As Long
   
    Set ws = ActiveSheet
    With ws
        rowLast = .Cells(Rows.count, "E").End(xlUp).Row + 2 ' Section includes 2 empty rows
        Set rngFull = .Range("C1:S" & rowLast)
        arr = rngFull.Value
   
        For i = 1 To UBound(arr)
            If arr(i, 3) <> "" Then
                If arr(i - 1, 3) = "" Then
                    empRowFirst = i
                ElseIf arr(i + 1, 3) = "" Or i = (UBound(arr) - 1) Then
                    empRowLast = i
                 
                    .Range("G" & i + 1 & ":S" & i + 1).Formula = _
                        "=COUNT(" & .Range("G" & empRowFirst & ":G" & empRowLast).Address(1, 0) & ")"
RESULT
2BON07
2701.12​
1CRE06
439.00​
2CAP21
3CAR21
215.47​
4CHE11
5CYC08
130.00​
6ELM01
416.66​
7FOOT07
770.00​
8ALL07
9ALL10
10MOR03
11NOR37
12PP07
13RCM180
14RCMPSER
729.99​
15ROG06
16UNI40
7
 
Last edited by a moderator:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Should this be
Rich (BB code):
For i =2 To UBound(arr)
 
Upvote 0
Only works for the first group. I have 50 groups. After the first this is the result for group 17. Here it shows 2 and should be 1
17KimL
343.00​
1AMA08
2BES11
3MUNIWB
343.00​
0002
 
Upvote 0
If it happens for each group, then
Rich (BB code):
"=COUNT(" & .Range("G" & empRowFirst & ":G" & empRowLast).Address(1, 0) & ")-1"
 
Upvote 0
If it happens for each group, then
Rich (BB code):
"=COUNT(" & .Range("G" & empRowFirst & ":G" & empRowLast).Address(1, 0) & ")-1"
Very Interesting. The original code I had Group 1 CountA was correct. Groups 2 to 50 had countA 1 to many. With your adding the -1 groups 2 to 50 countA are correct but group 1 countA is short 1
Also blank colums are now -1
17KimL
343.00​
1AMA08
2BES11
3MUNIWB
343.00​
-1-1-11
 
Upvote 0
did yo change this back to 1 ??
Rich (BB code):
For i =2 To UBound(arr)
 
Upvote 0
did yo change this back to 1 ??
Rich (BB code):
For i =2 To UBound(arr)
No still at 2. Changing it back to 1 makes group 1 CountA 1 short. Group 2 to 50 are correct except if there is a blank column then CountA is -1
 
Last edited:
Upvote 0
@Cowichandave
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

It would also help if you posted
  • your whole code so that we could test/modify it
  • some small sample data including say three small groups, with existing formulas with XL2BB and explain in words in relation to that sample data exactly what you are trying to do
 
Upvote 0
How are you getting your data ? I suspect that the cell in Column E that looks empty is not actually empty.

If you can rely on column D having the numeric values in it as in your sample data, you could give this a go.

VBA Code:
Sub cntsections_V02()
    Dim ws As Worksheet
    Dim rngFull As Range
    Dim arr As Variant
    Dim rowLast As Long, i As Long
    Dim empRowFirst As Long, empRowLast As Long
   
    Set ws = ActiveSheet
    With ws
        rowLast = .Cells(Rows.Count, "E").End(xlUp).Row + 2 ' Section includes 2 empty rows
        Set rngFull = .Range("C1:S" & rowLast)
        arr = rngFull.Value
   
        For i = 2 To UBound(arr)
            If Val(arr(i, 2)) <> 0 Then
                If Val(arr(i - 1, 2)) = 0 Then
                    empRowFirst = i
                ElseIf Val(arr(i + 1, 2)) = 0 Or i = (UBound(arr) - 1) Then
                    empRowLast = i
                 
                    .Range("G" & i + 1 & ":S" & i + 1).Formula = _
                        "=COUNT(" & .Range("G" & empRowFirst & ":G" & empRowLast).Address(1, 0) & ")"
                End If
            End If
        Next i
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top