Consulting

Results 1 to 2 of 2

Thread: Sort ascending with expand selection

  1. #1

    Sort ascending with expand selection

    I have reocrded a macro to sort column B with expand selection. The code is as follows
    Sheets("Master").Select
        Range("B1").Select
        ActiveSheet.Unprotect
        Columns("B:B").Select
        Range("A1:AZ79").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:= _
            xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
        Range("B1").Select
    I want to use the same code in a loop but the problem is that the range used in the recorded code uses the actual colum no used as Range("A1:AZ79").
    But this will not be fixed for all sheets . For some sheets last column may be M or Z etc.
    How can I adjust the code so that it will take the last used column for every sheet?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    [vba]

    With Sheets("Master")

    .Unprotect
    lastcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    .Range("A1").Resize(79, lastcol).Sort Key1:=Range("B1"), _
    Order1:=xlAscending, _
    Header:=xlGuess, _
    OrderCustom:=1, _
    MatchCase:=False, _
    Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    End With
    [/vba]
    ____________________________________________
    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

Posting Permissions

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