Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 19th, 2004, 06:54 AM
Registered User
 
Join Date: Jun 2004
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
    Range("H1").Select
    'go the end of the column
    Do While IsEmpty(ActiveCell.Offset(0, 0)) = False
        x = x + 1
        ActiveCell.Offset(1, 0).Select
    Loop

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
thanks


 
Old June 27th, 2004, 11:16 AM
Authorized User
 
Join Date: Aug 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

dabith,

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:

                 Range("C1").Select
                 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
    Next

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

CarlR
www.vba-programmer.com

 
Old June 27th, 2004, 12:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Range("H65536").End(xlUp).Select

'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 & ")"
 
Old March 8th, 2014, 12:53 AM
Registered User
 
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.





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 05: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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.