+ Reply to Thread
Results 1 to 9 of 9

Sort rows after a specific text is found

Hybrid View

  1. #1
    Registered User
    Join Date
    11-23-2010
    Location
    dallas, texas
    MS-Off Ver
    Excel 2003
    Posts
    7

    Lightbulb Sort rows after a specific text is found

    Hello,

    I am needing help to find a macro that can: find a specific text - for ex: "Schedule (below)" in cell C100 - and after it is found, the code would skip the next row below, then sort
    all rows by date from oldest to newest.
    eg:
    A B C
    100 Schedule (below)
    101 Due WO# Name
    102 11/20 0395625 B.G
    103 11/18 0385612 C.F
    104 11/05 0396452 B.O.G

    to

    A B C
    100 Schedule (below)
    101 Due WO# Name
    102 11/05 0396452 B.O.G
    103 11/18 0385612 C.F
    104 11/20 0395625 B.G

    I've recorded a macro, but I don't know how to make it works if other users have input or added extra rows above the "Schedule (below)" row.
    Please help.

    My recorded code for my actual worksheet:
    Sub Macro1()
        Range("A1588").Select
        ActiveWindow.SmallScroll Down:=543
        Range("A1588:J2139").Select
        ActiveWorkbook.Worksheets("vinyl").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("vinyl").Sort.SortFields.Add Key:=Range( _
            "A1588:A2139"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.Worksheets("vinyl").Sort.SortFields.Add Key:=Range( _
            "C1588:C2139"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        ActiveWorkbook.Worksheets("vinyl").Sort.SortFields.Add Key:=Range( _
            "B1588:B2139"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortNormal
        With ActiveWorkbook.Worksheets("vinyl").Sort
            .SetRange Range("A1588:J2139")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        ActiveWindow.SmallScroll Down:=-309
    End Sub
    Thanks very much in advance

    Tom
    Last edited by alansidman; 11-23-2015 at 07:55 PM. Reason: code tags added

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,719

    Re: Sort rows after a specific text is found

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    11-23-2010
    Location
    dallas, texas
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Sort rows after a specific text is found

    Thank you Alan. Yes I will take note next time.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Sort rows after a specific text is found

    Hi,

    One way as a module level procedure

    Sub SortData()
        Dim lrow As Long
        lrow = Range("C1:C100").Find(What:="Schedule (below)").Row
        Range("A" & lrow + 2 & ":C" & Range("C" & Rows.Count).End(xlUp).Row).Sort key1:=Range("B:B")
    End Sub
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Registered User
    Join Date
    11-23-2010
    Location
    dallas, texas
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Sort rows after a specific text is found

    Hi Richard,

    Thank you for your help. I've tried your code, and changed a little but I still couldn't get it working. What I really need is when I run the code, after it search for the specific text "Schedule (below)", then it would skip 1 more row, and start sorting all the rows below based on their due dates (col A's) from the oldest to the newest due date. when sorting, all the data and their formatting should move as well. Is this possible?
    thanks

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Sort rows after a specific text is found

    Quote Originally Posted by t.pham41 View Post
    Hi Richard,

    Thank you for your help. I've tried your code, and changed a little but I still couldn't get it working. ......snipped
    My code works when I created an example workbook with the data you supplied. If you're saying it's not working then please upload your workbook since presumably the layout is somewhat different.

  7. #7
    Registered User
    Join Date
    11-23-2010
    Location
    dallas, texas
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Sort rows after a specific text is found

    Hi Richard,

    Thanks for responding. Sorry I was out of town.
    I've attached a sample of my workbook. My actual workbook has over 10's of rows.
    I've tried the code, but somehow some of the rows (assuming after it found text "Schedule (below)", then skip the next row) the dates in col A are not sorting in ascending order (suppose to be from oldest dates to newest dates). Please let me know what I'm missing.

    thank you very much again.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Sort rows after a specific text is found

    Maybe:

    Sub t_pham41()
    Dim r As Range, x As Long
    x = Range("A" & Rows.Count).End(3).row
    Set r = Columns(2).Find("Schedule(below)", LookIn:=xlValues, lookat:=xlWhole)
        If Not r Is Nothing Then
            Range(Cells(r.row + 2, 1), Cells(x, 4)).Sort Cells(r.row + 2, 2), xlAscending
        End If
    Set r = Nothing
    End Sub

  9. #9
    Registered User
    Join Date
    11-23-2010
    Location
    dallas, texas
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Sort rows after a specific text is found

    Hi John,

    Thank you! I've also tried your code, but I have same problem with Richard's help. Please see my attached workbook for ref. I am new to macros, so please bare with me.
    thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA to copy specific rows and stop when it found null rows
    By Khaled Diab in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2014, 01:50 AM
  2. [SOLVED] Would like to copy rows until specific value is found
    By caooi23 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-07-2013, 01:41 AM
  3. Replies: 8
    Last Post: 06-03-2013, 05:34 PM
  4. [SOLVED] Locate column with specific title and delete rows if predefined set of text is found
    By johnny_tc in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-25-2012, 01:57 PM
  5. fastest way to find the row where a specific text is found in a specific column
    By getgray in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2011, 02:45 PM
  6. Set variable sort range based on found text
    By jeffbert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-20-2006, 05:20 PM

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