Counbt Formula

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I would like a formula to count the number of units as follows based on the type in Col H

0-7
8-30
31-60
60 days plus

Your assistance in this regard is most appreciated

Excel Workbook
CDEFGHI
3KNB11102HONDA JAZZ 1.5 EXZBD972KLZJHPXGE8880AS20016947441501U15
4KNB11134FORD Figo 1.4 Sigma AmbienteCJ20CKKLZPXAJ1LYPXRJ1CT5944347441515N30
5KNB11227RENAULT LOGAN LOGAN 1.6 EXPRESSIONZWN573KLZPXA1LSRADDAZD1250847441516N45
6KNB11117MERCEDES-BENZ Mercedes CRZS062KLZWDC2030422R18583247441530U50
7KNB11118MERCEDES-BENZ Mercedes CRZS062KLZWDC2030422R18583347441531U51
8KNB11119MERCEDES-BENZ Mercedes CRZS062KLZWDC2030422R18583447441532N52
9KNB11120MERCEDES-BENZ Mercedes CRZS062KLZWDC2030422R18583547441533U53
10KNB11121MERCEDES-BENZ Mercedes CRZS062KLZWDC2030422R18583647441534U54
11
Sheet1
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Can you give us an idea of what results you expect on the sample data ?
 
Upvote 0
Hi Michael


Thanks for the reply

0-7 0
8-30 1
31-60 6
61+ 0
 
Upvote 0
Ok, you have 8 line items and you come up with 7 results ???
Are you counting based on the numbers in Col I and the letters in Col H ???



I think I need a drink.....:confused:
 
Upvote 0
add header row to data ( name the columns anything you like )

i chose column "I" to be named "num"

you can change to anything you like, you just have to make the namesame in the mySqlQuery string

Code:
Sub doSQL()

    Dim i As Integer
    
    Dim strCon As String
    Dim mySqlQuery As String
    
    ' refer to 'microsoft activex data objects library'
    Dim cn As Object
    Dim rs As Object
    
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")


    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source='" & ThisWorkbook.FullName & "';" & _
             "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';"


    cn.Open strCon
    
    mySqlQuery = "SELECT 0, count(1) FROM [Sheet1$A1:G10] WHERE  0 <= [B]num[/B] and [B]num[/B] <  8 UNION " & _
                 "SELECT 1, count(1) FROM [Sheet1$A1:G10] WHERE  7 <  [B]num[/B] and [B]num[/B] < 31 UNION " & _
                 "SELECT 2, count(1) FROM [Sheet1$A1:G10] WHERE 30 <  [B]num[/B] and [B]num[/B] < 61 UNION " & _
                 "SELECT 3, count(1) FROM [Sheet1$A1:G10] WHERE 60 <  [B]num[/B]              ORDER BY 1"
    
    rs.Open mySqlQuery, cn
        
    Sheets("Sheet1").Range("K2:N2").CopyFromRecordset rs
    
    rs.Close
    cn.Close
    
    Set rs = Nothing
    Set cn = Nothing


End Sub
 
Upvote 0
Hi Michael

Range 8-30 should be 2 NOT 1

Count on Col H

Regards

Howard
 
Upvote 0
you are welcome

i made an error in the sql command

FROM [Sheet1$A1:G10] should be FROM [Sheet1$C2:I11] in all four lines

i copied your data to "a1" instead of "c3" when i was testing

on your worksheet, row 2 should contain column names, therefore the table range is C2:I11

js:)
 
Upvote 0
you could also run four separate sql queries

Code:
    mySQLquery = "SELECT count(1) FROM [Sheet2$A1:G10] WHERE  0 <=  num and num < 8 "
    rs.Open mySQLquery, cn
    Debug.Print rs.Fields(0)
    rs.Close
    
    mySQLquery = "SELECT count(1) FROM [Sheet2$A1:G10] WHERE  7 <  num and num < 31 "
    rs.Open mySQLquery, cn
    Debug.Print rs.Fields(0)
    rs.Close
    
    mySQLquery = "SELECT count(1) FROM [Sheet2$A1:G10] WHERE  30 <  num and num < 61 "
    rs.Open mySQLquery, cn
    Debug.Print rs.Fields(0)
    rs.Close
    
    mySQLquery = "SELECT count(1) FROM [Sheet2$A1:G10] WHERE  60 <  num "
    rs.Open mySQLquery, cn
    Debug.Print rs.Fields(0)
    rs.Close

or use a function

Code:
Function countRange(cn As Object, w As String) As Integer
    
    Dim rs As Object
    Set rs = CreateObject("ADODB.Recordset")


    mySQLquery = "SELECT count(1) FROM [Sheet2$A1:G10] WHERE " & w
    rs.Open mySQLquery, cn
    countRange = rs.Fields(0)
    rs.Close


End Function

and call this way

Code:
    Debug.Print countRange(cn, "7 <  num and num < 31")

          abc = countRange(cn, "7 <  num and num < 31")

    sheets("Sheet1").Range("K4") = countRange(cn, "7 <  num and num < 31")

if you made the connection and recordset (cn & rs) objects global
then you would not have to pass then to the function


you could also pass only the hi and lo digits to the function and assemble the WHERE clause within the function

js:)
 
Upvote 0
i did a bit more research

if you do not have, or don't want column headers then change connection string HDR=No

Code:
    strCon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
             "Data Source='" & ThisWorkbook.FullName & "';" & _
             "Extended Properties='Excel 12.0;HDR=No;IMEX=1';"

the SQL engine assigns column names F1, F2, F3 ......

the query string becomes
Code:
mySQLquery = "SELECT count(1) FROM [Sheet2$A1:G10] WHERE  0 <=  F7 and F7 < 8 "
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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