One frequent need is to find the last used cell in a range (for example, to start a new record). One way is to use the End method, as follows:
Dim rNext As Range
With Sheets("Sheet1")
If IsEmpty(.Range("A1").Value) Then
Set rNext = .Range("A1")
Else
Set rNext = .Range("A" & .Range("A" & _
.Rows.Count).End(xlUp).Row).Offset(1, 0)
End If
End WithThe nested dots and parentheses are difficult to keep straight when translating to AppleScript. The following is the direct AppleScript translation:
tell application "Microsoft Excel"
tell sheet "Sheet1" of active workbook
if value of range "A1" = "" then
set rNext to range "A1"
else
set rNext to get offset of range ("A" & ¬
(first row index of (get end range ¬
("A" & (count rows)) ¬
direction toward the top))) ¬
row offset 1
end if
end tell
end tellIt is preferable to use just a few
ofs
at a time, using intermediate variables along the way, at least until you become very familiar with constructions you run into regularly. You can use something like this:
set lastRowCellA to (get end range ¬
("A" & (count rows)) direction toward the top)
set lastRowNum to (first row index of lastRowCellA)
set lastRowCellAA to range ("A" & lastRowNum)
set rNext to get offset of lastRowCellAA row offset 1In fact, by doing this clarifying extraction, you can see that there are some redundant and circular steps. Once you have
lastRowCellA
in the first line, you can do the get offset straight away without needing to get the row index and put that back into the address.
set lastRowCellA to (get end range ¬
("A" & (count rows)) direction toward the top)
set rNext to get offset of lastRowCellA row offset 1This does exactly the same thing. That's almost simple enough to take the variables out again. But breaking up the code and using self-commenting variable names, if it starts to get too complicated, is usually a good idea. It will help you see things more clearly. The Visual Basic for Applications (VBA) code was in fact originally written to grab a larger range, not a single cell. In that case the extra steps are necessary.
Just remember to never set a variable to a (named) column, row, or cell — just to a range. It can be as simple as replacing the word
column
by
range, using the
A1-style address.


