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
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,220
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top