Vous êtes sur la page 1sur 8

1

Anytime we want the VBA code to do


something and are not sure where to start, use
the Record Macro in Excel.
Gives an initial map of the coding that is required

Page 1 of 8
To specify in VBA where the cursor is, use:
ActiveCell
Followed by the dot operator and then the
method that we want to use, such as:
ActiveCell.Copy
If we want to specify a starting location that is
not where the cursor is, then:
Range("C10").Select To select that cell
Range("C10").Copy To copy that cell
3

While working with many workbooks open at


the same time, to minimize errors, use
ThisWorkbook to indicate to the macro which
workbook it should be working in
ThisWorkbook refers to the workbook where the
procedure resides

Page 2 of 8
To select a one-cell range, add Select,

To select a range that is more than one cell,


use Resize,

5
ActiveCell.Resize(1,1).Select ActiveCell. Select

Page 3 of 8
Within the same worksheet

To another worksheet

Selecting a range from another location


Selecting the Range G11:J14 in Sheet2, from Cell
A1 in Sheet2

Page 4 of 8
Selecting a range from another location
Selecting three whole columns D, E, F from the
starting cell of Cell A1

To get to Column D, we need an Offset first. The row


argument is not important and can be left at 0.
In Resize, the column argument of 3 is what defines the
number of columns that EntireColumn will act on. The
row argument is not important and can be left at 1

To find the location of a cell.


Use the Cells.Find command.
get the right syntax by recording a macro from Excel. For
example, here is a recorded macro for find the Cell that
contains the word Profit":

10

Page 5 of 8
To get the address of the location of the cell we
were looking for, change the Activate to
Address
Also declare a variable to hold the address value,

11

To get the row number of the location of the cell


we were looking for, change the Activate to
Row
Also declare a variable to hold the address value,

12

Page 6 of 8
Every worksheet is allocated memory based on
its contents
If we have used no more than a 10 row by 10
column area for our work, and have not touched
any of the other cells, the "last cell" in that
worksheet would be Cell J10
We can locate this and also determine the VBA macro
for it by recording the macro as we press Ctrl+End

13

Use the GoTo keyword


The argument of True for the Scroll is what makes
the selected cell be located at the top left corner

14

Page 7 of 8
To duplicate the action of selecting multiple
sheets in Excel, i.e., the equivalent of clicking
on individual tabs while pressing on the Shift
key.
eg. Selecting the sheets between Worksheet(1),
the leftmost tab, and Worksheet(5), the fifth sheet
from the left.

15

16

Page 8 of 8

Vous aimerez peut-être aussi