Microsoft excel sorting tables in different sheets having different numbers of columns

jenniferRI

New Member
Joined
Jan 11, 2022
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,
I have 4 different tables in 4 sheets, they have in common the first 4 columns but each table has a different nbre of columns.
I want to sort all the tables after inserting a new row in Table1 (sheet1) according to 2 conditions: the values in a column that is only present in Table1 and the values in a shared column between all tables.
Can anyone help me in this regards,
Thank you in advance,
Jennifer
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
not every much info, so an array with the names for those 4 sheets and hopefully the 1st list in that shee.
The sort-key is purely guessing, so if you give more info ....
VBA Code:
Sub SortSheets()
     For Each sh In Array("sheet1", "sheet2", "sheet3", "sheet4")
          With Sheets(CStr(sh)).ListObjects(1).Range
               .Sort .Range("A1"), xlDescending, Header:=True
          End With
     Next
End Sub
 
Upvote 0
I want to sort table 1 and table 2 according to the values of the column "Included In registry" in table 1 and in ascending order of the Patient ID, and as you can notice the values of "Base completed" column in table 1 are automatically filled based on the values entered in "Base complete" column in table 2.

As example: If I add a new Patient in table1 with ID = 16945 and "Included in registry" field is equal to Y it should be added in both tables after patient 16942

and if I add a new Patient in table1 with ID = 16941 and "Included in registry" field is equal to N it should be added in both tables before patient 16943

the same idea for all the other tables, I only showed you an example of 2 tables.

I hope it is explained better now,
Thank you again,
 

Attachments

  • Table1.PNG
    Table1.PNG
    35.7 KB · Views: 5
  • Table2_Base.PNG
    Table2_Base.PNG
    22.9 KB · Views: 5
Upvote 0
the basic idea with sorting on "Base Completed" and "Follow up Completed"
So you can adopt it to your situation
VBA Code:
Sub SortSheets()
     For Each sh In Array("sheet1", "sheet2", "sheet3", "sheet4")
          Set lo = Sheets(CStr(sh)).ListObjects(1)

          i1 = 0: i2 = 0
          On Error Resume Next
          Set c1 = lo.ListColumns("Based Completed").Range      'this listcolumn
          Set c2 = lo.ListColumns("Follow up Completed").Range
          i1 = c1.Column - lo.Range.Column + 1                  'indexnumber of that column in the listobject
          i2 = c2.Column - lo.Range.Column + 1
          On Error GoTo 0

          With lo.Range
               If i1 = 0 Then                                   'at least "Base completed" must be present, otherwise end of sort
                    MsgBox "no column base completed !!!"
               Else
                    If i2 > 0 Then                              ' follow up completed is also present
                         .Sort .Cells(1, i2), xlAscending, , .Cells(1, i1), xlAscending, Header:=xlYes     ' sort with both columns
                    Else
                         .Sort .Cells(1, i1), xlAscending, Header:=xlYes     'sort with only 1 column
                    End If
               End If

          End With
     Next
End Sub
 
Last edited:
Upvote 0
additional "i1 = 0: i2 = 0", to reset both parameters at the beginning of the next loop
 
Upvote 0

Forum statistics

Threads
1,203,060
Messages
6,053,305
Members
444,651
Latest member
markkuznetsov1

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