VBA Sorting Column Issue

mrstats_2288

New Member
Joined
Jan 19, 2022
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
I am trying to figure out a way to sort certain sheets by column K and certain sheets by column M in Microsoft Excel VBA. I can get all of the column K to work, but not column M. It wants to just take the column K and sort it when I am trying to get it to sort column K, L and then M to show how I came up with that specific figure in the column.

I created this code, but it is only sorting Column K. (Column M is erratic and is not working at all. It is not allowing the data from columns K and L to show that they equal the data in column M).

Sub SortSheets()

'Updateby Extendoffice

Dim WS As Worksheet

mList = "|Sheet12|Sheet15|Sheet18|Sheet21|Sheet24|Sheet27|Sheet30|Sheet33|Sheet36|Sheet39|Sheet4|Sheet42|Sheet45|Sheet48|Sheet51|Sheet54|Sheet57|Sheet60|Sheet63|Sheet66|Sheet69|Sheet7|Sheet72|Sheet75|Sheet78|Sheet81|Sheet84|Sheet91|Sheet92|Sheet93|"

kList = "|Sheet10|Sheet100|Sheet101|Sheet102|Sheet103|Sheet104|Sheet105|Sheet106|Sheet107|Sheet108|Sheet109|Sheet11|Sheet110|Sheet111|Sheet113|Sheet13|Sheet14|Sheet16|Sheet17|Sheet19|Sheet20|Sheet22|Sheet23|Sheet25|Sheet26|Sheet28|Sheet29|Sheet3|Sheet31|Sheet32|Sheet34|Sheet35|Sheet37|Sheet38|Sheet4|Sheet40|Sheet41|Sheet43|Sheet44|Sheet46|Sheet47|Sheet49|Sheet5|Sheet50|Sheet52|Sheet53|Sheet55|Sheet56|Sheet58|Sheet59|Sheet6|Sheet61|Sheet62|Sheet64|Sheet65|Sheet67|Sheet68|Sheet7|Sheet70|Sheet71|Sheet73|Sheet74|Sheet76|Sheet77|Sheet79|Sheet8|Sheet80|Sheet82|Sheet83|Sheet85|Sheet86|Sheet87|Sheet88|Sheet89|Sheet9|Sheet90|Sheet94|Sheet95|Sheet96|Sheet97|Sheet98|Sheet99|"

Selection.Copy

On Error Resume Next

Application.ScreenUpdating = False

For Each WS In Worksheets

WS.Columns("A:K").Sort Key1:=WS.Columns("K"), Order1:=xlDescending

Next WS

ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll

Application.ScreenUpdating = True

For Each WS In Worksheets

WS.Columns("A:M").Sort Key1:=WS.Columns("M"), Order1:=xlDescending

Next WS

ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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