How to automatically sort the order of the data in worksheet 2 according to the numbers entered in C4 in worksheet 1.
i.e.
if enter 1 in C4 then sort by name
if enter 2 in C4 then sort by student ID
if enter 3 in C4 then sort by score
I made some attempts, but there have some problem, it can't sort on the fly after I type the numbers, it needs to manually execute the macro every time to work.
This is what worksheet 1 looks like
This is what worksheet 2 looks like
Column A is name, Column B is student ID, Cloumn C is score.
i.e.
if enter 1 in C4 then sort by name
if enter 2 in C4 then sort by student ID
if enter 3 in C4 then sort by score
I made some attempts, but there have some problem, it can't sort on the fly after I type the numbers, it needs to manually execute the macro every time to work.
VBA Code:
Sub Sort()
If Worksheets("Worksheets 1").Cells(4, 3).Value = 1 Then
Range("Worksheets 2").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo
ElseIf Worksheets("Worksheets 1").Cells(4, 3).Value = 2 Then
Range("Worksheets 2").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlNo
ElseIf Worksheets("Worksheets 1").Cells(4, 3).Value = 3 Then
Range("Worksheets 2").Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlNo
End If
End Sub
This is what worksheet 1 looks like
This is what worksheet 2 looks like
Column A is name, Column B is student ID, Cloumn C is score.