custom sort

charly1

Board Regular
Joined
Jul 18, 2023
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hi

I have 2 user form list boxes each containing two columns of data. The first columns of both list boxes are identically, albeit sorted differently.

How can I write a code that would sort the second list box based on its first column, so that it matches up to the sort order of the first list box.

Thank you so much in advance for any help.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi

I have 2 user form list boxes each containing two columns of data. The first columns of both list boxes are identically, albeit sorted differently.

How can I write a code that would sort the second list box based on its first column, so that it matches up to the sort order of the first list box.

Thank you so much in advance for any help.
Does the data come from a worksheet?
 
Upvote 0
Yes. the first columns in the list boxes correspond to the first column of a data table, which contain unique reference numbers. The other column in each of the two list boxes are two other columns in the same table.
 
Upvote 0
Yes. the first columns in the list boxes correspond to the first column of a data table, which contain unique reference numbers. The other column in each of the two list boxes are two other columns in the same table.
Are you able to sort the data in the table and then repopulate the listboxes?
 
Upvote 0
yes.

[I was thinking of somehow copying the first column of list box 1 to list box 2, and then using some sort of lookup function to repopulate the second list box with the required column of data from the table].
 
Upvote 0
yes.

[I was thinking of somehow copying the first column of list box 1 to list box 2, and then using some sort of lookup function to repopulate the second list box with the required column of data from the table].
Can you :

1. Use XL2BB to post the table data.

2. VBA Quick Wrap selection to post the code that populates the listboxes.
 
Upvote 0
1. The table has more than 3000 cells.
I have posted a sample below.
מספרתוך ספרעמודןמדףספרחלקיםנושא כללינושא פרטינושא שלישינושא כללי שנינושא פרטי שנינושא שלישי שניתוכןמחברפרטי המחברBlank DataBlank PlaceBoth BlankSumShelfColumn1
1-כט7תתן אמת ליעקב1הלכהנושאים שוניםשונים---ליקוטים בעניני אמת ושקר.ר' יעקב יחזקאל פיש-כט - 7כט - 7
2-כט2תתאה ועילאה1הלכהיו"ד: פ"ז-קי"א [תערובות ובב"ח], יו"ד: קכ"ג-קנ"ח\קע"ח-קפ"ב [עכו"ם]יו"ד---דיני בישול ובליעות בכלים.ר' משה ישראל רבינוביץאחרוני זמנינוכט - 2כט - 2
3-תשעה ספרי מוסר1מחשבה_ומוסר-מחשבה_ומוסר---כולל ספרי: אמרות טהורות, מבחר פנינים, החסיד יוסף יעב"ץ, איגרת הטיול, ארחות חיים, מעלות התורה, כתר תורה, ראש הגבעה, מנחת שמואל.--Missing DataMissing Data
4שבעה ספריםלו8תשועות חיים0תורה-תורה---ליקוטים מחז"ל מיוסד עפרש"י, ביאורי המקראות עפ"י הראשונים, ופרטי שמות היוצאים מראשי וסופי תיבות הפסוקים.ר' חיים ישעיה הכהן האלברסברגאחרונים (ה' תרד-ה' תרע / 1844-1910)שבעה ספרים [לו - 8]תוך
5ספרי בעל הפרדסלו7תשובת מהרא"ל0שו_ת-שו"ת----ר' אריה ליב עפשטייןגדולי האחרונים (ה' תסח-ה' תקלה / 1708-1775)ספרי בעל הפרדס [לו - 7]תוך


2.
VBA Code:
Dim Ary As Variant, Rws As Variant
   Dim TBVal As String
   Dim Ws As Worksheet

  
  
   Set Ws = Sheets("îôúç")
   TBVal = TextBox1.Value
   With Ws.ListObjects("Table1").DataBodyRange
      If TBVal = "" Then
         Ary = Ws.Evaluate("choosecols(" & .Address & " ,1 ,5, 7, 14, 20)")
      Else
         TBVal = Replace(TBVal, Chr(34), Chr(34) & Chr(34))
         Rws = Filter(Ws.Evaluate(Replace("transpose(if(isnumber(search(""" & TBVal & """,@)),row(@)-min(row(@))+1,""X""))", "@", .Columns(5).Address)), "X", False)
         If UBound(Rws) < 0 Then
            Me.ListBox1.List = Array("No matches")
            Exit Sub
         ElseIf UBound(Rws) = 0 Then
            ReDim Preserve Rws(1)
         End If
         Ary = Application.Index(.Value, Application.Transpose(Rws), Array(1, 5, 7, 14, 20))
      End If
   End With
   

   
   With Me.ListBox1
      .ColumnCount = UBound(Ary, 2)
      .List = Ary
   End With
 
Upvote 0
VBA Code:
Private Sub CommandButton2_Click()
Dim searchrange As Range
Set searchrange = Worksheets("מפתח").ListObjects("Table1").ListColumns(1).DataBodyRange
Dim tableData As Range
Set tableData = Worksheets("מפתח").ListObjects("Table1").ListColumns(14).DataBodyRange
Dim Rr As Long
Dim Bb As Long
Bb = (ListBox1.ListCount - 1)
For Rr = 0 To Bb
With ListBox7
.ColumnCount = 2
.AddItem
.List(Rr, 0) = ListBox1.List(Rr, 0)
Dim c As String
c = WorksheetFunction.XLookup(ListBox1.List(Rr, 0), searchrange, tableData)
.List(Rr, 1) = c
End With
Next





End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,347
Messages
6,124,421
Members
449,157
Latest member
mytux

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