Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default Relative cell position in VBA

When I am writing a formula in VBA how do I refer to a cell relatively rather
than an exact cell?

For example:
Range("B1").Select
Selection.End (xlDown)
ActiveCell.Offset(1, 7).Select

This puts me in cell in column "I" but the row is unknown to me. Now I want
to write a formula that adds the numbers in columns B-D and subtracts the
number in column J of the same line into column I. How do I reference cells
on the same row but in different columns when I do not know the row number?
And, can this be adjusted if I know the row number but the column is relative?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,722
Default Relative cell position in VBA

Maybe something like
ActiveCell.FormulaR1C1 = "=R[-1]C[-1]"

Where you use the numbers in brackets to offset from current cell. If you're
already in the row/column you want, leave out those brackets, ie.
ActiveCell.FormulaR1C1 = "=R[-1]C"
--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*


"iashorty" wrote:

When I am writing a formula in VBA how do I refer to a cell relatively rather
than an exact cell?

For example:
Range("B1").Select
Selection.End (xlDown)
ActiveCell.Offset(1, 7).Select

This puts me in cell in column "I" but the row is unknown to me. Now I want
to write a formula that adds the numbers in columns B-D and subtracts the
number in column J of the same line into column I. How do I reference cells
on the same row but in different columns when I do not know the row number?
And, can this be adjusted if I know the row number but the column is relative?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Relative cell position in VBA

Leave it out and VB will use the current row. So, change your last line to
this (note the comma)...

ActiveCell.Offset(,N).Select

where N is the number of columns you want to offset from the current row. I
would like to point out though that you do not need to select a cell in
order to operate on it or its content in VB. Your 3 lines of posted code can
be reduced to this single line of code...

Range("B1").End(xlDown).Offset(1, 7).Select

and if you are selecting this cell to do something with it, that operation
can probably be combined into this single line of code as well. Perhaps this
previous posting of mine (a response to another person using
Select/Selection type constructions) will be of some help to you in your
future programming...

Whenever you see code constructed like this...

Range("A1").Select
Selection.<whatever

you can almost always do this instead...

Range("A1").<whatever

In your particular case, you have this...

Range("C2:C8193").Select 'select cells to export
For Each r In Selection.Rows

which, using the above concept, can be reduced to this...

For Each r In Range("C2:C8193").Rows

Notice, all I have done is replace Selection with the range you Select(ed)
in the previous statement and eliminate the process of doing any
Select(ion)s. Stated another way, the Selection produced from
Range(...).Select is a range and, of course, Range(...) is a range... and,
in fact, they are the same range, so it doesn't matter which one you use.
The added benefit of not selecting ranges first is your active cell does not
change.

--
Rick (MVP - Excel)


"iashorty" wrote in message
...
When I am writing a formula in VBA how do I refer to a cell relatively
rather
than an exact cell?

For example:
Range("B1").Select
Selection.End (xlDown)
ActiveCell.Offset(1, 7).Select

This puts me in cell in column "I" but the row is unknown to me. Now I
want
to write a formula that adds the numbers in columns B-D and subtracts the
number in column J of the same line into column I. How do I reference
cells
on the same row but in different columns when I do not know the row
number?
And, can this be adjusted if I know the row number but the column is
relative?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Position drawing object relative to cell Horatio J. Bilge, Jr. Excel Discussion (Misc queries) 2 September 29th 08 09:16 PM
How to macro to relative position jk9533 Excel Discussion (Misc queries) 6 October 15th 07 08:11 PM
Displaying a cell relative to the position to another cell Tibbs Excel Discussion (Misc queries) 2 July 21st 06 08:28 AM
Relative Cell position NOT working with or without macro Scratching my Head Excel Discussion (Misc queries) 6 May 30th 05 06:12 PM
Create cell formula from a relative position Geoff[_11_] Excel Programming 3 May 26th 05 06:42 PM


All times are GMT +1. The time now is 09:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"