Forestq

Active Member
Joined
May 9, 2010
Messages
482
hi,

I have SAP file and I`m counting number of row in one order, example:
A1
1
1
B1
1
C1
1
1

<tbody>
</tbody>

I have code:

Code:
                For i = 2 To lastrow
                            If SAP_WS.Range("A" & i).Value <> "" Then
                            row_wynik = row_wynik + 1
                            ws.Cells(row_wynik, 1).Value = SAP_WS.Cells(i, 1)
                            ws.Cells(row_wynik, 2).Value = SAP_WS.Cells(i, 2)
                            
                        End If                
                Next i

How to count row number for A, B and C?

Result should be:
A=3
B=2
C=3


Please help
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
It appears from your post that you have an empty row above each entry in column A, except for the first entry in A2. Based on that layout, here's some code you can adapt to your needs that counts the rows for A,B,C, ...
Code:
Sub Counts()
'Assumes there is an empty row above each entry in column A except for the first entry in A2
Dim lastrow As Long, SAP_WS As Worksheet
Set SAP_WS = ActiveSheet
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow
    If SAP_WS.Range("A" & i).Value <> "" Then
        If SAP_WS.Range("A" & i).End(xlDown).Row < Rows.Count Then
            MsgBox "Count for " & SAP_WS.Range("A" & i).Value & " is " & _
                Range(Cells(i, "A"), Cells(i, "A").End(xlDown)).Rows.Count - 2
        Else
            MsgBox "Count for " & SAP_WS.Range("A" & i).Value & " is " & Range(Cells(i, "A"), Cells(Rows.Count, "B").End(xlUp)).Rows.Count
        End If
    End If
Next i
End Sub
 
Upvote 0
this formula should work too, if you have A, B, C in D1:D3, you would type starting in E1


=IF(D2="",COUNTA(INDEX($B$1:$B$10,MATCH(2,$B$1:$B$10)):INDEX($B$1:$B$10,MATCH($D1,$A$1:$A$10,0))),COUNTA(INDEX($B$1:$B$10,MATCH($D2,$A$1:$A$10,0)-1):INDEX($B$1:$B$10,MATCH($D1,$A$1:$A$10,0))))
 
Upvote 0
Hi JoeMo

I have value as below:
A1
1
1
B1
1
C1
D1
1
E1
1

<tbody>
</tbody>

after I run your code (I change a little) I still got wrong number:
sum A= 3
sum B= 2
sum C= 4
sum D= 3
sum E= 2

my full code
Code:
Sub Counts1()
'Assumes there is an empty row above each entry in column A except for the first entry in A2

Application.DisplayAlerts = False
Application.ScreenUpdating = False


Dim ws As Worksheet
Dim wb As Workbook


Set wb = ActiveWorkbook
Set ws = wb.Worksheets("WYNIK")

ws.Activate
Columns("A:B").Select
Selection.Delete Shift:=xlToLeft
Range("A1").Select

Dim FileToOpen As String

    FileToOpen = OpenFile("Open SAP File", "*.xls", ThisWorkbook.Path & "")
         
        If FileToOpen <> "" Then
         
            Dim SAP_WB As Workbook
            Dim SAP_WS As Worksheet
            
            Set SAP_WB = Workbooks.Open(FileToOpen)
            Set SAP_WS = SAP_WB.Worksheets(1)
                                
            SAP_WS.Activate
            Columns("A:L").Select
            Selection.Delete Shift:=xlToLeft
            Columns("B:H").Select
            Selection.Delete Shift:=xlToLeft
            Columns("B:F").Select
            Selection.Delete Shift:=xlToLeft
            Columns("C:C").Select
            Selection.Delete Shift:=xlToLeft
            Range("B2").Select
            Selection.Delete Shift:=xlUp
            Range("A1").Select
            Selection.EntireRow.Delete
            Range("A1:A3").Select
            Selection.EntireRow.Delete
            Range("A1").Select
                    
Dim lastrow As Long
Set SAP_WS = ActiveSheet
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastrow
    If SAP_WS.Range("A" & i).Value <> "" Then
        If SAP_WS.Range("A" & i).End(xlDown).Row < Rows.Count Then
        '    MsgBox "Count for " & SAP_WS.Range("A" & i).Value & " is " & _
         '       Range(Cells(i, "A"), Cells(i, "A").End(xlDown)).Rows.Count '- 1
        
            MsgBox "Count for " & SAP_WS.Range("A" & i).Value & " is " & _
                Range(Cells(i, "B"), Cells(i, "B").End(xlDown)).Rows.Count '- 1
           
        Else
            MsgBox "Count for " & SAP_WS.Range("A" & i).Value & " is " & Range(Cells(i, "A"), Cells(Rows.Count, "B").End(xlUp)).Rows.Count
        End If
    End If
Next i

End If

End Sub
 
Upvote 0
my working code:

Code:
                For i = 1 To lastrow
                        
                        If SAP_WS.Range("A" & i).Value <> "" Then
                            If SAP_WS.Range("A" & i).Value <> "" And SAP_WS.Range("A" & i).Offset(1, 1).Value <> "" Then
                                    
                                    count_qty = Range(Cells(i, "A"), Cells(i, "B").End(xlDown)).Rows.Count
                                    
                                    row_wynik = row_wynik + 1
                                    ws.Cells(row_wynik, 1).Value = SAP_WS.Cells(i, 1)
                                    ws.Cells(row_wynik, 2).Value = SAP_WS.Cells(i, 2)
                                    ws.Cells(row_wynik, 3).Value = count_qty
                            Else
                                    row_wynik = row_wynik + 1
                                    ws.Cells(row_wynik, 1).Value = SAP_WS.Cells(i, 1)
                                    ws.Cells(row_wynik, 2).Value = SAP_WS.Cells(i, 2)
                                    ws.Cells(row_wynik, 3).Value = "1"
                                    
                            End If
                        End If                
                Next i
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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