Count numbers in data

SanjayaGarg

New Member
Joined
Nov 10, 2017
Messages
27
Office Version
  1. 2013
Platform
  1. Windows
Dear Friends ! I have a large data in a column containing 'Route No.' and below it nos. then again route no. then no. and so on. as shown in attached example. Now the required thing is that route nos are shown in another column continuously and count of nos. in that route no. in front of that. e.g.
1713971393069.png
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
FWIW, when you post an image of your data instead of something that can be copied and pasted into a spreadsheet, it is difficult for others to experiment with it. Which means your chances of getting help drop significantly. Instead, use the free XL2BB tool (link below) to post your data in a way that makes it accessible to others.

 
Upvote 0
Dear Friends ! I have a large data in a column containing 'Route No.' and below it nos. then again route no. then no. and so on. as shown in attached example. Now the required thing is that route nos are shown in another column continuously and count of nos. in that route no. in front of that. e.g. View attachment 110439
Result
DataRoute No.CountSum
Route No. 54Route No. 54
5​
516​
45​
Route No. 56
4​
1122​
345​
Route No. 59
5​
3513​
23​
Route No. 60
6​
2302​
57​
46​
Route No. 56
487​
324​
78​
233​
Route No. 59
21​
435​
445​
267​
2345​
Route No. 60
59​
467​
234​
276​
879​
387​
 
Upvote 0
Can it be done?
Because the route number of each block only appears at the top and not on each row, and the number of rows in each block can change, the only way I can think of is to use VBA.
Are you open to a VBA solution?
 
Upvote 0
Here is the VBA code I came up with:
VBA Code:
Sub GetTotals()

    Dim rng1 As Range
    Dim cell As Range
    Dim rt As String
    Dim fnd As Range
    Dim ct As Integer
    Dim tot As Double
    Dim lr As Long
    Dim rng2 As Range
        
    Application.ScreenUpdating = False
    
'   Get range of values to total (starting in cell C3)
    Set rng1 = Range("C3:C" & Range("C2").End(xlDown).Row)
    
'   Loop through all cells in range of values to total
    For Each cell In rng1
'       Get route number
        rt = cell.Value
'       Search for route number in column A
        Set fnd = Columns("A:A").Find(What:=rt, After:=Range("A1"), LookIn:= _
            xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False)
'       If not found, put 0 for Count and Sum in columns D and E
        If fnd Is Nothing Then
            cell.Offset(0, 1).Value = 0
            cell.Offset(0, 2).Value = 0
        Else
'           See if any data below heading, and if not, put in zeroes
            If fnd.Offset(1, 0) = "" Then
                cell.Offset(0, 1).Value = 0
                cell.Offset(0, 2).Value = 0
            Else
'               Find last row in data block
                lr = fnd.End(xlDown).Row
'               Get counts and sums
                Set rng2 = Range(Cells(fnd.Row + 1, "A"), Cells(lr, "A"))
                cell.Offset(0, 1).Value = lr - fnd.Row
                cell.Offset(0, 2).Value = Application.WorksheetFunction.Sum(rng2)
            End If
'           Reset found range
            Set fnd = Nothing
        End If
    Next cell
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"
    
End Sub

And here is the proof (everything in red populated by VBA code):
1714055891710.png
 
Upvote 0
Solution
Thanks Joe for the code. Can this be done without VBA code, only by using formula. Actually this was asked in job interview. Interviewer asked that without VBA code how would you get the result? Of course, question was not answered.
 
Upvote 0
In the older version, I used a helper column. However, with a newer version like 365, it can be done without the helper. Not sure which was allowed. I think the tricky part was to get the label for each route.
Book1
ABCDEF
1
2DataHelper
3Route No. 54 Route No.CountSum
445.00Route No. 54Route No. 545516
5345.00Route No. 54Route No. 5641122
623.00Route No. 54Route No. 5953513
757.00Route No. 54Route No. 6062302
846.00Route No. 54
9 
10Route No. 56 
11487.00Route No. 56
12324.00Route No. 56
1378.00Route No. 56
14233.00Route No. 56
15 
16Route No. 59 
1721.00Route No. 59
18435.00Route No. 59
19445.00Route No. 59
20267.00Route No. 59
212345.00Route No. 59
22 
23Route No. 60 
2459.00Route No. 60
25467.00Route No. 60
26234.00Route No. 60
27276.00Route No. 60
28879.00Route No. 60
29387.00Route No. 60
Sheet8
Cell Formulas
RangeFormula
E4:E7E4=COUNTIF($B$3:$B$29,D4)
F4:F7F4=SUMIF($B$3:$B$29,D4,$A$3:$A$29)
B3:B29B3=IF(ISNUMBER(A3),LOOKUP(2,1/(LEFT($A$3:A3,5)="Route"),$A$3:A3),"")
 
Last edited:
Upvote 0
In the older version, I used a helper column. However, with a newer version like 365, it can be done without the helper. Not sure which was allowed. I think the tricky part was to get the label for each route.
Book1
ABCDEF
1
2DataHelper
3Route No. 54 Route No.CountSum
445.00Route No. 54Route No. 545516
5345.00Route No. 54Route No. 5641122
623.00Route No. 54Route No. 5953513
757.00Route No. 54Route No. 6062302
846.00Route No. 54
9 
10Route No. 56 
11487.00Route No. 56
12324.00Route No. 56
1378.00Route No. 56
14233.00Route No. 56
15 
16Route No. 59 
1721.00Route No. 59
18435.00Route No. 59
19445.00Route No. 59
20267.00Route No. 59
212345.00Route No. 59
22 
23Route No. 60 
2459.00Route No. 60
25467.00Route No. 60
26234.00Route No. 60
27276.00Route No. 60
28879.00Route No. 60
29387.00Route No. 60
Sheet8
Cell Formulas
RangeFormula
E4:E7E4=COUNTIF($B$3:$B$29,D4)
F4:F7F4=SUMIF($B$3:$B$29,D4,$A$3:$A$29)
B3:B29B3=IF(ISNUMBER(A3),LOOKUP(2,1/(LEFT($A$3:A3,5)="Route"),$A$3:A3),"")
Thanks Cubist. Solved the problem. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,327
Members
453,032
Latest member
Pauh

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