Problems to Populate a Multiple Listbox with non contiguous columns

purinqui

New Member
Joined
Nov 30, 2019
Messages
29
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone, a big greeting to everyone before anything else.

I am having a problem with the handling of a Multiple Listbox.

I need to populate a Multiple Listbox from a sheet containing 3 columns but I need it to only load the information
column 1 and 3 only, avoiding column 2.

I have made the code work but with continuous columns.
But when I want to load two columns that are not continuous, the code does not allow me to populate the multiplelistbox

This is the sheet I have, and I need only the information in column A and C to be loaded into the MultipleListbox.

Multiple Listbox no continuous columns.PNG


When I run the macro it doesn't load anything in the multiplelistbox.

Multiple Listbox no continuous columns load.PNG


Try to do it with this code but when the multiplelistbox is opened it doesn't load any information:

VBA Code:
Option Explicit
Dim rng1 As Range, rng2 As Range, rng As Range

Private Sub CommandButton1_Click()
rng.Sort rng.Cells(1), 1, Header:=xlNo
End Sub

Private Sub CommandButton2_Click()
rng.Sort rng.Cells(1), 3, Header:=xlNo
End Sub

Private Sub CommandButton3_Click()

With rng.Columns(1)
  .Formula = "=Today() - RandBetween(0, 1100)"
  .Calculate: .Value = .Value
End With

End Sub

Private Sub UserForm_Initialize()

Dim ws As Worksheet

Set ws = Worksheets("Sheet1")


Set rng1 = ws.Range("A1:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
Set rng2 = ws.Range("C1:C" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)

Set rng = Union(rng1, rng2)

CommandButton3_Click
ListBox1.RowSource = rng.Address(external:=True)
End Sub

Note: I use the Union Method to join two non-contiguous columns.

Thank you in advance for any contribution you can give. Regards.

Link of Woorkbook
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You can use this UserForm_Initialize event code to populate the ListBox...
VBA Code:
Private Sub UserForm_Initialize()
  Dim X As Long, Arr As Variant
  Arr = Application.Index(Cells, Evaluate("ROW(1:" & Cells(Rows.Count, "A").End(xlUp).Row & ")"), [{1,3}])
  For X = 1 To UBound(Arr)
    Arr(X, 1) = Format(Arr(X, 1), "m/d/yyyy")
  Next
  ListBox1.List = Arr
End Sub
 
Upvote 0
You can use this UserForm_Initialize event code to populate the ListBox...
VBA Code:
Private Sub UserForm_Initialize()
  Dim X As Long, Arr As Variant
  Arr = Application.Index(Cells, Evaluate("ROW(1:" & Cells(Rows.Count, "A").End(xlUp).Row & ")"), [{1,3}])
  For X = 1 To UBound(Arr)
    Arr(X, 1) = Format(Arr(X, 1), "m/d/yyyy")
  Next
  ListBox1.List = Arr
End Sub


Thank you very much, it works perfectly!!. Now i have the Multiple Listbox Populated.

One more question about it.

I need to sort the Multiple ListBox. (High to Low and Low to High.)

I did this action for sort with this code

VBA Code:
Private Sub UserForm_Initialize()

Dim ws As Worksheet

Set ws = Worksheets("Sheet1")


Set rng1 = ws.Range("A1:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
Set rng2 = ws.Range("C1:C" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)

Set rng = Union(rng1, rng2)

End Sub


Private Sub CommandButton1_Click()
rng.Sort rng.Cells(1), 1, Header:=xlNo
End Sub

But now the vba show me an error when a run it. "This cant be done on a multiple range selection, select a single range and try again".

How can i sort this multiple listbox taking into account one of the columns has dates as information.

Thank you very much. Regard
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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