Selection in a range.

M

ML

Hi

I am extremely new to Excel programming (this morning) and have had some
macro designs thrust upon me.

I am trying to select rows 1 & 2 of a previous selection (to effectively
format the header rows in table).

I have made it work where the table is 3 columns using:

Selection.Range("A1:C2").Select

but I need to make it work for tables more than 3 colums, the tables could
be any size and anywhere on a worksheet. So I tried:

Selection.Range("1:2").Select

but this didn't work.

Any suggestions?
 
R

Ron Rosenfeld

Hi

I am extremely new to Excel programming (this morning) and have had some
macro designs thrust upon me.

I am trying to select rows 1 & 2 of a previous selection (to effectively
format the header rows in table).

I have made it work where the table is 3 columns using:

Selection.Range("A1:C2").Select

but I need to make it work for tables more than 3 colums, the tables could
be any size and anywhere on a worksheet. So I tried:

Selection.Range("1:2").Select

but this didn't work.

Any suggestions?

First of all, and importantly, there is no need to "select" the cells within
VBA in order to format them. It is more efficient to just address the cells
directly.

One way to set a variable so as to refer to the first two rows in a previous
selection:

------------------------
Dim c As Range
Set c = Selection
Set c = c.Resize(2)
------------------------

If all the cells are being formatted the same, then perhaps something like:

----------------------
With c
.NumberFormat = "@"
.Font.Bold = True
.Font.Italic = True
.Font.Size = 16
End With
 
M

ML

This has solved it.

Many thanks.


Ron Rosenfeld said:
First of all, and importantly, there is no need to "select" the cells within
VBA in order to format them. It is more efficient to just address the cells
directly.

One way to set a variable so as to refer to the first two rows in a previous
selection:

------------------------
Dim c As Range
Set c = Selection
Set c = c.Resize(2)
------------------------

If all the cells are being formatted the same, then perhaps something like:

----------------------
With c
.NumberFormat = "@"
.Font.Bold = True
.Font.Italic = True
.Font.Size = 16
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top