+ Reply to Thread
Results 1 to 7 of 7

Sort Select Range: Either Find Column Header or Last Column:Last Row

Hybrid View

  1. #1
    Registered User
    Join Date
    06-24-2014
    Location
    SC
    MS-Off Ver
    2007 - 2013
    Posts
    12

    Sort Select Range: Either Find Column Header or Last Column:Last Row

    Range("A3:F50").Select
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("F3:F50") _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.Sort
            .SetRange Range("A2:F50")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    My code was hard coded to sort on Column F, and I just guessed by going to row 50 that it would go past my actual data. Today it failed and I realized it failed because my raw data had an extra column. The column I need to sort on is "Grand Total". Because of the extra column, it was now Column G instead of F. So apparently it may not be consistently in the same place. So can I include a FIND in the range selection? Also include something that looks for the last row of data?

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,258

    Re: Sort Select Range: Either Find Column Header or Last Column:Last Row

    Try it like this:
       Sub TestMacro()
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("A3").CurrentRegion.Find("Grand Total") _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.Sort
            .SetRange Range("A3").CurrentRegion
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .Apply
        End With
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    06-24-2014
    Location
    SC
    MS-Off Ver
    2007 - 2013
    Posts
    12

    Re: Sort Select Range: Either Find Column Header or Last Column:Last Row

    That's close. The Grand Total column header is merged between H1:H2. Attached is a sample set.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,258

    Re: Sort Select Range: Either Find Column Header or Last Column:Last Row

     Sub TestMacro2()
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("1:2").Find("Grand Total").Offset(2) _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.Sort
            .SetRange Intersect(Range("2:" & Cells(Rows.Count, "A").End(xlUp).Row - 1), Range("A3").CurrentRegion)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .Apply
        End With
    End Sub

  5. #5
    Registered User
    Join Date
    06-24-2014
    Location
    SC
    MS-Off Ver
    2007 - 2013
    Posts
    12

    Re: Sort Select Range: Either Find Column Header or Last Column:Last Row

    So I've spent the better part of the day trying to learn and understand vba range select statements. I've attached a new sample report. This has 2 tabs. What my normal raw data looks like and what it looks like after I ran my macro with a hard coded Column F. Below is my entire CODE. Bernie's last update does sort of sort on the "Grand Total" column but doesn't move up the "Grand Total" row. This code was working well for me until a new column was inserted and it thew everything off. So I'm trying to adjust the code to be dynamic based on possible changes in the raw data.

    Sub SFCaseCounts()
    '
    ' SFCaseCounts Macro
    ' Sort and Format SF Case Count Reports
    '
    ' Keyboard Shortcut: Ctrl+Shift+S
    '
       'Un-merge and delete Column B
        Range("A1:B1").Select
        Selection.UnMerge
        Columns("B:B").Select
        Selection.Delete Shift:=xlToLeft
       'Sort entire active sheet based on last column with header Grand Total
        Range("A3:F50").Select
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("F3:F50") _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.Sort
            .SetRange Range("A2:F50")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
       'Highlight the top 3 rows through the last column
        Range("A1:F3").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 15773696
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
       'Highlight last column
        Range("F4:F50").Select
        With Selection.Interior
            .PatternColorIndex = xlAutomatic
            .Color = 15773696
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
       'Highlight range of A4 through last row but next to last column with white
        Range("A4:E50").Select
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
       'Add Border to cells with data
        Range("A1:F50").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
       'Adjust column width of Column A
        Columns("A:A").ColumnWidth = 21.29
        Cells.Select
        Cells.EntireRow.AutoFit
    End Sub
    Here is some code I found that I thought might help me, but I couldn't get it to work.
    Sub iSOSC()
    Dim ans
    
    ans = SOSC("UnPivot", "F")
    
    If ans = False Then
      MsgBox "Check the Function!"
    End If
    End Sub
    
    '===
    
    'Sort On Single Columns SOSC
    Function SOSC(Sht As String, cLetterToSort As String) As Boolean
    Dim ws As Worksheet
    Dim Rng As Range
    Dim LastRow As Long
    Dim LastCol As Long
     
    
    On Error GoTo Exit_Func
    Set ws = ThisWorkbook.Sheets(Sht)
    With ws
      LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
      LastRow = .Cells(.Rows.Count, LastCol).End(xlUp).Row
    
      .Range(.Cells(1, 1), .Cells(LastRow, LastCol)).Sort _
          Key1:=.Range(cLetterToSort & 1), _
          Order1:=xlAscending, _
          Header:=xlYes, _
          OrderCustom:=1, _
          MatchCase:=False, _
          Orientation:=xlTopToBottom
    End With
    SOSC = True
    Exit Function
    
    Exit_Func:
    SOSC = False
    End Function
    I'm also trying to learn from this, particularly the ability to select various ranges. Seems like a skill that will always be useful using vb macros, so I won't have to ask for everything and eventually be able to help others
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,258

    Re: Sort Select Range: Either Find Column Header or Last Column:Last Row

    I did not 'move up' the Grand Total row because the summary should remain at the bottom. To get it to move up, use this, which just removes the -1 from the .SetRange line:

    Sub TestMacro2()
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("1:2").Find("Grand Total").Offset(2) _
            , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.ActiveSheet.Sort
            .SetRange Intersect(Range("2:" & Cells(Rows.Count, "A").End(xlUp).Row), Range("A3").CurrentRegion)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .Apply
        End With
    End Sub
    As a side note - merged cells and multiple rows of headers are things you should avoid in your workbook design.

  7. #7
    Registered User
    Join Date
    06-24-2014
    Location
    SC
    MS-Off Ver
    2007 - 2013
    Posts
    12

    Re: Sort Select Range: Either Find Column Header or Last Column:Last Row

    I'll give that a try. The data is generated from the salesforce application, so I can't alter the design. At least not that I'm aware of at this time.

    Thanks Bernie

+ 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. Find Column Header that includes String, then use that Address as Sort Key1
    By TimBZKK in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2013, 05:17 PM
  2. Replies: 1
    Last Post: 10-05-2013, 03:35 PM
  3. Range Select Based on Column Header
    By Nospmas in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2013, 11:27 AM
  4. VBA to select data range, and sort by a column
    By Pete123abc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2011, 03:28 PM
  5. Find last numerical value in row range and return the column header
    By Fidd$ in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-04-2011, 10:15 AM

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