Automatically Update Rows with VBA

THE19

New Member
Joined
Jun 8, 2016
Messages
1
Hello everyone,

I am making a list with a VBA Form to input information into different categories (columns).
In addition, I want the list to update automatically in alphabetic order firstly based on the entry in column A, and thereafter on the entry in Column B. I am currently trying to modify a piece of VBA code found on another forum (see below), but failing miserably as this is the first time I have worked in VBA.

Private Sub Worksheet_Change(ByVal Target As Range)​
On Error Resume Next​
If Not Intersect(Target, Range("A:A")) Is Nothing Then​
Range("A2").Sort Key1:=Range("A3"),_​
Order1:=xlAscending, Header:=xlYes, _​
OrderCustom:=1, MatchCase:=False, _​
Orientation:=xlTopToBottom​
End If​
End Sub​

Two issues: 1. Only the the input in Column A is sorted for, meaning that the corresponding input in other columns but for the same row are "lost" and do not move with the input in column A. 2. It (obviously) does not sort for the input in Column A first and thereafter the input in Column B.

Can anybody modify the code or maybe come up with a better solution?
Much appreciated /THE19
 

wideboydixon

Well-known Member
Joined
Jun 2, 2016
Messages
3,401
Firstly, you need to sort the whole range e.g.

Code:
Range("A:B").Sort ... ' Sort all of columns A and B
Range("A1:D20").Sort ... ' Sort a 4 x 20 grid
Range("A2:E99").Sort ... ' Sorting starting at A2
Secondly, you have to add extra keys to the sort:

Code:
Range("A2:E99").Sort Key1:=Range("A3"), Order1:=xlAscending, Key2:=Range("B3"), Order2:=xlAscending, Header:=xlYes
WBD
 

Forum statistics

Threads
1,085,495
Messages
5,384,007
Members
401,871
Latest member
allemandi

Some videos you may like

This Week's Hot Topics

Top