CE101 Posted October 12, 2010 Share Posted October 12, 2010 COUNTIF is an Excel worksheet function that counts the number of cells within a range that meet the given criteria. Syntax: COUNTIF(range,criteria) Example: COUNTIF(A2:A5,"apples") I would like to use this function in my script. Googling around I've seen a lot of VB examples... http://www.eggheadcafe.com/software/aspnet/30447507/count-if-range.aspx Application.CountIf(Range("J3:J600"), "35") http://forums.devshed.com/visual-basic-programming-52/countbetween-function-165810.html Application.CountIf(Sheets("sheet2").Range("f2:f500"), "=16") http://www.vbforums.com/showthread.php?t=618030 Application.CountIf(Columns("D:D"), date1) www.vbaexpress.com/forum/archive/index.php/t-8121.html Application.CountIf(Range("a1:a20"),"=5") However I can’t seem to get the syntax correct for AutoIT. Here's what I've tried... expandcollapse popup#include <Debug.au3> ; included at top of pgm _DebugSetup ("" & ": Debugger") #include <Excel.au3> Local $oExcel = _ExcelBookNew() Local $aArrayB[6] = ["", "New York", "Los Angeles", "Chicago", "Boston", "Atlanta"] For $i = 1 to 10 For $j = 1 to 5 $var1 = Random (1, 5, 1) $var2 = $aArrayB[$var1] _ExcelWriteCell($oExcel, $var2, $i , $j) Next Next $var3 = 99 ; Count the number of occurences of "Atlanta" ; On each CountIf, $var3 gets set to 0... Why?? $var3 = $oExcel.Application.CountIf("A:E", "Atlanta") ; no error msg, but returns 0 _DebugReportVar("Test 1: #recs found ... $var3", $var3) $var3 = $oExcel.Application.CountIf("A1:E10", "Atlanta") ; no error msg, but returns 0 _DebugReportVar("Test 2: #recs found ... $var3", $var3) $var3 = $oExcel.Application.CountIf("Range(A1:E10)", "Atlanta") ; no error msg, but returns 0 _DebugReportVar("Test 3: #recs found ... $var3", $var3) $var3 = $oExcel.Application.CountIf("Sheets(Sheet1).Range(A1:E10)", "Atlanta") ; no error msg, but returns 0 _DebugReportVar("Test 4: #recs found ... $var3", $var3) ; Even if garbage is entered into argument #1 (range), there is no error msg $var3 = $oExcel.Application.CountIf("Range(junk???)", "Atlanta") ; no error msg, but returns 0 _DebugReportVar("Test 5: #recs found ... $var3", $var3) ; Countif needs 2 arguments. If you have only one argument or you add an extra one, it fails $var3 = $oExcel.Application.CountIf("Range(junk???)") ; error, "requested action with this object has failed" $var3 = $oExcel.Application.CountIf("Range(junk???)", "Atlanta", "junk????") ; error, "requested action with this object has failed" As you can see from the examples I've tried, Excel definitely recognizes it as a CountIf. That's why it objects if the correct number of arguments are not present. So the question is -- why doesn’t it give the correct results when the correct number of arguments are not present. Any suggestions would be greatly appreciated. Sample code would be even better. Link to comment Share on other sites More sharing options...
water Posted October 12, 2010 Share Posted October 12, 2010 I think you have to write the formula into a cell. I use it this way: $oExcel.Activesheet.Range("B1").Formula = '=COUNTIF(C1:IV1;"X")' Cell B1 then contains the number of cells in the range from C1 to IV1 that contain an "X". My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
JoHanatCent Posted October 12, 2010 Share Posted October 12, 2010 Maybe if you specify the Excel range? #include <Debug.au3> ; included at top of pgm _DebugSetup("" & ": Debugger") #include <Excel.au3> Local $oExcel = _ExcelBookNew() $oExcel.Sheets("Sheet1").Select ; ===> Just to select "sheet1" Local $aArrayB[6] = ["", "New York", "Los Angeles", "Chicago", "Boston", "Atlanta"] For $i = 1 To 10 For $j = 1 To 5 $var1 = Random(1, 5, 1) $var2 = $aArrayB[$var1] _ExcelWriteCell($oExcel, $var2, $i, $j) Next Next $var3 = 99 ;=== Maybe Change this into an Excel Range : $var3 = $oExcel.Application.CountIf($oExcel.range("A:E"), "Atlanta") ;=== End of Change ; } _DebugReportVar("Test 1: #recs found ... $var3", $var3) MsgBox(64, "Result:", $var3, 3) ; === Can also write the result to the sheet: $oExcel.Range("G5") = $oExcel.Application.WorksheetFunction.CountIf($oExcel.Range("A:E"), "Atlanta") ; === End of writing to sheet Link to comment Share on other sites More sharing options...
GMK Posted October 12, 2010 Share Posted October 12, 2010 For example, if you wanted to write the formula in cell F1, use: _ExcelWriteFormula($oExcel, '=COUNTIF(A1:E10, "Atlanta")', 'F1') If you don't need the actual formula in the worksheet, just write the formula temporarily to get the value from the cell, then delete the formula later. Link to comment Share on other sites More sharing options...
CE101 Posted October 13, 2010 Author Share Posted October 13, 2010 Thank you all for your suggestions. Problem solved. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now