Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old June 19th, 2004, 06:54 AM
Registered User
Join Date: Jun 2004
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default variable used as cell reference?

hey everybody

hoping someone can help with VBA syntax:

I want to use the variable x as a row number reference,

 'Insert Total calculation
    x = 0
    'go the end of the column
    Do While IsEmpty(ActiveCell.Offset(0, 0)) = False
        x = x + 1
        ActiveCell.Offset(1, 0).Select

I have tried the two statements below but they seem to be illegal.

 ActiveCell.FormulaR1C1 = "=SUM(R[ActiveCell.Offset(0,0)]C:R[ActiveCell.Offset(-&x,0)]C)"

ActiveCell.FormulaR1C1 = "=SUM(R[-&x]C:R[-1]C)"

Any help appreciated

Reply With Quote
  #2 (permalink)  
Old June 27th, 2004, 11:16 AM
Authorized User
Join Date: Aug 2003
Location: Penn Yan, New York, USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts


Think you should probably consider the different ways of addressing cells. For instance, you can go to cells C1 with either of the following commands:

                 Cells(1, 3).Select

The 2nd example is in the form Cells(Row,Col).Select -- which has the advantage of being able to set up a numeric loop with no trouble. For instance, if you wanted to select (sequentially) all of the cells in column C from row 1 to 100, you'd simply create a For/Next loop such as:
    For i = 1 to 100
        Cells(i, 3).Select

To get more examples of this, suggest you see the page referenced below:


Reply With Quote
  #3 (permalink)  
Old June 27th, 2004, 12:16 PM
Friend of Wrox
Join Date: Jun 2003
Location: , , Denmark.
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts

Also, if for some reason you have an empty cell in the middle of your col H, your loop would stop there.
Try this one:

Dim Lst As Long
Dim Rng As Range

' the last cell in col H that has a value

'save the rowNo
Lst = ActiveCell.row

'Define the range
Set Rng = Range("H1:H" & Lst)

'Go to cell that should contain the sum
ActiveCell.Offset(1, 0).Select

'Calculate the sum
ActiveCell.Formula = "=SUM(" & Rng.Address & ")"
Reply With Quote
  #4 (permalink)  
Old March 7th, 2014, 11:53 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Mar 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile so simple yet overlooked

tnx carlR.U just made my day. i ve been using macro for quite some time now, but this variable cell selection dint occur to me.
i was able to use it for range selection like - Range("Bi:AAi").Select, but not a particular cell.
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a variable to reference a cell MikeCt203 Excel VBA 2 March 24th, 2008 04:02 PM
Using Match function retrieve cell reference not wapfu Excel VBA 2 December 11th, 2006 04:09 AM
Reference a Cell in a Webtable. Ahrenl Excel VBA 2 May 25th, 2005 11:25 AM
Passing the cell reference to a variable Artist Excel VBA 0 April 14th, 2004 04:34 AM

All times are GMT -4. The time now is 11:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.