VBA code needs refined

farr80

New Member
Joined
Dec 29, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a table with columns A-H. Column H is sorted by fill color red (to top) first. Colum H is then sorted descending second. Column A is then sorted ascending last. My below code is working in this fashion but one thing is really annoying and would like it fixed. Anytime ANY cell is changed, the table refreshes to the top. I can be on row 200 and change a cell from 0 to 4 and it sends me to the top. I then have to scroll all the way down again. Can this be fixed? Any help would be greatly appreciated.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long
LR = Cells(Rows.Count, 1).End(3).Row
With ActiveSheet.Sort
.SortFields.Clear
.SortFields.Add Key:=Range("H3:H" & LR), SortOn:=xlSortOnValues, Order:=xlDescending
.SortFields.Add Key:=Range("A3:A" & LR), SortOn:=xlSortOnValues, Order:=xlAscending
.SetRange Range("A3:H" & LR)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
End Sub
Invscreenshot.png
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You are sorting everytime because you have the macro behind a change event without any conditions.

Maybe this is enough for you.

VBA Code:
Sub jec()
 With Sheets(1).ListObjects(1)
   .Range.Sort .Range(1, 8), 2, .Range(1, 1), , 1, , , 1
 End With
End Sub
 
Upvote 0
Sub jec() With Sheets(1).ListObjects(1) .Range.Sort .Range(1, 8), 2, .Range(1, 1), , 1, , , 1 End With End Sub
Appreciate your response and apologize as I am not a coding guy at all. I learn by google and youtube. I cleared out my current code and entered your suggestion but no sorting occured at all. I have spent probably 13 hrs on just this issue alone. I hate to lose. Lol.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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