In the VBA loop, find the last row of the table and bring to the top.

CBM2020

New Member
Joined
May 30, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi VBA Friends, I need your help again.

The below VBA loop help me to sort multiple columns for multiple tables in multiple sheets, and it works perfectly.
Now after these actions, for each table, I need to find the last row of all these tables and bring to the top row (first row after header) of the table (cut and paste). I tried few times and its not working.

Many thanks in advance!

VBA Code:
Public Sub Sort_Tables()

Dim sheetsTables As Variant
Dim i As Long

sheetsTables = Split("Jan22 tblMth01 Feb22 tblMth02 Mar22 tblMth03 Apr22 tblMth04 May22 tblMth05 Jun22 tblMth06 Jul22 tblMth07 Aug22 tblMth08 Sep22 tblMth09 Oct22 tblMth10 Nov22 tblMth11 Dec22 tblMth12")

For i = 0 To UBound(sheetsTables) Step 2

With ActiveWorkbook.Worksheets(sheetsTables(i)).ListObjects(sheetsTables(i + 1)).Sort
.SortFields.Clear
.SortFields.Add2 Key:=Range(sheetsTables(i + 1) & "[PSM]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add2 Key:=Range(sheetsTables(i + 1) & "[Type]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add2 Key:=Range(sheetsTables(i + 1) & "[Client]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add2 Key:=Range(sheetsTables(i + 1) & "[Event]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add2 Key:=Range(sheetsTables(i + 1) & "[Date End]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Next

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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