Populate Listbox from range in reverse order

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to populate a userform Listbox from a range:

VBA Code:
LastRow = Sheet10.Range("C65536").End(xlUp).Row
Set rngSource = Sheet10.Range("D7:F" & LastRow)
 With Me.Listbox1
.ColumnCount = 3
 .ColumnWidths = "80;40;50"
.List = rngSource.Cells.Value
 .IntegralHeight = False
 .Height = 94
.IntegralHeight = True
End With

What I need is for it to populate the listbox in the reverse order from what it currently does - what would be the most efficient method for this?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
How about
VBA Code:
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, c As Long
  
   Ary = Sheet10.Range("D7:F" & Sheet10.Range("C" & Rows.Count).End(xlUp).Row).Value2
   ReDim Nary(1 To UBound(Ary), 1 To UBound(Ary, 2))
   For r = UBound(Ary) To 1 Step -1
      nr = nr + 1
      For c = 1 To UBound(Ary, 2)
         Nary(nr, c) = Ary(r, c)
      Next c
   Next r
   With Me.ListBox1
      .ColumnCount = 3
      .ColumnWidths = "80;40;50"
      .List = Nary
      .IntegralHeight = False
      .Height = 94
      .IntegralHeight = True
   End With
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
How about
VBA Code:
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, nr As Long, c As Long
 
   Ary = Sheet10.Range("D7:F" & Sheet10.Range("C" & Rows.Count).End(xlUp).Row).Value2
   ReDim Nary(1 To UBound(Ary), 1 To UBound(Ary, 2))
   For r = UBound(Ary) To 1 Step -1
      nr = nr + 1
      For c = 1 To UBound(Ary, 2)
         Nary(nr, c) = Ary(r, c)
      Next c
   Next r
   With Me.ListBox1
      .ColumnCount = 3
      .ColumnWidths = "80;40;50"
      .List = Nary
      .IntegralHeight = False
      .Height = 94
      .IntegralHeight = True
   End With

Hi, this has been a help to me, and it is just what I need, but I have a question I'm wondering if anyone can help with?

In my source data worksheet there is a date field stored in the dd/mm/yyyy format. However, when I use ths code it displays the date as d/m/yyyy. Is there any way I can tweak things so it diplays the date in the right format dd/mm/yyyy? I'm very new to VBA and not a coder, so any help gratefully received. Thanks.
 
Upvote 0
As you already have a thread running for this topic, you need to stick to it. Thanks
 
Upvote 0
As you already have a thread running for this topic, you need to stick to it. Thanks
Sorry, I'm new to this forum. This isn't my thread, but where I first saw the code I have tried to use? Should I not ask a question about the code here?
 
Upvote 0
You need to continue in your existing thread, rather than duplicating your question.
 
Upvote 0
You need to continue in your existing thread, rather than duplicating your question.
I'm confused, I've not duplicated my question... In my own thread I asked if there was a way to reverse results in a listbox. One of your users kindly gave me some advice, which I accepted and am going to be investigating.

As he was helping me I came across this bit of code in another thread which seems to have done the trick... However I just needed to know if the code could be tweaked slightly to format the date correctly. I thought that asking in the original thread where the code originated from would be the best place, as to keep continuity with that bit of code? I'm obviously wrong - I'll cut and paste the code back into the other thread then....
 
Upvote 0
However I just needed to know if the code could be tweaked slightly to format the date correctly.
That needs to be in your existing thread, not here.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

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