Rahul87
New Member
- Joined
- Apr 7, 2023
- Messages
- 18
- Office Version
- 365
- 2021
- 2019
- 2016
- 2013
- 2011
- 2010
- 2007
- 2003 or older
- Platform
- Windows
Dear Experts,
please help me out, Any Help would be highly appreciated, I am very much new to the VBA coding in excel and trying to short out some task in excel. I tried hard and found some codes step by step from internet and tried to achieve some of the task, but still unable to achieve the goal. Please help in achieving my goal. Below is full Description.
I have data in excel like this as in image
I want data to be sorted like below in the Image2 on the basis of Column E and ranking should be given in column H and Rank should starts from 1,2,3........ and if value in column E is 0.0% or null value then it should be given Rank at very last or kept at last of the record.
Below is the code in which I am struggling with, but unable to achieve the above task. Please help me to achieve the goal.
please help me out, Any Help would be highly appreciated, I am very much new to the VBA coding in excel and trying to short out some task in excel. I tried hard and found some codes step by step from internet and tried to achieve some of the task, but still unable to achieve the goal. Please help in achieving my goal. Below is full Description.
I have data in excel like this as in image
I want data to be sorted like below in the Image2 on the basis of Column E and ranking should be given in column H and Rank should starts from 1,2,3........ and if value in column E is 0.0% or null value then it should be given Rank at very last or kept at last of the record.
Below is the code in which I am struggling with, but unable to achieve the above task. Please help me to achieve the goal.
VBA Code:
Sub SortAndRank()
' Define variables
Dim ws As Worksheet
Dim lastRow As Long
Dim sortRange As Range
Dim rankRange As Range
Dim i As Long
Dim nullCount As Long
' Set worksheet object
Set ws = ThisWorkbook.Sheets("Sheet2") ' Update with your sheet name
' Find last row of data in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Set range to sort (include headers)
Set sortRange = ws.Range("A1:H" & lastRow)
' Move null or 0 values in column E to end of sorted range
nullCount = Application.WorksheetFunction.CountIf(sortRange.Columns(5), "<>0") + _
Application.WorksheetFunction.CountIf(sortRange.Columns(5), "")
If nullCount > 0 Then
sortRange.Sort Key1:=sortRange.Columns(5), Order1:=xlAscending, _
Key2:=sortRange.Columns(8), Order2:=xlAscending, Header:=xlYes
End If
' Sort range by column E in descending order
sortRange.Sort Key1:=sortRange.Columns(5), Order1:=xlDescending, _
Key2:=sortRange.Columns(8), Order2:=xlAscending, Header:=xlYes
' Set range to assign ranks (exclude headers)
Set rankRange = ws.Range("H" & nullCount + 2 & ":H" & lastRow)
' Assign ranks and handle null or 0 values in column E
If nullCount > 0 Then
For i = 1 To lastRow - nullCount - 1
If rankRange.Cells(i, 1) = rankRange.Cells(i + 1, 1) Then ' If values in column E are equal
rankRange.Cells(i + 1, 1) = rankRange.Cells(i, 1) ' Set current cell equal to previous cell
Else
rankRange.Cells(i + 1, 1) = i + 1 - nullCount ' Set current cell to next rank
End If
Next i
For i = lastRow - nullCount To lastRow - 1
rankRange.Cells(i + 1, 1) = "" ' Clear any existing rank values
Next i
Else
rankRange.Cells(1, 1) = 1 ' Set first cell in range to 1
For i = 2 To lastRow - 1
If rankRange.Cells(i, 1) = rankRange.Cells(i + 1, 1) Then ' If values in column E are equal
rankRange.Cells(i + 1, 1) = rankRange.Cells(i, 1) ' Set current cell equal to previous cell
Else
rankRange.Cells(i + 1, 1) = i ' Set current cell to next rank
End If
Next i
End If
End Sub