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,081,450
Messages
5,358,745
Members
400,511
Latest member
kingahatsi

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top