Consulting

Results 1 to 5 of 5

Thread: How to Construct a Column Variable for Range

  1. #1

    How to Construct a Column Variable for Range

    I have a series of variables (Col1, Col2, ...) for column letters. If I substitute a variable for the column letter:
    Range(Col1 & 3).Select
    it works just fine.
    I would like to create a loop that looks something like:

    Dim Col1 As String, Col2 As String
    Col1 = "A"
    Col2 = "F"
    For N=1 To 2
        Range("Col" & N & N + 2).Clear
    Next N
    This of course doesn't work because it would create something like:
    Range(Col12).Clear
    In fact it might create something like:
    Range("Col"12).Clear (I'm not sure)
    What I need is something like the Excel "Indirect" function, I think.
    Range(Indirect("Col" & N) & N + 2).Clear
    In any case I'm going crazy trying to figure it out.
    Any ideas?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Cyberdude
    I have a series of variables (Col1, Col2, ...) for column letters. If I substitute a variable for the column letter:
    Range(Col1 & 3).Select
    it works just fine.
    I would like to create a loop that looks something like:

    Dim Col1 As String, Col2 As String
    Col1 = "A"
    Col2 = "F"
    For N=1 To 2
        Range("Col" & N & N + 2).Clear
    Next N
    This of course doesn't work because it would create something like:
    Range(Col12).Clear
    In fact it might create something like:
    Range("Col"12).Clear (I'm not sure)
    What I need is something like the Excel "Indirect" function, I think.
    Range(Indirect("Col" & N) & N + 2).Clear
    In any case I'm going crazy trying to figure it out.
    Any ideas?
    Dude,

    Try an array


    Dim ColArray
    Dim N As Long
        ColArray = Array("A", "F")
        For N = 0 To 1
            Range(ColArray(N), N + 3).Clear
        Next N
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Hey, xld, that looks promising ... I'll give it a try. Thanx.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Cyberdude
    Hey, xld, that looks promising ... I'll give it a try. Thanx.
    If you do, get it right (as I didn't)


    ColArray = Array("A", "F")
        For N = 0 To 1
            Range(ColArray(N) & N + 3).Clear
        Next N
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Hey, xld, the array solution works (and I did spot the missing "&"). That's a BIG help in reducing my code lines.
    Thanx.

Posting Permissions

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