+ Reply to Thread
Results 1 to 7 of 7

Set range and offset

Hybrid View

  1. #1
    Registered User
    Join Date
    04-12-2007
    Posts
    57

    Set range and offset

    I am having trouble with one line of this code that I can't figure out how to write correctly. I have set the range, but I need to offset by 10 rows up. My code fails at set rng. Here is what I have so far
    Dim LsTRow As Long
    Dim rng As range
    LsTRow = Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = range("A7:q" & LsTRow).Offset(-10, 0).Select
    
    With rng
        .Sort Key1:=.Cells(7, 3), Order1:=xlAscending, _
        Key2:=.Cells(7, 1), Order3:=xlAscending, Header _
        :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
        , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
        xlSortNormal
    End With
    End Sub
    Last edited by sammar12; 05-16-2009 at 06:10 PM.

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

    Re: Set range and offset

    If the range starts in A7, you can't offset it 10 rows up.

    Please change your code tags from HTML to CODE.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    04-12-2007
    Posts
    57

    Re: Set range and offset

    Could you suggest a method to get around this?

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

    Re: Set range and offset

    Probably, if I knew what you were trying to do. Post a workbook and explain in context.

  5. #5
    Registered User
    Join Date
    04-12-2007
    Posts
    57

    Re: Set range and offset

    Attached is a sample workbook of a similiar situation. This is a list of available products. Monthly the number of products can increase or decrease. I like to generate a sell sheet by item # for our sales people and alphabetical. Therefore I would like to set the range (in this example only through column c, but my actual book is through column Q). Once the range is set, I would like to sort based on Column c, however I would like to leave the last 10 items always at the bottom of the workbook.
    Attached Files Attached Files

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

    Re: Set range and offset

    Ah, the clarity of an example.
    Sub x()
        Dim lRow    As Long
        Dim rng     As Range
    
        lRow = Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = Range("A7", Cells(lRow, "Q"))
        If rng.Rows.Count <= 11 Then Exit Sub
        Set rng = rng.Resize(rng.Rows.Count - 10)
    
        With rng
            .Sort Key1:=.Cells(1, "C"), Order1:=xlAscending, _
                  Key2:=.Cells(1, "A"), Order1:=xlAscending, _
                  Header:=xlNo, OrderCustom:=1, _
                  MatchCase:=False, Orientation:=xlTopToBottom
        End With
    End Sub

+ 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