royalmarine Posted May 16, 2013 Share Posted May 16, 2013 Hey guys, been trying to find if it's possible to find the next empty cell in Excel. I have a program that writes several rows of data, but I need it to find the next empty row to use. I can search via a single column, which will do just fine, as I can ensure that no data exists in column 1 or column A. Not sure if it's available in excel.au3 or if I would need to use water's alpha version. Any suggestions? Thanks! p.s. I didn't provide any code as I have none as of yet, since I can't find anything to base some code off :/ Link to comment Share on other sites More sharing options...
water Posted May 16, 2013 Share Posted May 16, 2013 Excel provides the information about the last used cells. I will provide an example tomorrow.. royalmarine 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted May 16, 2013 Moderators Share Posted May 16, 2013 (edited) I'm sure water can give you better using the excel object (I had that snippet but cannot find it), but for a quick and dirty you could do something like this: #include <Excel.au3> $oExcel = _ExcelBookOpen(@DesktopDir & "\Test.xls", 1) For $i = 1 To 10 $cell = _ExcelReadCell($oExcel, $i, 1) If $cell = "" Then _ExcelWriteCell($oExcel, "Found an empty cell", $i, 1) ExitLoop EndIf Next Edit: Found it. This will move the cursor to the first empty row in column A Const $xlCellTypeLastCell = 11 $oExcel = ObjCreate("Excel.Application") $oBook = $oExcel.Workbooks.Open("C:\Users\Hades\Desktop\Test.xls") $oExcel.Visible = True $oSheet = $oBook.Worksheets(1) $oSheet.Activate $oRange = $oSheet.UsedRange $oRange.SpecialCells($xlCellTypeLastCell).Activate $newRow = $oExcel.ActiveCell.Row + 1 $oExcel.Range("A" & $newRow).Activate Edited May 16, 2013 by JLogan3o13 royalmarine and SkysLastChance 2 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
royalmarine Posted May 19, 2013 Author Share Posted May 19, 2013 that for loop worked perfect for me. Thanks a mill guys Link to comment Share on other sites More sharing options...
water Posted May 19, 2013 Share Posted May 19, 2013 Glad you got it working My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted May 20, 2013 Moderators Share Posted May 20, 2013 Glad that did it for you. If everything is resolved, you can mark the topic as solved. "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
MrCheese Posted October 4, 2016 Share Posted October 4, 2016 Hey Guys, I had a question about this one, but the solution above didn't help me. I used an example in the range find function. #include <Array.au3> #include <Excel.au3> #include <MsgBoxConstants.au3> $test= "test" $cBookLocalName = @ScriptDir & "\" & $test & ".xlsx" Global $oWorkbook = 0 _Excel_BookClose($oWorkbook) Global $oExcel = _Excel_Open() $oWorkbook = _Excel_BookOpen($oExcel, $cBookLocalName) Local $aResult = _Excel_RangeFind($oWorkbook, "37000") If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeFind Example 1", "Error searching the range." & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeFind Example 1", "Find all occurrences of value '37000' (partial match)." & @CRLF & "Data successfully searched.") _ArrayDisplay($aResult, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") I wanted to find "37000" in Column A. But I get: "C:\Users\60080462\Documents\Stuff\macros\autoit-v3\Include\Excel.au3" (656) : ==> The requested action with this object has failed.: $aResult[$iIndex][1] = $oMatch.Name.Name $aResult[$iIndex][1] = $oMatch^ ERROR My excel file essentially has '1's in every cell from A1:C33, except for A27 which has 37000 (attached). The end goal is to determine the first row containing "" (i.e. is blank) in column A, and then feed that as a variable for use elsewhere in the script. As always, your help is appreciated! Thanks test.xlsx Link to comment Share on other sites More sharing options...
MrCheese Posted October 4, 2016 Share Posted October 4, 2016 OK: so this works, and gives me an array: #include <Array.au3> #include <Excel.au3> #include <MsgBoxConstants.au3> $test= "test" $cBookLocalName = @ScriptDir & "\" & $test & ".xlsx" Global $oWorkbook = 0 _Excel_BookClose($oWorkbook) Global $oExcel = _Excel_Open() $oWorkbook = _Excel_BookOpen($oExcel, $cBookLocalName) $lastline=_Excel_RangeRead ($oWorkbook,Default,Default,Default) _ArrayDisplay($lastline) Thanks to here: However, how do get the variable out as the next row that is blank? Thanks Link to comment Share on other sites More sharing options...
water Posted October 4, 2016 Share Posted October 4, 2016 (edited) The wiki describes how to retrieve the value of some special cells: https://www.autoitscript.com/wiki/Excel_Range Edited November 1, 2016 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
water Posted October 4, 2016 Share Posted October 4, 2016 1 hour ago, MrCheese said: "C:\Users\60080462\Documents\Stuff\macros\autoit-v3\Include\Excel.au3" (656) : ==> The requested action with this object has failed.: $aResult[$iIndex][1] = $oMatch.Name.Name $aResult[$iIndex][1] = $oMatch^ ERROR That's caused by a bug in the implmentation of the COM error handling in the latest version of AutoIt. The solution is described here: My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
MrCheese Posted November 1, 2016 Share Posted November 1, 2016 I ended up using: $oExcel.ActiveSheet.UsedRange.Rows.Count to give me the last row used. Link to comment Share on other sites More sharing options...
water Posted November 1, 2016 Share Posted November 1, 2016 But be careful: This is only true when the used range starts with row 1 - as described in the wiki. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
MrCheese Posted November 1, 2016 Share Posted November 1, 2016 yeah they usually will. One thing though, do you know how to determine the usedrange on just a particular column. Say, I have data A1:A50 and as the script roles, it puts a comment in column B. But say at B20 it fails. and I want the script to determine where it failed by determining the last row used in column B. Thoughts? Link to comment Share on other sites More sharing options...
water Posted November 1, 2016 Share Posted November 1, 2016 Why should it "fail" at B20? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki 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