Link to home
Start Free TrialLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

sorting listbox with additional information from current sheet

Excel vba:

The code below is some standard internet code to sorting a listbox asc or desc..numeric or alphabetically.

This code is executed by:
Run "SortListBox", ListBox1, 6, 1, 1

What I need:

I need to see if the code can look at the current spreadhsheet and find the current activecell...
Look one cell to the left -1.
Take that value  and see if it exists in Column  3 of the listbox.
(This value may appear in more than one row  in the listbox...)

If results are found those rows get bumped to the top of the list.
and then the rest of the rows are still sorted the same way as requested. beneath the rows that get bumped to to the top.
 

Thanks
fordraiders

Sub SortListBox(oLb As MSForms.ListBox, sCol As Integer, sType As Integer, sDir As Integer)

    Dim vaItems As Variant
    Dim i As Long, j As Long
    Dim c As Integer
    Dim vTemp As Variant
    
    'Put the items in a variant array
  ' On Error GoTo SortListBox_Error
On Error Resume Next
    vaItems = oLb.List
    
    'Sort the Array Alphabetically(1)
    If sType = 1 Then
        For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
            For j = i + 1 To UBound(vaItems, 1)
                'Sort Ascending (1)
                If sDir = 1 Then
'                   If vaItems(i, sCol) > vaItems(j, sCol) Then
                    If IIf(Len(Trim$(IIf(IsNull(vaItems(i, sCol)), "", vaItems(i, sCol)))) = 0, String$(255, Chr$(255)), vaItems(i, sCol)) > _
                       IIf(Len(Trim$(IIf(IsNull(vaItems(j, sCol)), "", vaItems(j, sCol)))) = 0, String$(255, Chr$(255)), vaItems(j, sCol)) Then
                        For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
                            vTemp = vaItems(i, c)
                            vaItems(i, c) = vaItems(j, c)
                            vaItems(j, c) = vTemp
                        Next c
                    End If

                'Sort Descending (2)
                ElseIf sDir = 2 Then
                    If vaItems(i, sCol) < vaItems(j, sCol) Then
                        For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
                            vTemp = vaItems(i, c)
                            vaItems(i, c) = vaItems(j, c)
                            vaItems(j, c) = vTemp
                        Next c
                    End If
                End If
    
            Next j
        Next i
    'Sort the Array Numerically(2)
    '(Substitute CInt with another conversion type (CLng, CDec, etc.) depending on type of numbers in the column)
    ElseIf sType = 2 Then
        For i = LBound(vaItems, 1) To UBound(vaItems, 1) - 1
            For j = i + 1 To UBound(vaItems, 1)
                'Sort Ascending (1)
                If sDir = 1 Then
'                   If CInt(vaItems(i, sCol)) > CInt(vaItems(j, sCol)) Then
                    If CInt(IIf(Len(Trim$(IIf(IsNull(vaItems(i, sCol)), "", vaItems(i, sCol)))) = 0, 32767, vaItems(i, sCol))) > _
                       CInt(IIf(Len(Trim$(IIf(IsNull(vaItems(j, sCol)), "", vaItems(j, sCol)))) = 0, 32767, vaItems(j, sCol))) Then
                        For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
'                           vTemp = vaItems(i, c)
'                           vaItems(i, c) = vaItems(j, c)
                            vTemp = IIf(Len(Trim$(IIf(IsNull(vaItems(i, c)), "", vaItems(i, c)))) = 0, "", vaItems(i, c))
                            vaItems(i, c) = IIf(Len(Trim$(IIf(IsNull(vaItems(j, c)), "", vaItems(j, c)))) = 0, "", vaItems(j, c))
                            vaItems(j, c) = vTemp
                        Next c
                    End If

                'Sort Descending (2)
                ElseIf sDir = 2 Then
'                   If CInt(vaItems(i, sCol)) < CInt(vaItems(j, sCol)) Then
                    If CInt(IIf(Len(Trim$(IIf(IsNull(vaItems(i, sCol)), "", vaItems(i, sCol)))) = 0, "0", vaItems(i, sCol))) < _
                       CInt(IIf(Len(Trim$(IIf(IsNull(vaItems(j, sCol)), "", vaItems(j, sCol)))) = 0, "0", vaItems(j, sCol))) Then
                        For c = 0 To oLb.ColumnCount - 1 'Allows sorting of multi-column ListBoxes
'                           vTemp = vaItems(i, c)
'                           vaItems(i, c) = vaItems(j, c)
                            vTemp = IIf(Len(Trim$(IIf(IsNull(vaItems(i, c)), "", vaItems(i, c)))) = 0, "", vaItems(i, c))
                            vaItems(i, c) = IIf(Len(Trim$(IIf(IsNull(vaItems(j, c)), "", vaItems(j, c)))) = 0, "", vaItems(j, c))
                            vaItems(j, c) = vTemp
                        Next c
                    End If
                End If
    
            Next j
        Next i
    End If
    
    'Set the list to the array
    oLb.List = vaItems
    
  Exit Sub
  
SortListBox_Error:'
  
End Sub

Open in new window

Avatar of [ fanpages ]
[ fanpages ]

(cough)

The code below is some standard internet code to sorting a listbox asc or desc..numeric or alphabetically.

With some "non-standard" code I added because of your previous question:

"Sorting a listbox via code Update...not working with columns with null/blanks"
[ https://www.experts-exchange.com/questions/28235471/Sorting-a-listbox-via-code-Update-not-working-with-columns-with-null-blanks.html?anchorAnswerId=39481974#a39481974 ]

This aside...

Do you have a sample workbook that you can upload/attach to this thread that contains test data, or do you need me to re-post the sample workbook I provided in the previous question?

[EDIT: 26/09/2013 09:20 (UK time) to add previous sample workbook ~ fp.]
Q-28235471.xlsm
Avatar of Fordraiders

ASKER

if you could repost the sample workbook. That would be great. and sorry for the non-recognition of your work. did not mean to offend.
Yes, that's right.

I have updated my previous comment to include the sample workbook from that thread in case anybody else wishes to look at the previous question for background.

Having reminded myself of that question though, it may be as easy to start again with test data to address this question.
fanpages, here is the file
Q-28235471-REVISED-0926.xlsm
fanpages, is this data sufficient?
Hi again,

I may be getting confused, or misunderstanding the purpose of your workbook.

The Listbox within your attached file only has two columns, and the statement you posted above (Run "SortListBox", ListBox1, 6, 1, 1) seems to indicate you have (at least) six columns.

The worksheet [Sheet1] you provided seems to include the transposed data from five columns (in the range [C33:G38]).  Again, not six.

I also do not understand your opening statement; "always the current cell will be the value in red but in different cells on the spreadsheet and a different column".

Do you mean the ActiveCell (the currently selected cell), or do you manually mark a cell in red to make it the "current cell"?


Either I am missing something like the Listbox &/or the data you are using, or there is something missing from your question.

This statement also puzzles me:
"AND THEN SORT THE LISTBOX IN COLUMN 3 BASED ON THIS VALUE..ANY VALUE BELOW  THEN GETS SORTED BY COLUMN 3 ASC OR DESC DEPENDS ON HOW SPECIFIED
IF THE VALUE DOES NOT EXIST…THEN NO SORT."

Do you mean you simply select a cell ([D19] = 11221), take the associated value one cell to the left ("BARNES", in the example within the worksheet), then re-order the Listbox so all the rows with "BARNES" in column 3 are displayed at the top, & then the rest of the rows (not "BARNES") are sorted alphabetically (ascending) on column 3?

Should there be any other sort criteria?  For example, in the range [C33:G38] you also seem to be sorting on the first column [C], although your closing statement seems to indicate it is column [G] (the fifth column):

"IF AT ALL POSSIBLE SORTING NEEDS TO OCCUE ON 2 COLUMNS  3  AND 5 ASC…"


Please can you confirm what is in the Listbox you are using, the total number of columns, & what you mean by "current cell" (in that, is it the current selection on the worksheet)?

Also, how is the data arranged in the worksheet where the selection occurs?

Is there any chance you can provide an example workbook that contains the data layout as you are using it; the actual workbook (not a workbook with question text) that includes some "test" data?  This need not be actual data, but simply to demonstrate a sample so I can copy the same format to expand for extended testing?

Thanks.

BFN,

fp.
fanpages,
 sorry for the confusion...

"Do you mean you simply select a cell ([D19] = 11221), take the associated value one cell to the left ("BARNES", in the example within the worksheet), then re-order the Listbox so all the rows with "BARNES" in column 3 are displayed at the top, & then the rest of the rows (not "BARNES") are sorted alphabetically (ascending) on column 3?"

Yes. this is exactly what i'am trying to do.

"Do you mean the ActiveCell (the currently selected cell), or do you manually mark a cell in red to make it the "current cell"?

Just the ActiveCell (the currently selected cell), . nothing manually marked in red.

===================================================
and forget this statement .  it is not necessary.

"Should there be any other sort criteria?  For example, in the range [C33:G38] you also seem to be sorting on the first column [C], although your closing statement seems to indicate it is column [G] (the fifth column):
"IF AT ALL POSSIBLE SORTING NEEDS TO OCCUE ON 2 COLUMNS  3  AND 5 ASC…"
===================================================


Hope this revision makes sense.
Q-28235471-REVISED2-0930.xlsm
Thanks.  Yes, that is much clearer.

Two final queries (sorry):

1) I presume you can select any cell on the rows where data resides.  The code should simply look in column [F] for the "Mfgname" to use for the preferred sort criteria.

2) Do you wish the list to be re-sorted within the existing worksheet?  Or do you still wish to use the list-box approach?  I wasn't clear whether you left my example list-box in the worksheet just because you didn't know how to remove it, or whether you did actually want the sorted data to be shown in a similar way?

Thanks again.
1) I presume you can select any cell on the rows where data resides.  The code should simply look in column [F] for the "Mfgname" to use for the preferred sort criteria

No , on my next project "Mfgname" could be inj Column [AA], or Column [H].
Whiach is why the stress on the Activcell...nine times out of ten...the cell immediatelt ot the left will be the mfgname column.


2) Do you wish the list to be re-sorted within the existing worksheet?  Or do you still wish to use the list-box approach?  I wasn't clear whether you left my example list-box in the worksheet just because you didn't know how to remove it, or whether you did actually want the sorted data to be shown in a similar way?

Do you wish the list to be re-sorted within the existing worksheet?  Or do you still wish to use the list-box approach?

2)a. Sorry but list-box approach only...

Nothong is to be written to the sheet , sorted and then presented to the listbox.

The "listbox data" comes straight from the recordset and getrows method.

Thanks
very very much !!
:)

OK.  I'll just go with the ActiveCell/Offset one column to the left approach.

However, I am now confused again!

I seem to be missing a link between the worksheet & the listbox being displayed.

The "listbox data" comes straight from the recordset and getrows method.

How is the data in the listbox/userform shown?  Do you click a button, is it when a cell is selected, or by some other manual (or automatic) process?

Are you using the UserForm (I provided, & that which is currently shown when the workbook opens) to show the listbox?

As you know, presently there are only two columns in the listbox I previous provided.  Do you now need me to create at least six columns of random data (but ensuring that there are some examples of the "Mfgname" you have provided to prove the revised code is working as expected?

...or...

Is the worksheet data you have provided going to be the test data in the listbox?


Obviously the UserForm width will need to be increased to accommodate these additional columns.  If you have an existing UserForm that you are displaying your data within shall I take a copy from you so I can apply the code directly to that?


Sorry if this is obvious, but you, of course, have your idea of the finished product &/or something resembling that already... & I think I only have part of the data & nothing representing the final presentation.

It is difficult to appreciate how all this fits together & if I can anticipate any issues that may arise & code "defensively" for these.

I am concerned that you are seeing one finished product, & I am assuming another, & that you may be disappointed with the outcome.

Thanks for any further information you can provide to describe the ultimate goal.
yes, and i apologize, I'm loading the listbox via against a recordset from access and using the   .getrows  method to load the listbox

The data in the spreadsheet is an example of what can be loaded into the listbox.

That would be great to just randomly load data into your new 6 column listbox and have it sorted as requested by the mfgname (ActiveCell/Offset one column to the left approach)

Then sorted by the mfgname after

"Do you now need me to create at least six columns of random data (but ensuring that there are some examples of the "Mfgname" you have provided to prove the revised code is working as expected?

yes.  That would be great.

Thanks again
fordraiders






I was displaying it on the sheet to give an example of what the data looks  like in the listbox.


 Arr = rst.GetRows
       rst.MoveFirst
i = 0



With frmResultAll.ListBox1
   .Clear
        .ColumnHeads = False
        .ColumnCount = rst.Fields.Count
.Column = Arr
        .ListIndex = -1
End With
example-listbox-and-sheet-mfgnam.bmp
:)

Thank you.  I'm almost there with the code.

I just have one final question:

If sorting in an ascending order, the selected "Mfgname" (e.g. "BARNES") will be at the top of the list(box) & all others will follow in ascending alphabetical order.

However, if sorting in a descending order, which condition should occur?

a) "BARNES" at the top of the list(box) & all others follow in a descending order,
or
a) All others (except "BARNES") listed in a descending order, & then all the "BARNES" entries at the very end?


Thanks for your patience so far :)

PS. I also noticed that throughout I have been referring to six columns & your opening question text states:

Run "SortListBox", ListBox1, 6, 1, 1

The 6 here indicates seven columns (0 relative; 0, 1, 2, 3, 4, 5, & 6).

What I've done is added a seventh column that includes a number; the original sequential order of the rows in the listbox.  I have also added a sort option (in the drop-down combobox) that allows the sorting of this seventh column in an ascending or descending order, so you can always return to the original order of all the items in the list if you wish to.
fanpages, Thanks for hanging with this.

If my folks want to get rid of the intial sort after displaying a name bubbled to the top.

Its totally up to them.

I'am not worried about anything after the results are brought back.

If they want to kill that presorted mfgname bubbled to the top...its up to them.
dont worry about that part.
But Thanks.


Great, Thanks for adding the extra column...

and i guess you have allowed for a null or blank cell if its exists to the left of the active cell. Because a Mfgname will not always be there. ?




fordraiders
ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
fanpages, Thanks.
maybe i'm not doing something correctly.
The from is not modeless. So i cant navigate on the sheet...So i added vbmodeless.


If i'am in Column G and the mfgname to the left of the active cell is "Barnes"
After selecting a sort order and clicking the "Sort" Button...the Barnes mfgnames...rows do not filter to the top...
Sorry.

or maybe i'm doing something wrong ?
maybe i'm not doing something correctly.
The from is not modeless. So i cant navigate on the sheet...So i added vbmodeless.

Sorry, you never asked for it to be modeless.

Please click [Close] to select another row from the worksheet.

If i'am in Column G and the mfgname to the left of the active cell is "Barnes"
After selecting a sort order and clicking the "Sort" Button...the Barnes mfgnames...rows do not filter to the top...

I suspect this is linked to not closing the form & your own change.

Please remove the vbModeless option, & use by closing the UserForm between selections, & I think you'll find it does function as expected.
ok did as you asked.


ok, the "selection change event" seems to not be firing off ?
fanpages-test1.bmp
ok...i redownloaded..

form is popping up now..(deal with modelesss later on my own)

As i go from row to row...form opens...
i then Closing the form after every row selection,

When the form pops up.. I hit the "sort" button,. but the mfgname showing in the blue form region...does not bubble to the top ?
aaaah i see what you did.... very very very very nice...
way above and beyond...this is really great !!!!!

got it to working now..
I see the selections at the bottom of the drop down list...
wow...wish i could offer more than 500. Points...I hope they change that one day for people who pay monthly premiums.

Very Nicely done...
No, thank you.

I enjoy the "out of the ordinary" requests, & new challenges.

There are only so many questions of a similar nature you can answer!

Glad I can help :)

BFN,

fp.

PS. I am not looking for additional points, but if you ever wish to award more to any "Expert", you can simply create a new question with, for instance, the title "Points for fanpages", describe why you are awarding additional points, & then just wait for the specific "Expert" to comment so you can select that as the solution.

For example:
[ https://www.experts-exchange.com/searchResults.jsp?searchTerms=POINTS+FOR+FANPAGES&searchSubmit= ]
Thanks again very much !
just sent additional question for you for extra points ..
It appears doing this is no longer accepted.

Sorry if I embarrassed you, fordraiders.