+ Reply to Thread
Results 1 to 10 of 10

reference a range using a variable

Hybrid View

  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'

    sub hi()
    dim i as long
    i = 1
    Range("A1")select
    End sub

    I want something like

    Range(i & "1").select
    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

    cells(i,1).select
    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:

    Cells(1,i).Select

  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

    cells(1, i)
    method. Because I get an error...

    This here works for me...

    Workbooks("name.xlsm").Worksheets("Sheet1").Range("A1:A10").copy Destination:= Workbooks("name.xlsx").Worksheets("Sheet1").Range("A1:A10")
    This does not work for me...

    Workbooks("name.xlsm").Worksheets("Sheet1").Range(cells(1,1),cells(10,1)).copy Destination:= Workbooks("name.xlsx").Worksheets("Sheet1").Range(cells(1,1),cells(10,1))
    ...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

    Left(Cells(1, symbol).AddressLocal(RowAbsolute:=False, ColumnAbsolute:=False), 1)

  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:

    Sub Test
    Dim i As Long
    Dim rngSource As Range, rngDestination As Range
    
    i=10
    
    With Workbooks("name.xlsm").Worksheets("Sheet1")
      Set rngSource=.Range(.Cells(1,i),.Cells(10,i))
    End With
    
    With Workbooks("name.xlsx").Worksheets("Sheet1")
      Set rngDestination=..Cells(1,i)
    End With
    
    rngSource.Copy Destination:=rngDestination
    
    End Sub

  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:

    With ThisWorkbook.Sheets(1)
      MsgBox .Range("A1").Value
    End With
    Means that you're referring to the Range A1 on sheet 1 of this workbook.

    The same block of code without the period:

    With ThisWorkbook.Sheets(1)
      MsgBox Range("A1").Value
    End With
    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:

    Workbooks("name.xlsm").Worksheets("Sheet1").Range(cells(1,1),cells(10,1)).copy Destination:= Workbooks("name.xlsx").Worksheets("Sheet1").Range(cells(1,1),cells(10,1))
    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.

    Dim My as String
    My = Left(Cells(1, i).Address(1, 0), InStr(1, Cells(1, i).Address(1, 0), "$") - 1)
    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