+ Reply to Thread
Results 1 to 3 of 3

Using variables in range columns

Hybrid View

  1. #1
    Registered User
    Join Date
    06-06-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    23

    Using variables in range columns

    This should be pretty simple, I'm just struggling with it:

    So I'm trying to make a loop that takes all of the values in column "x" and creates a named array with the value in x1. I'm running into trouble with using the variable x as a means of defining the column in the range. Advice?

    
    Sub coding()
    
    Dim x As Integer, hed As String
    With Worksheets("Sheet1")
    x = 1
    
    
    Do Until .Cells(x, 1).Value = ""
    hed = .Cells(x, 1).Value
    ThisWorkbook.Names.Add Name:=hed, RefersTo:=Worksheets("Sheet1").Range(x & "1" & x & 100)
    x = x + 1
    Loop
    
    End With
    
    End Sub

  2. #2
    Registered User
    Join Date
    06-06-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Using variables in range columns

    Got it, if anyone runs into a similar problem the solution is:

     
    Range(Cells(x, 1), Cells(x, 100))

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,782

    Re: Using variables in range columns

    Instead of Range use Cells:

    .Range(x & "1" & x & 100)

    becomes:

    .Range(Cells(x, 1), Cells(x, 100))

    that assumes that x is referring to a row. If it's a column:


    .Range(Cells(1, x), Cells(100, x))


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ 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