+ Reply to Thread
Results 1 to 5 of 5

Auto-detect range size

  1. #1
    shelfish
    Guest

    Auto-detect range size

    I'm fairly new to VBA. I'm taking db output and wanting to work with
    it. However, the number of rows varies greatly. The only way I can
    address one of the columns as a range is to use an input box to ask the
    user to select the cells. Put another way, I'd like to use the logic
    used by autofill to know where to stop to know where to establish the
    end of the range. Is there an existing way to do this that I haven't
    found?

    Written generally, here are the are the steps to my script:

    1. Insert columns in A and B.
    2. In B2, Contatenate C2, D2
    3. Autofill Destination:=[here's my problem, range unknown]

    OR I could do it this way

    1. Insert columns in A and B
    2. Do While [same problem, establish size of range in D]
    3. Contatenate as above
    4. Offset to next row
    5. Loop

    There is probably a much improved way to do either of these but like I
    said, I've only been at this for about a week. Thanks in advance for
    any assistance.


  2. #2
    JMB
    Guest

    RE: Auto-detect range size

    First example is the equivalent to going to cell D2, holding the Control key
    and pressing the down arrow, the second is equivalent to going to D65536,
    holding the Control key and pressing the up arrow. Which one you use depends
    on if there are gaps in your data in column D.


    Sub test1()
    Dim rngTest As Range

    With Worksheets("Sheet2")
    Set rngTest = .Range("B2", _
    .Cells(.Range("D2").End(xlDown).Row, 2))
    End With

    MsgBox rngTest.Address
    End Sub


    Sub test2()
    Dim rngTest As Range

    With Worksheets("Sheet2")
    Set rngTest = .Range("B2", _
    .Cells(.Cells(.Rows.Count, _
    4).End(xlUp).Row, 2))
    End With

    MsgBox rngTest.Address
    End Sub



    "shelfish" wrote:

    > I'm fairly new to VBA. I'm taking db output and wanting to work with
    > it. However, the number of rows varies greatly. The only way I can
    > address one of the columns as a range is to use an input box to ask the
    > user to select the cells. Put another way, I'd like to use the logic
    > used by autofill to know where to stop to know where to establish the
    > end of the range. Is there an existing way to do this that I haven't
    > found?
    >
    > Written generally, here are the are the steps to my script:
    >
    > 1. Insert columns in A and B.
    > 2. In B2, Contatenate C2, D2
    > 3. Autofill Destination:=[here's my problem, range unknown]
    >
    > OR I could do it this way
    >
    > 1. Insert columns in A and B
    > 2. Do While [same problem, establish size of range in D]
    > 3. Contatenate as above
    > 4. Offset to next row
    > 5. Loop
    >
    > There is probably a much improved way to do either of these but like I
    > said, I've only been at this for about a week. Thanks in advance for
    > any assistance.
    >
    >


  3. #3
    shelfish
    Guest

    Re: Auto-detect range size

    I am totally confused by what you typed but it works like a charm.
    Thanks-a-million.

    Shelton


  4. #4
    shelfish
    Guest

    Re: Auto-detect range size

    I'm trying to copy this for use in column A as well but with no luck. I
    think it must be that "2" at the end. What does it represent?


  5. #5
    JMB
    Guest

    Re: Auto-detect range size

    I will try to explain. First, anything inside the With/End With that is
    preceded by a period refers back to the object in the first line of the With
    statement - "Sheet2" in this case.

    With Worksheets("Sheet2")
    Set rngTest = .Range("B2", _
    .Cells(.Range("D2").End(xlDown).Row, 2))
    End With

    Range/Cell references inside of other range references also must refer to
    the proper worksheet, or Excel will think you mean the active sheet (like the
    reference to .Range("D2") above

    One way a range can be defined by the beginning and ending cell. You know
    the beginning cell is B2. We find the second cell by defining a row and
    column reference.

    ..Cells(row,column)

    the row is found by starting in D2 and jumping to the last cell that is
    contiguous (similar to you selecting cell D2, holding down the Control key
    and hitting the down arrow). This statement refers to that cell and we add
    ..Row on the end to get just the row reference of that cell.

    ..Range("D2").End(xlDown).Row

    The 2 in the Cells reference refers to column B, as you suspected. So, you
    could use

    With Worksheets("Sheet2")
    Set rngTest = .Range("A2", _
    .Cells(.Range("D2").End(xlDown).Row, 1))
    End With

    You could also save the Row reference of the last cell in column D to a
    variable if you have to use it many times.

    LastRow = WorkSheets("Sheet2").Range("D2").End(xlDown).Row



    "shelfish" wrote:

    > I'm trying to copy this for use in column A as well but with no luck. I
    > think it must be that "2" at the end. What does it represent?
    >
    >


+ 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