Hi,
I have a macro that is saved in a 'Personal' sheet which is linked to a ribbon button and available on any spreadsheet.
The macro runs great except for the section that 'sorts' a range.
Full code;
Sub Critical_Clients_Sort()
Dim strSheetName As String
Dim fName As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim mystring As String
Dim count1 As Integer
Dim count2 As Integer
Dim cell As Range
If ActiveSheet.Name = "(Enter Sheet Name)" Then
fName = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
If fName = "False" Then
MsgBox "File not saved, exiting function", vbOKOnly
Cancel = True
Exit Sub
End If
End If
If Sheet1.Name <> "(Enter Sheet Name)" Then
Application.ScreenUpdating = False
strSheetName = ActiveSheet.Name
With ActiveWorkbook
ActiveSheet.Name = Left(.Name, Len(.Name) - 5)
End With
Range("D1").Select
Selection.ClearContents
Columns("D:D").ColumnWidth = 0.92
Range("E:E,G:H,K:Q").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Cut
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Set rng1 = Range(Range("A2"), Range("A2").End(xlDown))
Set rng2 = rng1.Offset(0, 2)
Set rng3 = Range(Range("A2:G2"), Range("A2:G2").End(xlDown))
count1 = 0
count2 = 0
If Range("A2") <> "" Then
For Each cell In rng2
If cell.Value <> "" And InStr(cell.Value, ".") = 0 Then
cell.Value = cell.Value & "."
End If
Next
For Each cell In rng1
If cell.Value = "[Unknown]" And cell.Offset(0, 2).Value <> "" Then
rng4 = cell.Offset(0, 2)
cell.Value = Left(rng4, InStr(1, rng4, ".") - 1)
cell = UCase(cell)
count1 = count1 + 1
End If
If cell.Value = "[Unknown]" And cell.Offset(0, 2).Value = "" Then
count2 = count2 + 1
End If
Next
End If
rng3.Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:= _
Range("A2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
rng1.Select
With Selection
' .SetRange rng1
' .Header = xlNo
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A:G").Select
Range("A:G").EntireColumn.AutoFit
Application.ScreenUpdating = True
Range("A2").Select
MsgBox ("Number of [Unknown] corrected: " & count1 & vbCrLf & "Number of [Unknown] not corrected: " & count2)
End If
End Sub
The section I am having issues with is;
rng1.Select
With Selection
' .SetRange rng1
' .Header = xlNo
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The error that appears is;
Run-time error '438':
Object doesn't support this property or method.
When I 'debug' the first highlighted line that it has a problem with is '.Header'
I have tried 'No', 'Yes' and 'Guess' for the .Header line but neither works.
Bookmarks