+ Reply to Thread
Results 1 to 3 of 3

Selecting and then sorting a range

  1. #1
    Sharkbait
    Guest

    Selecting and then sorting a range

    Hey, I had a problem that maybe someone can help me with.

    I'm trying to sort a range in a macro. The columns that compose the
    range never change, but the rows will. (i.e. columns a:o, but rows may
    vary from 26:400 or 26:40)

    I was trying to use range(selection, selection.end(xlDown)) and
    range(selection, selection.end(xlToRight))

    The selection.end(xlDown) works fine, but I have a partially blank
    column in the middle of my column range. It has data for some jobs, but
    not all. When I do selection.end(xlTo Right), it stops at this blank.
    There is however, data I need sorted on the right of this column.

    Is there a way to select the same columns every time, but check for the
    end of data when adding rows to the selection?

    I appreciate any help.


  2. #2
    Dave Peterson
    Guest

    Re: Selecting and then sorting a range

    Can you pick out a column that always has data in it if the row is used?

    I'm gonna use column A.

    dim LastRow as long
    dim myRng as range

    with activesheet
    lastrow = .cells(.rows.count,"A").end(xlup).row
    set myrng = .range("A26:O" & lastrow)
    end with

    with myrng
    .sort key1:=.columns(1), order1:=xlascending, header:=....
    end with

    Sharkbait wrote:
    >
    > Hey, I had a problem that maybe someone can help me with.
    >
    > I'm trying to sort a range in a macro. The columns that compose the
    > range never change, but the rows will. (i.e. columns a:o, but rows may
    > vary from 26:400 or 26:40)
    >
    > I was trying to use range(selection, selection.end(xlDown)) and
    > range(selection, selection.end(xlToRight))
    >
    > The selection.end(xlDown) works fine, but I have a partially blank
    > column in the middle of my column range. It has data for some jobs, but
    > not all. When I do selection.end(xlTo Right), it stops at this blank.
    > There is however, data I need sorted on the right of this column.
    >
    > Is there a way to select the same columns every time, but check for the
    > end of data when adding rows to the selection?
    >
    > I appreciate any help.


    --

    Dave Peterson

  3. #3
    Valued Forum Contributor Excelenator's Avatar
    Join Date
    07-25-2006
    Location
    Wantagh, NY
    Posts
    333
    Why not just select Cell A26 and use

    Selection.CurrentRegion.Select

    ??
    ---------------------------------------------------
    ONLY APPLIES TO VBA RESPONSES WHERE APPROPRIATE
    To insert code into the VBE (Visual Basic Editor)
    1. Copy the code.
    2. Open workbook to paste code into.
    3. Right click any worksheet tab, select View Code
    4. VBE (Visual Basic Editor) opens to that sheets object
    5. You may change to another sheets object or the This Workbook object by double clicking it in the Project window
    6. In the blank space below the word "General" paste the copied code.

+ 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