+ Reply to Thread
Results 1 to 10 of 10

reference a range using a variable

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    reference a range using a variable

    how do i reference A1 using my long variable 'i'

    Please Login or Register  to view this content.

    I want something like

    Please Login or Register  to view this content.
    but I need to get the column letter of cells(1, i) somehow.

  2. #2
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: reference a range using a variable

    Please Login or Register  to view this content.
    Regards,
    Vandan

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: reference a range using a variable

    Wrong way round, there - A is the column, not the row:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: reference a range using a variable

    hi niko79542, there is a couple of ways for Range syntax:

    1. Range("a1") - using variables: Range(x & y), where x = "a" and y=1
    2. Range(Cells(i,n),cells(m,k)) where i,m - row identifier, n,k - columns

  5. #5
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: reference a range using a variable

    Noooo!!! Dont go away!!! Yes I understand these options, however my variable i is a number, and I cant use the

    Please Login or Register  to view this content.
    method. Because I get an error...

    This here works for me...

    Please Login or Register  to view this content.
    This does not work for me...

    Please Login or Register  to view this content.
    ...I figured I would save you guys the long winded reason as to why I am not using cells(1,i)...But here it is..

    ---------- Post added at 04:48 PM ---------- Previous post was at 04:46 PM ----------

    Tchh... One really complicated way is

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: reference a range using a variable

    Try this:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: reference a range using a variable

    Didn't get a change to try this out last night, but this is what I was looking for, thanks Andrew

  8. #8
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: reference a range using a variable

    So, I did scrounge up a solution. thanks again Andrew.

    is the ..Cells in your code a shortcut? or did you just not bother typing out Range().

    Not sure you ever revisit this thread again...but if you do =P.

    nick

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: reference a range using a variable

    Inside a With block you use a period at the start of an object to show it's a child of the object named in the With.

    So:

    Please Login or Register  to view this content.
    Means that you're referring to the Range A1 on sheet 1 of this workbook.

    The same block of code without the period:

    Please Login or Register  to view this content.
    Means you're referring to range A1 on the active sheet, so the two blocks of code can return different results depending on which sheet is active when code is run.

    This was what was causing you problems, when you were specifying:

    Please Login or Register  to view this content.
    All 4 references to Cells refer to objects on the active sheet, not necessarily on the sheets you're trying to copy from or paste to, and this was causing Excel problems. You were effectively saying, "Make a range on this sheet, from cell A to cell B on another sheet".

    All my code did was precisely specify which cells I was referencing.

    Does that make sense? It's a bit counter-intuitive, but such are the strange byways of Excel coding

  10. #10
    Registered User
    Join Date
    05-18-2012
    Location
    Norwalk, CT
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: reference a range using a variable

    Ahh....That totally makes sense.

    What I ended up doing was using Cells.Address to convert the column number to a letter, and then it was easy to go from there.

    Please Login or Register  to view this content.
    I thought I tried your way of referring to ranges using the period before the cell reference, but I got an error. I'll try it again now with your explanation in mind.

    Nick

+ 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