nigelandrewfoster
Well-known Member
- Joined
- May 27, 2009
- Messages
- 747
This fails at the red line. I have several tables of ingredients in INGREDIENTS worksheet(where this code is stored), which is summarised in one long list in TABLES worksheet via formulas for the purpose of providing a listbox to select them, which has to be in alphabetical order. I want the table in TABLES to sort every time the corresponding entry on one of the INGREDIENTS tables is updated. I HAVE to copy the values first to column B as otherwise blank values are displayed first in the listbox, which I don't want. Am I right in saying that the problem is I cannot sort a range in a worksheet that is unselected? I want to get away from selecting a range at all in my programming, but:
doesn't work either. Can you give me some suggestions? There has to be an easier way than this...? And one that works!
Code:
With ws.Range("B1:B280")
.Selection.Sort Key1:=.Range("B2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With
Code:
Private Sub worksheet_change(ByVal Target As Range)
Dim ws As Worksheet
Set ws = Worksheets("TABLES")
If Not Intersect(Target, Range("j5:al96")) Is Nothing Then
ws.Range("A1:A280").Copy
ws.Range("B1").PasteSpecial Paste:=xlPasteValues
ws.Range("B1:B280").Select
[COLOR="Red"]Selection.Sort Key1:=ws.Range("B2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal[/COLOR]
End If
End Sub