Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Loretta Carpenter   on Oct 31 In MS Office Category.

  
Question Answered By: Abasi Massri   on Oct 31

You've found where the recorder  really doesn't help  much, in
programmatically locating and selecting variable locations of cells.
The recorder provides the absolute  selection statements, which aren't
very useful in the long run.

The first thing to do is review Help on "Range" and Cells, two very
useful keywords to know and love.

Now to discuss the specifics of your questions...

> Newbie here stumped on a supposedly easy  problem. I am trying to
> record a macro  that would select  the cell  to the left  of the last
> row, last column  with data. After that highlight  from that point
> upto the first cell in that column.
>
> The shortcut  keys sequence  I press  when doing this manually  are:
>
> 1. <ctrl><end> 'gets me to the last row, last column with data'

You've got this part already with the ActiveCell statement.

Now you need to grab and save those coordinates to make use of them in
the next part...

First, add these at the start of your subroutine to keep the
coordinates in:
Dim iRow as Integer
Dim iCol as Integer

Now, after your line  from (1.) you need to capture the coordinates of
that cell with:
ActiveCell.SpecialCells(xlLastCell).Select
iRow = ActiveCell.Row
iCol = ActiveCell.Column

With the information you now have with the statements above, you can
form a Range statement using the Cells keyword that completes the next
two parts ...

> 2. <left> 'selects cell to the left of above'

Selecting the cell to the LEFT of the current cell means using the
SAME row, and one less than the current column, gives the statement:
Cells(iRow,iCol - 1)

> 3. <ctrl><shift><up> 'highlights from #2 to the first cell'

The location of the top of the column is in row  # 1, or whatever row
is really the top of your data. If you have a row of labels as a
header, the row is 2, etc.

So that location in the Cell format is:
Cells(1,iCol - 1)

Now, putting that together into a range, you need the Range statement
selecting from either the bottom up to the top or the top down. I
prefer top to bottom so the Range statement becomes:

Range(Cells(1,iCol - 1),Cells(iRow,iCol - 1)).Select

Now we have that whole column selected, so your sub looks like this:

Sub SelectColumn()
Dim iRow as Integer
Dim iCol as Integer
ActiveCell.SpecialCells(xlLastCell).Select
iRow = ActiveCell.Row
iCol = ActiveCell.Column
Range(Cells(1,iCol - 1),Cells(iRow,iCol - 1)).Select
End Sub

Share: 

 

This Question has 6 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on keypress shortcuts Or get search suggestion and latest updates.


Tagged: