Hi,
I would like to sort my excel file based on the value in cell BW1.
- If this cell is empty, I want the rows to be sorted based on column N from highest to lowest.
- If the cell is non-empty, I want to use the value of this cell to exclude all rows that have a value that is smaller than the value in cell BW1. In order to achieve this the value of a specific cell in column "M" must be compared to the value of cell BW1. Afterwards, we sort again the file based on column N from highest to lowest
I have written the following code
However, this macro hides all rows when the cell is non-empty. Help is much appreciated!!
I would like to sort my excel file based on the value in cell BW1.
- If this cell is empty, I want the rows to be sorted based on column N from highest to lowest.
- If the cell is non-empty, I want to use the value of this cell to exclude all rows that have a value that is smaller than the value in cell BW1. In order to achieve this the value of a specific cell in column "M" must be compared to the value of cell BW1. Afterwards, we sort again the file based on column N from highest to lowest
I have written the following code
Code:
Application.ScreenUpdating = FalseDim LastCellData
Dim LastRow
Sheets("Data").Select
Range("A2").Select
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
ActiveWorkbook.Worksheets("Data").AutoFilter.Sort.SortFields.Clear
If Range("BW1").Value = "" Then
Sheets("Data").Select
ActiveWorkbook.Worksheets("Data").AutoFilter.Sort.SortFields.Add Key:=Range( _
"N1:N & LastRow"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Data").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Else
Sheets("Data").Select
ActiveSheet.Range("A1:BV" & LastRow).AutoFilter Field:=20, Criteria1:=">=" & Range("BW1").Value _
, Operator:=xlAnd
'The above command doesn't work properly
ActiveWorkbook.Worksheets("Data").AutoFilter.Sort.SortFields.Add Key:=Range( _
"N1:N & LastRow"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Data").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
Sheets("Overview").Select
Application.ScreenUpdating = True
However, this macro hides all rows when the cell is non-empty. Help is much appreciated!!