filmov
tv
Coding with ChatGPT: Sort Dataset Rows by Multiple Criteria
Показать описание
00:16 Business Scenario
01:04 Write Code in ChatGPT
04:39 Test Code with New Dataset
VBA Code used in this video:
_____________________________________________________
Sub SortDataset()
Dim ws As Worksheet
Dim lastRow As Long
' Set the worksheet object
Set ws = ThisWorkbook.Sheets("employee_records (2)") ' Replace "Sheet1" with your actual sheet name
' Find the last row in the dataset
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Assuming the dataset starts from column A
' Sort the dataset
With ws.Sort
' Set the range to sort (adjust the range as per your dataset)
.SetRange ws.Range("A3:F" & lastRow)
' Set the sort criteria
.SortFields.Clear
.SortFields.Add Key:=ws.Range("D3:D" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ' Sort by Country
.SortFields.Add Key:=ws.Range("E3:E" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ' Sort by Department
.SortFields.Add Key:=ws.Range("F3:F" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ' Sort by Hiring Date
' Set the sort settings
.SetRange ws.Range("A3:F" & lastRow)
.Header = xlYes ' Assuming the first row contains headers
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
_____________________________________________________
Related Links:
******More Videos in Playlists******
#ExcelforHR#HRAnalytics#Excel#HR
01:04 Write Code in ChatGPT
04:39 Test Code with New Dataset
VBA Code used in this video:
_____________________________________________________
Sub SortDataset()
Dim ws As Worksheet
Dim lastRow As Long
' Set the worksheet object
Set ws = ThisWorkbook.Sheets("employee_records (2)") ' Replace "Sheet1" with your actual sheet name
' Find the last row in the dataset
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Assuming the dataset starts from column A
' Sort the dataset
With ws.Sort
' Set the range to sort (adjust the range as per your dataset)
.SetRange ws.Range("A3:F" & lastRow)
' Set the sort criteria
.SortFields.Clear
.SortFields.Add Key:=ws.Range("D3:D" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ' Sort by Country
.SortFields.Add Key:=ws.Range("E3:E" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ' Sort by Department
.SortFields.Add Key:=ws.Range("F3:F" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ' Sort by Hiring Date
' Set the sort settings
.SetRange ws.Range("A3:F" & lastRow)
.Header = xlYes ' Assuming the first row contains headers
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
_____________________________________________________
Related Links:
******More Videos in Playlists******
#ExcelforHR#HRAnalytics#Excel#HR