+ Reply to Thread
Results 1 to 5 of 5

passing a range as a parameter

  1. #1
    Registered User
    Join Date
    07-07-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    24

    passing a range as a parameter

    I've posted about a similar issue in the past but never really understood. I'm at the same roadblock again and I've tried to make it really simple, but I still can't get it to work.

    Please Login or Register  to view this content.
    Two things: I'd like for it to sort based on the first column of the range and it currently just errors out. I'm not sure what's wrong. Secondly, I don't like hardcoding in the number 23. It will always sort starting at Cell(1,1) but I'd like for it to automatically adjust to the end of column 1, e.g. Cells(1,X). Any help would be greatly appreciated.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: passing a range as a parameter

    Remove the parens:
    Please Login or Register  to view this content.
    Then
    Please Login or Register  to view this content.
    Last edited by shg; 11-15-2009 at 04:29 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-07-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: passing a range as a parameter

    Thanks. I'm still getting an error, but now it's in a different place so this is progress.

    "Method 'Range' of object '_Global' failed" and it highlighted this line:

    Range(rng).Sort Key1:=Range(Cells(1, 1), Cells(1, 23))

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: passing a range as a parameter

    Did you look at the second half of the post?

    Range(rng) is an invalid construct.

    Range(Cells(1, 1), Cells(1, 23)) refers to A1:W1 on the active sheet, unrelated to the variable rng.

  5. #5
    Registered User
    Join Date
    07-07-2009
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: passing a range as a parameter

    Quote Originally Posted by shg View Post
    Did you look at the second half of the post?

    Range(rng) is an invalid construct.

    Range(Cells(1, 1), Cells(1, 23)) refers to A1:W1 on the active sheet, unrelated to the variable rng.
    You edited your post and added the second part after I loaded the page and started typing my reply. Thank you though. It was exactly what I needed. I'm still learning the basics of VBA and I didn't think to reference rng like it is an object.

+ 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