Excel Range: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
Line 5: Line 5:


== Current Region ==
== Current Region ==
<syntaxhighlight lang="autoit">$oRange = $oWorkBook.Activesheet.Range("B4").CurrentRegion</syntaxhighlight>
The current region is a range bounded by any combination of blank rows and blank columns.<br />
The current region is a range bounded by any combination of blank rows and blank columns.<br />
[[File:Range_CurrentRegion.jpg]]<br />
Note that cell B6 is not part of the region. The cell has been touched before but now is blank and hence bounds the region.
Note that cell B6 is not part of the region. The cell has been touched before but now is blank and hence bounds the region.
<syntaxhighlight lang="autoit">$oRange = $oWorkBook.Activesheet.Range("B4").CurrentRegion</syntaxhighlight>
[[File:Range_CurrentRegion.jpg]]


== Empty Cells ==
== Empty Cells ==

Revision as of 13:11, 26 April 2016

On this page you will find some special ranges and how to define them in AutoIt.
As input file I will use the following Excel workbook. All cells which have a value now or had a value before (means: all cells which had been touched by the user or a script) are displayed in grey.

All examples show cells of the selected range in yellow.

Current Region

The current region is a range bounded by any combination of blank rows and blank columns.
Note that cell B6 is not part of the region. The cell has been touched before but now is blank and hence bounds the region.

$oRange = $oWorkBook.Activesheet.Range("B4").CurrentRegion

Empty Cells

$oRange = $oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeBlanks)

Marks all empty cells in the specified range.

Entire Row / Entire Column

$oRange = $oWorkBook.Activesheet.Range("B2").EntireRow

Selects the entire row(s) of the specified range.

Use method "EntireColumn" to select the columns of the specified range.

Last Cell

$oRange = $oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeLastCell)

Returns the last used cell (bottom right) in the used range.


External Link: Excel Ranges: Finding the Last Cell in a Range

Local Const $xlUp = -4162
With $oWorkbook.ActiveSheet ; process active sheet
	$oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) ; get a Range that contains the last used cells
	$iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count ; get the the row count for the range starting in row/column 1 and ending at the last used row/column
	$iLastCell = .Cells($iRowCount + 1, "B").End($xlUp).Row ; start in the row following the last used row and move up to the first used cell in column "B" and grab this row number
	ConsoleWrite("Last used cell in column B: " & $iLastCell & @CRLF)
EndWith

Returns the last used cell in column "B".


NB: If the script moved up to row 1 you need to check if the cell is really the last used cell or just the top of the range.

Used Range

$oRange = $oWorkBook.Activesheet.UsedRange

Returns a Range including all cells which have a value now or had a value before (means: all cells which had been touched by the user or a script).
Note that the range does not contain row 1 and column 1!


$sUpperLeftCell = $oWorkbook.ActiveSheet.UsedRange.Cells(1, 1).Address

Returns the address of the upper left cell of the used range. In this examle: B2

With $oWorkbook.ActiveSheet
	$oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell)
	$oRange = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column))
EndWith

This lines extend the used range to start with cell A1.