+ Reply to Thread
Results 1 to 5 of 5

VBA referecing cell with a variable

  1. #1
    Registered User
    Join Date
    03-18-2011
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    7

    VBA referecing cell with a variable

    Hi,
    I have a quick question...

    suppose I have a variable as follows:

    number = 10
    I wish to code the cell "A[number]", which equates to the cell "A10". How do I do that?

    Hope I'm not confusing anyone!

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: VBA referecing cell with a variable

    Hi howwwie and welcome to the forum.

    There are two ways using VBA.

    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    Both should work.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    03-18-2011
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA referecing cell with a variable

    Thanks for that! It still doesn't seem to like it...
    the full problem is as shown:
    I used a user-defined function in VBA called "commasep", which concatenates a group of cells separated by commas.

    Next I created a macro which selects active cells in column B, and applies the function "commasep":

    Sub CombineEMails()
    '
    ' CombineEMails Macro
    ' Combine emails into one list, separated by commas
    '

    '
    Dim LastRow As Long
    With ActiveSheet
    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
    End With

    Range("F8").Select
    ActiveCell.Formula = "=commasep(B2:B11)"

    End Sub
    ===================================
    See the bit where it is bolded. Instead of "B11", I wish to use B[LastRow].

    I was wondering what code should I use instead of B2:B11?

    Thanks!

  4. #4
    Registered User
    Join Date
    03-18-2011
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: VBA referecing cell with a variable

    @MarvinP

    It works now that I used the "range" function: ActiveCell.Formula = commasep(Range("B2:B" & LastRow))
    Thanks a lot!
    Last edited by howwwie; 03-19-2011 at 01:13 AM.

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: VBA referecing cell with a variable

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1