cnilsson76 Posted January 31, 2022 Share Posted January 31, 2022 (edited) I have been successfully finding the last used row of Excel sheets with the following line. $oRange = $oWorkbook.Sheets("Raw Data").UsedRange.SpecialCells($xlCellTypeLastCell) This works perfectly if there is no formatting in the sheet. However, if there is an empty cell with any formatting (highlighting, borders, defined number format, etc.) then that is the returned range. This is the correct behavior of the .UsedRange property and is expected. However, I need to find the last row containing data without regard to any formatting. The sheet I am searching has a defined Excel table object covering A:K for the Max length Excel will allow (=$A$2:$K$1048471). This means that all the cells in the table have been "touched" by a user/script and thus the Used.Range function does not return the last row with actual data. I found this post on stackoverflow which includes a great answer for VBA users. In has a section for "Find Last Row in a Sheet" With Sheets("Sheet1") If Application.WorksheetFunction.CountA(.Cells) <> 0 Then lastrow = .Cells.Find(What:="*", _ After:=.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row Else lastrow = 1 End If End With I have tried to use the _Excel_RangeFind function with the values for LookAt, LookIn, etc. from the VBA above plugged in but every permutation generates an error. Just one failed example is below. Local $oRange = _Excel_RangeFind($oWorkbook.Sheets("Raw Data"), "*", .Range("A1")) The stackoverflow post also has a section called "Find Last Row in a Table (ListObject)" which is exactly what I need to do, however, I have no idea how to translate this into AutoIT. Sub FindLastRowInExcelTableColAandB() Dim lastRow As Long Dim ws As Worksheet, tbl as ListObject Set ws = Sheets("Sheet1") 'Modify as needed 'Assuming the name of the table is "Table1", modify as needed Set tbl = ws.ListObjects("Table1") With tbl.ListColumns(3).Range lastrow = .Find(What:="*", _ After:=.Cells(1), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row End With End Sub I have also seen methods that talk about reading up from the bottom until you reach some data but I'm concerned about performance with the table range being so large. Are there any sure fire ways to find the last used row (used meaning not blank) in an Excel table while ignoring all formatting? Edited January 31, 2022 by cnilsson76 Link to comment Share on other sites More sharing options...
Solution water Posted January 31, 2022 Solution Share Posted January 31, 2022 Maybe something like this? #include <Excel.au3> ; Pass the following parameters: ; * Object of the Workbook ; * Number or Name of the Worksheet ; * Name or Index number of the table ; * Name or Index number of the Column Func FindLastRowInExcelTable($oWorkbook, $vWorksheet, $sTable, $iColumn) Local $xlByRows = 1 ; Searches across a row, then moves to the next row. Local $xlPrevious = 2 ; Search for previous matching value in range. Local $iLastRow, $oWorksheet, $oTable $oWorksheet = $oWorkbook.Sheets($vWorksheet) $oTable = $oWorksheet.ListObjects($sTable) With $oTable.ListColumns($iColumn).Range $iLastRow = .Find("*", .Cells(1), $xlFormulas, $xlPart, $xlByRows, $xlPrevious, False).Row EndWith EndFunc ;==>FindLastRowInExcelTable Zedna and robertocm 2 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 January 31, 2022 Share Posted January 31, 2022 BTW: More information can be found in the wiki: Excel Range - AutoIt Wiki (autoitscript.com) 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...
cnilsson76 Posted February 2, 2022 Author Share Posted February 2, 2022 On 1/31/2022 at 2:40 AM, water said: Maybe something like this? #include <Excel.au3> ; Pass the following parameters: ; * Object of the Workbook ; * Number or Name of the Worksheet ; * Name or Index number of the table ; * Name or Index number of the Column Func FindLastRowInExcelTable($oWorkbook, $vWorksheet, $sTable, $iColumn) Local $xlByRows = 1 ; Searches across a row, then moves to the next row. Local $xlPrevious = 2 ; Search for previous matching value in range. Local $iLastRow, $oWorksheet, $oTable $oWorksheet = $oWorkbook.Sheets($vWorksheet) $oTable = $oWorksheet.ListObjects($sTable) With $oTable.ListColumns($iColumn).Range $iLastRow = .Find("*", .Cells(1), $xlFormulas, $xlPart, $xlByRows, $xlPrevious, False).Row EndWith EndFunc ;==>FindLastRowInExcelTable This is perfect! The syntax for how to reference a named table and column was the missing part for me. $oTable = $oWorksheet.ListObjects($sTable) With $oTable.ListColumns($iColumn).Range $iLastRow = .Find("*", .Cells(1), $xlFormulas, $xlPart, $xlByRows, $xlPrevious, False).Row EndWith I had poured over the docs but was struggling to merge Excels dot notation with AutoIT. Thank you for your help! Link to comment Share on other sites More sharing options...
water Posted February 2, 2022 Share Posted February 2, 2022 😃 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