+ Reply to Thread
Results 1 to 4 of 4

Find Last Row with data and enabled Sorting Macro

Hybrid View

emina002 Find Last Row with data and... 06-17-2011, 05:29 PM
mojo249 Re: Find Last Row with data... 06-17-2011, 06:04 PM
emina002 Re: Find Last Row with data... 06-17-2011, 06:08 PM
protonLeah Re: Find Last Row with data... 06-17-2011, 06:20 PM
  1. #1
    Forum Contributor
    Join Date
    05-16-2011
    Location
    Philippines
    MS-Off Ver
    Excel 365/Excel 2016
    Posts
    315

    Find Last Row with data and enabled Sorting Macro

    Hi to all.Hoping you have a good day.I would like seek help or advices regarding my program. Please see attachment to visualize my problem.

    First I have 4 column A to D. that reviews all the tabs name in a workbook,it was reviewed based on their position. I would like to sort out the value of column A after the word "Template". I can record a macro and since my program is to review all tabs in a workbook my last tab was "Template" and If I unhide one tab "template" will be include in sorting.How can i determine the row "template" and after that I perform the sorting?

    Here is my recorded macro. In my sample file the Worksheet is "Sheet1",my original worksheet named Index.Thanks for the big help.

     ActiveWorkbook.Worksheets("Index").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Index").Sort.SortFields.Add Key:=Range("A16"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            With ActiveWorkbook.Worksheets("Index").Sort
            .SetRange Range("A16:A100")
            .Header = xlNo
            .MatchCase = True
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
           .Apply
         
          End With
    Attached Files Attached Files
    Last edited by emina002; 07-05-2011 at 06:00 AM. Reason: No attachment

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Find Last Row with data and enabled Sorting Macro

    Hi

    See your updated workbook attached.

    Dion

    Sub SortData()
    
    Dim ws As Worksheet
    Dim varRow As Long
    Dim varFirstRow As Long
    Dim varLastRow As Long
    Dim rng As Range
    
    Set ws = ThisWorkbook.Sheets("Sheet1")
    varLastRow = ws.Range("A60000").End(xlUp).Row
    
    'Find varFirstRow
    For varRow = 2 To varLastRow
        If ws.Cells(varRow, 1).Value = "Template" Then
            varFirstRow = varRow + 1
        End If
    Next varRow
    
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range(Cells(varFirstRow, 1), Cells(varLastRow, 1)) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        
        .SetRange Range(Cells(varFirstRow, 1), Cells(varLastRow, 4))
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    End Sub
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    05-16-2011
    Location
    Philippines
    MS-Off Ver
    Excel 365/Excel 2016
    Posts
    315

    Re: Find Last Row with data and enabled Sorting Macro

    @mojo249 Thanks that was a big help

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,964

    Re: Find Last Row with data and enabled Sorting Macro

    maybe:
        Dim TemplateRow As Variant, _
            LastARow    As Long
            
        LastARow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
        Set TemplateRow = Sheets("sheet1").Range("A:A").Find("Template")
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range(Cells(TemplateRow.Row, 1), Cells(LastARow, 1))
            .Header = xlNo
            .MatchCase = True
           .Apply
        End With
    Ben Van Johnson

+ 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