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
Bookmarks