Michelle Lea
New Member
- Joined
- May 15, 2023
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
I have the following code to run a macro that sorts column A after I hit the Enter key and keep the modified row selected. However, it also runs when I hit the Tab key. I don't want it to. Is there code that would run the macro when hitting Enter ONLY?
Thank you!
Private Sub Worksheet_Change(ByVal Target As Range)
'If Target.Column <> 8 Then Exit Sub
' Sort_After_Enter Macro
Dim lngR As Long
Dim strV As String
If Target.Cells.Count > 8 Then Exit Sub
strV = Target.Value
Application.EnableEvents = False
With Me.ListObjects("ContactsTable").Sort
.SortFields.Clear
.SortFields.Add2 Key:=Me.Range("ContactsTable[[#All],[COMPANY (Contact)]]"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
Target.EntireColumn.Find(strV, LookIn:=xlValues, LookAt:=xlWhole).Select
ActiveCell.Offset(0, 1).Activate
Application.EnableEvents = True
End Sub
-----
Sub ResetEvents()
Application.EnableEvents = True
End Sub
Thank you!
Private Sub Worksheet_Change(ByVal Target As Range)
'If Target.Column <> 8 Then Exit Sub
' Sort_After_Enter Macro
Dim lngR As Long
Dim strV As String
If Target.Cells.Count > 8 Then Exit Sub
strV = Target.Value
Application.EnableEvents = False
With Me.ListObjects("ContactsTable").Sort
.SortFields.Clear
.SortFields.Add2 Key:=Me.Range("ContactsTable[[#All],[COMPANY (Contact)]]"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
Target.EntireColumn.Find(strV, LookIn:=xlValues, LookAt:=xlWhole).Select
ActiveCell.Offset(0, 1).Activate
Application.EnableEvents = True
End Sub
-----
Sub ResetEvents()
Application.EnableEvents = True
End Sub