ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How do I make references to cells in subroutines relative? (https://www.excelbanter.com/excel-programming/377471-how-do-i-make-references-cells-subroutines-relative.html)

Atreides

How do I make references to cells in subroutines relative?
 
How do I make references to cells in subroutines relative?

For example, if part of my subroutine is:

Range("A5:D33").Select

then I insert or delete cells in the spreadsheet, the references to these
cells in the macro stay the same, and are thus incorrect. They always have to
be updated manually. How can I make these change automatically when I modify
the spreadsheet?

Or is there a better way to reference cells in the worksheet?

Thanks
Atreides

Ardus Petus

How do I make references to cells in subroutines relative?
 
Use named range instead of literal

Cheers
--
AP

"Atreides" <atreides1AThotmailD0Tcom a écrit dans le message de news:
...
How do I make references to cells in subroutines relative?

For example, if part of my subroutine is:

Range("A5:D33").Select

then I insert or delete cells in the spreadsheet, the references to these
cells in the macro stay the same, and are thus incorrect. They always have
to
be updated manually. How can I make these change automatically when I
modify
the spreadsheet?

Or is there a better way to reference cells in the worksheet?

Thanks
Atreides




Atreides

How do I make references to cells in subroutines relative?
 
Hi Ardus,

Sorry, I'm not sure what you mean by that. I've leart VBA by experiment
rather than instruction, so I'm not really familiar with the terms. How do I
use a named range?

Thanks
Peter

"Ardus Petus" wrote:

Use named range instead of literal

Cheers
--
AP

"Atreides" <atreides1AThotmailD0Tcom a écrit dans le message de news:
...
How do I make references to cells in subroutines relative?

For example, if part of my subroutine is:

Range("A5:D33").Select

then I insert or delete cells in the spreadsheet, the references to these
cells in the macro stay the same, and are thus incorrect. They always have
to
be updated manually. How can I make these change automatically when I
modify
the spreadsheet?

Or is there a better way to reference cells in the worksheet?

Thanks
Atreides





Ardus Petus

How do I make references to cells in subroutines relative?
 
Select the range you want to name
Use menu InsertNameDefine
enter a valid name (eg: MyRange)
click OK

Now you can use:
Range("MyRange").Select
anywhere in your code.

Insertions and deletions will be reflected in your name definaition

Cheers,
--
AP


"Atreides" <atreides1AThotmailD0Tcom a écrit dans le message de news:
...
Hi Ardus,

Sorry, I'm not sure what you mean by that. I've leart VBA by experiment
rather than instruction, so I'm not really familiar with the terms. How do
I
use a named range?

Thanks
Peter

"Ardus Petus" wrote:

Use named range instead of literal

Cheers
--
AP

"Atreides" <atreides1AThotmailD0Tcom a écrit dans le message de news:
...
How do I make references to cells in subroutines relative?

For example, if part of my subroutine is:

Range("A5:D33").Select

then I insert or delete cells in the spreadsheet, the references to
these
cells in the macro stay the same, and are thus incorrect. They always
have
to
be updated manually. How can I make these change automatically when I
modify
the spreadsheet?

Or is there a better way to reference cells in the worksheet?

Thanks
Atreides







Atreides

How do I make references to cells in subroutines relative?
 
Thanks Ardus, that explains it well.

It seems that Names are even more useful than I previously thought!

Atreides

"Ardus Petus" wrote:

Select the range you want to name
Use menu InsertNameDefine
enter a valid name (eg: MyRange)
click OK

Now you can use:
Range("MyRange").Select
anywhere in your code.

Insertions and deletions will be reflected in your name definaition

Cheers,
--
AP


"Atreides" <atreides1AThotmailD0Tcom a écrit dans le message de news:
...
Hi Ardus,

Sorry, I'm not sure what you mean by that. I've leart VBA by experiment
rather than instruction, so I'm not really familiar with the terms. How do
I
use a named range?

Thanks
Peter

"Ardus Petus" wrote:

Use named range instead of literal

Cheers
--
AP

"Atreides" <atreides1AThotmailD0Tcom a écrit dans le message de news:
...
How do I make references to cells in subroutines relative?

For example, if part of my subroutine is:

Range("A5:D33").Select

then I insert or delete cells in the spreadsheet, the references to
these
cells in the macro stay the same, and are thus incorrect. They always
have
to
be updated manually. How can I make these change automatically when I
modify
the spreadsheet?

Or is there a better way to reference cells in the worksheet?

Thanks
Atreides








All times are GMT +1. The time now is 05:17 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com