Help using CountA or ??

Cowichandave

New Member
Joined
Jan 18, 2009
Messages
44
Office Version
  1. 2016
Each number in Column A is a person in Col B. I need a count for total entries in Col D to G for each person. Would CountA work. I am lost on this one. Any help would be appreciated.



A​
B​
C​
D​
E​
F​
G​
1
BobT
149N01
2BAS16
3DRR03
150.00​
4ATB19
339.99​
5AUM02
6BAY05
7BUI05
199.99​
8CAC03
399.94​
639.90​
2339.96​
9CAS21
10COM45
140.00​
11CONT07
298.93​
119.77​
2
3
3
1
2
BonR
115520
2CRE06
439.00​
3CAP21
195.00​
4CARL07
215.47​
5CHE11
100.00​
2
0
1
1
3
ChrisB
1CHI18
114.50​
24TH01
2066.89​
1129.94​
149.70​
3AND12
415520
86.20​
819.20​
5ATC21
6CAN91
1095.00​
7CAO01
259.56​
1691.00​
8CAR32
1316.93​
9CSC07
490.00​
490.00​
10COM44
909.20​
573.54​
975.30​
11DOW04
568.97​
12ENB03
13FAL04
14INF33
303.24​
15MAS13
515.52​
1740.35​
711.64​
769.28​
6
4
7
4
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
1235.xlsm
ABCDEFG
11BobT
2149N01
32BAS16
43DRR03150.00
54ATB19339.99
65AUM02
76BAY05
87BUI05199.99
98CAC03399.94639.902339.96
109CAS21
1110COM45140.00
1211CONT07298.93119.77
132331
14
152BonR
16115520
172CRE06439.00
183CAP21195.00
194CARL07215.47
205CHE11100.00
212011
22
233ChrisB
241CHI18114.50
2524TH012066.891129.94149.70
263AND12
2741552086.20819.20
285ATC21
296CAN911095.00
307CAO01259.561691.00
318CAR321316.93
329CSC07490.00490.00
3310COM44909.20573.54975.30
3411DOW04568.97
3512ENB03
3613FAL04
3714INF33303.24
3815MAS13515.521740.35711.64769.28
396474
Sheet4
Cell Formulas
RangeFormula
D13:G13D13=COUNTA(D2:D12)
D21:G21D21=COUNTA(D16:D20)
D39:G39D39=COUNTA(D24:D38)
 
Upvote 0
1235.xlsm
ABCDEFG
11BobT
2149N01
32BAS16
43DRR03150.00
54ATB19339.99
65AUM02
76BAY05
87BUI05199.99
98CAC03399.94639.902339.96
109CAS21
1110COM45140.00
1211CONT07298.93119.77
132331
14
152BonR
16115520
172CRE06439.00
183CAP21195.00
194CARL07215.47
205CHE11100.00
212011
22
233ChrisB
241CHI18114.50
2524TH012066.891129.94149.70
263AND12
2741552086.20819.20
285ATC21
296CAN911095.00
307CAO01259.561691.00
318CAR321316.93
329CSC07490.00490.00
3310COM44909.20573.54975.30
3411DOW04568.97
3512ENB03
3613FAL04
3714INF33303.24
3815MAS13515.521740.35711.64769.28
396474
Sheet4
Cell Formulas
RangeFormula
D13:G13D13=COUNTA(D2:D12)
D21:G21D21=COUNTA(D16:D20)
D39:G39D39=COUNTA(D24:D38)
 
Upvote 0
Actually wanting a vba solution based on Range A and coming up with a CountA for Col D to G for each Range A
 
Upvote 0
Actually wanting a vba solution based on Range A and coming up with a CountA for Col D to G for each Range A
VBA Code:
Sub InsertRowsAndCountNonEmptyCells()
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim SubCode As String
    Dim NewRow As Long
    Dim CountData1 As Long, CountData2 As Long, CountData3 As Long, CountData4 As Long
    Dim i As Long

    ' Set the worksheet where your data is located
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Update with your sheet name

    ' Initialize the first new row
    NewRow = 2

    ' Find the last row with data in column B
    LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    ' Loop through the data
    For i = 2 To LastRow ' Assuming data starts from row 2
        SubCode = ws.Cells(i, 2).Value
        If SubCode <> ws.Cells(i - 1, 2).Value And SubCode <> "" Then
            ' Insert a new row when a new sub code is encountered
            ws.Rows(i + NewRow - 1).Insert Shift:=xlDown

            ' Count non-empty cells in columns Data1 to Data4 for the new row
            CountData1 = Application.WorksheetFunction.CountA(ws.Range("D" & i & ":D" & i))
            CountData2 = Application.WorksheetFunction.CountA(ws.Range("E" & i & ":E" & i))
            CountData3 = Application.WorksheetFunction.CountA(ws.Range("F" & i & ":F" & i))
            CountData4 = Application.WorksheetFunction.CountA(ws.Range("G" & i & ":G" & i))
            
            ' Display the counts in the new row's corresponding cells
            ws.Cells(i + NewRow, 4).Value = CountData1
            ws.Cells(i + NewRow, 5).Value = CountData2
            ws.Cells(i + NewRow, 6).Value = CountData3
            ws.Cells(i + NewRow, 7).Value = CountData4
            
            ' Increment the new row counter
            NewRow = NewRow + 1
        End If
    Next i
End Sub



Here Is VBA Code Just Change the name of sheet4 to your sheet name
 
Upvote 0
VBA Code:
Sub InsertRowsAndCountNonEmptyCells()
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim SubCode As String
    Dim NewRow As Long
    Dim CountData1 As Long, CountData2 As Long, CountData3 As Long, CountData4 As Long
    Dim i As Long

    ' Set the worksheet where your data is located
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Update with your sheet name

    ' Initialize the first new row
    NewRow = 2

    ' Find the last row with data in column B
    LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    ' Loop through the data
    For i = 2 To LastRow ' Assuming data starts from row 2
        SubCode = ws.Cells(i, 2).Value
        If SubCode <> ws.Cells(i - 1, 2).Value And SubCode <> "" Then
            ' Insert a new row when a new sub code is encountered
            ws.Rows(i + NewRow - 1).Insert Shift:=xlDown

            ' Count non-empty cells in columns Data1 to Data4 for the new row
            CountData1 = Application.WorksheetFunction.CountA(ws.Range("D" & i & ":D" & i))
            CountData2 = Application.WorksheetFunction.CountA(ws.Range("E" & i & ":E" & i))
            CountData3 = Application.WorksheetFunction.CountA(ws.Range("F" & i & ":F" & i))
            CountData4 = Application.WorksheetFunction.CountA(ws.Range("G" & i & ":G" & i))
          
            ' Display the counts in the new row's corresponding cells
            ws.Cells(i + NewRow, 4).Value = CountData1
            ws.Cells(i + NewRow, 5).Value = CountData2
            ws.Cells(i + NewRow, 6).Value = CountData3
            ws.Cells(i + NewRow, 7).Value = CountData4
          
            ' Increment the new row counter
            NewRow = NewRow + 1
        End If
    Next i
End Sub


Here Is VBA Code Just Change the name of sheet name

VBA Code:
Sub InsertRowsAndCountNonEmptyCells()
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim SubCode As String
    Dim NewRow As Long
    Dim CountData1 As Long, CountData2 As Long, CountData3 As Long, CountData4 As Long
    Dim i As Long

    ' Set the worksheet where your data is located
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Update with your sheet name

    ' Initialize the first new row
    NewRow = 2

    ' Find the last row with data in column B
    LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    ' Loop through the data
    For i = 2 To LastRow ' Assuming data starts from row 2
        SubCode = ws.Cells(i, 2).Value
        If SubCode <> ws.Cells(i - 1, 2).Value And SubCode <> "" Then
            ' Insert a new row when a new sub code is encountered
            ws.Rows(i + NewRow - 1).Insert Shift:=xlDown

            ' Count non-empty cells in columns Data1 to Data4 for the new row
            CountData1 = Application.WorksheetFunction.CountA(ws.Range("D" & i & ":D" & i))
            CountData2 = Application.WorksheetFunction.CountA(ws.Range("E" & i & ":E" & i))
            CountData3 = Application.WorksheetFunction.CountA(ws.Range("F" & i & ":F" & i))
            CountData4 = Application.WorksheetFunction.CountA(ws.Range("G" & i & ":G" & i))
           
            ' Display the counts in the new row's corresponding cells
            ws.Cells(i + NewRow, 4).Value = CountData1
            ws.Cells(i + NewRow, 5).Value = CountData2
            ws.Cells(i + NewRow, 6).Value = CountData3
            ws.Cells(i + NewRow, 7).Value = CountData4
           
            ' Increment the new row counter
            NewRow = NewRow + 1
        End If
    Next i
End Sub



Here Is VBA Code Just Change the name of sheet4 to your sheet name
That does not work. Here are the results

1BobT
149N01
0.00​
0.00​
0.00​
0.00​
2BAS16
3DRR03
1.00​
1.00​
1.00​
1.00​
4ATB19
0.00​
0.00​
0.00​
0.00​
5AUM02
6BAY05
1.00​
1.00​
1.00​
1.00​
7BUI05
199.99​
8CAC03
1.00​
1.00​
1.00​
1.00​
9CAS21
0.00​
0.00​
0.00​
0.00​
10COM45
140.00​
11CONT07
1.00​
1.00​
1.00​
1.00​
0010
2BON07
1.00​
1.00​
1.00​
1.00​
115520
2CRE06
1.00​
1.00​
1.00​
1.00​
3CAP21
0.00​
1.00​
0.00​
0.00​
4CARL07
215.47​
5CHE11
1.00​
1.00​
1.00​
1.00​
3ChrisB
1CHI18
114.50​
24TH01
1.00​
1.00​
1.00​
1.00​
3AND12
0.00​
0.00​
0.00​
0.00​
415520
86.20​
819.20​
5ATC21
1.00​
1.00​
1.00​
1.00​
6CAN91
1095.00​
7CAO01
1.00​
1.00​
1.00​
1.00​
8CAR32
1.00​
0.00​
0.00​
0.00​
9CSC07
490.00​
490.00​
10COM44
1.00​
1.00​
1.00​
1.00​
11DOW04
568.97​
12ENB03
13FAL04
0.00​
0.00​
0.00​
0.00​
14INF33
0.00​
0.00​
0.00​
1.00​
15MAS13
515.52​
1740.35​
711.64​
769.28​
1111
 
Upvote 0
Is your data in Post #1 your actual starting point ? ie do you have 2 blank rows between the end of BobT and the start of BonR or does the macro need to insert them.
Is the first item "1" in row 2 of column A ?
Do you want the formulas in the final output or just the resulting values ?
 
Upvote 0
Is your data in Post #1 your actual starting point ? ie do you have 2 blank rows between the end of BobT and the start of BonR or does the macro need to insert them.
Is the first item "1" in row 2 of column A ?
Do you want the formulas in the final output or just the resulting values ?
This is the data. I have added the row and column numbers. There are 2 blank lines already between each employee. No blank lines should be added.. A1 is the employee. B2 is the start of the data. Col A is the employee number and it varies up to about 50
I just need the resulting value in col D to G which for employee #1 is 2 , 3,3 ,1. For employee 1 the total should be in row 13. For employee2 in row 21 and so on
ABCDEFG
11BobT
2149N01
32BAS16
43DRR03
150.00​
54ATB19
339.99​
65AUM02
76BAY05
87BUI05
199.99​
98CAC03
399.94​
639.90​
2339.96​
109CAS21
1110COM45
140.00​
1211CONT07
298.93​
119.77​
13
14
152BON07
16115520
172CRE06
439.00​
183CAP21
195.00​
194CARL07
215.47​
205CHE11
100.00​
21
22
233ChrisB
241CHI18
114.50​
2524TH01
2066.89​
1129.94​
149.70​
263AND12
27415520
86.20​
819.20​
285ATC21
296CAN91
1095.00​
307CAO01
259.56​
1691.00​
318CAR32
1316.93​
329CSC07
490.00​
490.00​
3310COM44
909.20​
573.54​
975.30​
3411DOW04
568.97​
3512ENB03
3613FAL04
3714INF33
303.24​
3815MAS13
515.52​
1740.35​
711.64​
769.28​
 
Upvote 0

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

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