The simplest way to copy a range from one location to another is to use the copy range command, which is the equivalent of the Copy method in Visual Basic for Applications (VBA):
With ActiveSheet
.Range("A1:J10").Copy Destination:=.Range("M1")
End WithIn AppleScript, this becomes:
tell application "Microsoft Excel" copy range range "A1:J10" destination range "M1" end tell
There is a common repetition of keywords (for example, range) in commands, direct objects, and parameters, so that the same word is repeated twice in the syntax. Don't leave any out. You'll see that the shorthand of specifying just the top-left initial cell for the destination range is sufficient in AppleScript as well as in VBA.
Remember also that
of active sheet
is taken for granted as the default, just as in VBA.
Note that the copy range command copies values, formatting, conditional formatting, validation, etc.
You can paste the contents of the clipboard into the active cell using the paste worksheet command (in the Microsoft Excel Suite this time), which is the equivalent of the VBA Paste method into a particular cell. Or, you can use the optional destination argument.
ActiveWorkbook.Sheets("Sheet1").Range("A1:J10").Copy
With Workbooks("Book2.xls").Sheets("Sheet1")
.Paste Destination:=.Range("J3")
End WithIn AppleScript, this becomes:
tell application "Microsoft Excel" copy range (range "A1:J10" of sheet "Sheet1" of active workbook) tell sheet "Sheet1" of workbook "Book2.xls" paste worksheet destination range "J3" end tell end tell
If you're only trying to copy values, it's far more efficient and quicker to simply assign them, rather than calling the copy range command (Copy method in VBA):
With Sheets("Sheet1").Range("A1:J10")
Sheets("Sheet2").Range("B10").Resize(_
.Rows.Count, .Columns.Count).Value = .Value
End WithIn AppleScript, this becomes:
tell application "Microsoft Excel" tell (range "A1:J10" of sheet "Sheet1") set value of (get resize (range "B10" of sheet ¬ "Sheet2" of active workbook) row size (count rows) ¬ column size (count columns)) ¬ to get its value end tell end tell
In this case, it is necessary to specify
of active workbook
for
Sheet2
because it is in a
tell
block targeted at a range in
Sheet1, which is a different sheet. In AppleScript, if you don't specify
of active workbook, it looks for
Sheet2
inside its own
range A1:J10, and when it doesn't find a sheet there, it doesn't do anything.
Similarly, you need the explicit get for
its
value
to force it to be evaluated in the same command. If you set a variable to it in a previous line, then the get is not necessary. Actually, Microsoft Excel
seems not nearly so insistent on the explicit get as Microsoft Word, but in this case it is necessary. The
its
may not be essential this time, but it helps keep straight whose value (the original range's) you're talking to. And frequently,
it
is necessary in
tell
blocks.
Paste special
The paste special command in the Table Suite (there is a different paste special on worksheet command in the Excel Suite which is not concerned with ranges) corresponds to the PasteSpecial method in VBA, and allows you to perform operations on the destination range based on the contents of the clipboard (for example, add, subtract, multiply, and divide).
For example, to multiply the value in each cell of a range by
2:
Application.ScreenUpdating = False
' Get the value in the clipboard using a
' cell in a temporary worksheet
With ActiveWorkbook.Worksheets.Add
With .Cells(1, 1)
.Value = 2
.Copy
End With
End With
Sheets("Sheet1").Range("A1:J10").PasteSpecial_
Paste:=xlPasteAll, _
Operation:=xlPasteSpecialOperationMultiply
' Delete the temporary worksheet
With Application
.DisplayAlerts = False
ActiveSheet.Delete
.DisplayAlerts = True
.ScreenUpdating = True
End WithHere it is in AppleScript:
tell application "Microsoft Excel" set screen updating to false -- speed it up --get the value in the clipboard to a cell --in a temporary worksheet tell (make new worksheet at active workbook) tell cell 1 of row 1 set value to 2 copy range -- to clipboard end tell end tell paste special (range "A1:J10" of sheet "Sheet1") ¬ what paste all operation paste special operation ¬ multiply --delete the temporary worksheet set display alerts to false delete active sheet set display alerts to true set screen updating to true end tell
Take a look at the dictionary for paste special and the various enumerations of its parameters. Occasionally, the names of keywords vary from the VBA ones, such as what rather than Paste. This avoids terminology conflicts in AppleScript where keywords have a broad scope:
paste
is a very common term in scripting additions.
Transpose a range
You can also use the paste special command to transpose columns and rows. Note that if the destination range has more than one cell, it must have the same number of rows and columns as the source range has columns and rows:
With ActiveSheet
.Range("A1:E2").Copy
.Range("A4:B8").PasteSpecial
Transpose:=True
End WithIn AppleScript, this becomes:
tell application "Microsoft Excel" copy range range "A1:E2" paste special range "A4:B8" with transpose end tell
Note that the paste special command will not allow you to transpose the range in place, even if you're using a square range. Also, recall that compiling transpose true compiles to with transpose.


