Hello,
I've been trying to figure out a way to turn a number of If statements into an efficient loop, but haven't been able to figure it out. What the code does is if a cell is clicked, it will filter rows out of the table if cells in that column have blanks. It will then sort the remaining data largest to smallest. Using if statements, this was very easy to achieve with the below code:
If Not Intersect(Target, Range("H6")) Is Nothing Then
On Error Resume Next
CuCell = ActiveCell
ActiveSheet.ShowAllData
Range("$H$6:$H$500").AutoFilter Field:=3, Criteria1:="<>", Visibledropdown:=False
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("H7:H500"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortTextAsNumbers
With ActiveSheet.Sort
.SetRange Range("C6:X500")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
If Not Intersect(Target, Range("I6")) Is Nothing Then
On Error Resume Next
ActiveSheet.ShowAllData
Range("$I$6:$I$500").AutoFilter Field:=4, Criteria1:="<>", Visibledropdown:=False
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("I7:I500"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortTextAsNumbers
With ActiveSheet.Sort
.SetRange Range("C6:X500")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
Display More
I'm performing an if statement for all cells from H6:X6 however and would rather loop the request. the problem is the different variables needed for the loop:
- I need to dynamically account for the vertical range in question
- I need to account for the field in the filter call
I've been browsing this forum for some time, but haven't had much luck. Any guidance would be appreciated.