+ Reply to Thread
Results 1 to 2 of 2

Way to not sort when only a header row and one row of data to sort?

  1. #1
    StargateFanFromWork
    Guest

    Way to not sort when only a header row and one row of data to sort?

    I have this code which I modify for each scenario in any given workbook:
    ************************************************************
    Sub Sort_Sheet_NotesFromPrograms()
    '
    ActiveSheet.Unprotect 'place at the beginning of the code
    Application.Goto Reference:="R1C1"
    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess,
    _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    ActiveSheet.Protect ' place at end of code
    Range("A2").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    End Sub
    ************************************************************
    The difficulty is that if there aren't at least two rows with data, I get an
    error message. For me, that's fine. I know the reason behind the error box
    so no worries. But I'd like to have better code so I can share these
    workbooks knowing that even blank sheets will work flawlessly and the users
    won't get flustered by these error boxes popping up.

    Is there a way to incorporate something that will help in those cases where
    there is not yet anything to sort?

    TIA. Much appreciated. You guys, as always, are the best! :oD



  2. #2
    Tom Ogilvy
    Guest

    RE: Way to not sort when only a header row and one row of data to sort



    Sub Sort_Sheet_NotesFromPrograms()
    '
    if Range("A2") <> "" then
    ActiveSheet.Unprotect 'place at the beginning of the code
    Application.Goto Reference:="R1C1"
    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess,
    _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    ActiveSheet.Protect ' place at end of code
    Range("A2").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    End if
    End Sub


    or more sophisticated

    lastrow = cells(rows.count,1).End(xlup).row
    if lastrow > 2 then


    End if



    --
    Regards,
    Tom Ogilvy


    "StargateFanFromWork" wrote:

    > I have this code which I modify for each scenario in any given workbook:
    > ************************************************************
    > Sub Sort_Sheet_NotesFromPrograms()
    > '
    > ActiveSheet.Unprotect 'place at the beginning of the code
    > Application.Goto Reference:="R1C1"
    > Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess,
    > _
    > OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    > ActiveSheet.Protect ' place at end of code
    > Range("A2").Select
    > Selection.End(xlDown).Select
    > ActiveCell.Offset(1, 0).Select
    > End Sub
    > ************************************************************
    > The difficulty is that if there aren't at least two rows with data, I get an
    > error message. For me, that's fine. I know the reason behind the error box
    > so no worries. But I'd like to have better code so I can share these
    > workbooks knowing that even blank sheets will work flawlessly and the users
    > won't get flustered by these error boxes popping up.
    >
    > Is there a way to incorporate something that will help in those cases where
    > there is not yet anything to sort?
    >
    > TIA. Much appreciated. You guys, as always, are the best! :oD
    >
    >
    >


+ 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