Setting a range for a listbox

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,120
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Someone told me a range did not have to be consecutive columns, and I'm pretty sure I confirmed that.
However when I set a range for a 2 column listbox :-
VBA Code:
lstPrefix.List = .Range(cCol & First & ":" & cCol & Last, "L" & First & ":" & "L" & Last).Value
This first col is correctly CCol but the other is not "L" but the adjacent column to the right of cCol
Can I resolve this ? Maybe a named range? If yes, I'll look further into that.
Thanks.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I propose 2 options:

A) Load the 12 columns from A to L and hide the columns you don't need.

VBA Code:
  Dim col As Long
  
    col = .Range(cCol & 1).Column
    lstPrefix.ColumnWidths = WorksheetFunction.Replace("0;0;0;0;0;0;0;0;0;0;0;", col * 2 - 1, 1, "")
    lstPrefix.List = .Range("A" & first & ":" & "L" & last).Value

B) Load all the records in the 2 columns into an array and then pass the array to the listbox

VBA Code:
  Dim a As Variant
  Dim i As Long, k As Long
  ReDim a(1 To last, 1 To 2)
  
    For i = first To last
      k = k + 1
      a(k, 1) = .Range(cCol & i)
      a(k, 2) = .Range("L" & i)
    Next

    lstPrefix.List = a

🧙‍♂️
 
Upvote 0

Forum statistics

Threads
1,225,757
Messages
6,186,848
Members
453,379
Latest member
gabriellegonzalez

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