Combobox add to list - two ranges

neodjandre

Well-known Member
Joined
Nov 29, 2006
Messages
950
Office Version
  1. 2019
Platform
  1. Windows
I am using this code:

VBA Code:
Dim lastrow As Integer
Dim lastrow2 As Integer
Dim adrow As Integer
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

adrow = x_admin.Range("range_admin_header").Row + 1
lastrow = Application.WorksheetFunction.CountIf(x_admin.Range("J" & adrow & ":" & "J245"), "1") - 1
lastrow2 = Application.WorksheetFunction.CountIf(x_admin.Range("L" & adrow & ":" & "L245"), "1") - 1

Set rng1 = x_admin.Range("I" & adrow & ":" & "I" & adrow + lastrow)
Set rng2 = x_admin.Range("K" & adrow & ":" & "K" & adrow + lastrow2)
Set rng3 = Union(rng1, rng2)

query_combo.List = rng3.Value2

query_combo is a VBA combobox. But for some weird reason the combobox only lists rng1 values instead of the Union values with rng2.
 
How about
VBA Code:
Dim lastrow As Integer
Dim lastrow2 As Integer
Dim adrow As Integer
Dim Ary1 As Variant, Ary2 As Variant, Ary3 As Variant
Dim r As Long, nr As Long

adrow = x_admin.Range("range_admin_header").Row + 1
lastrow = Application.WorksheetFunction.CountIf(x_admin.Range("J" & adrow & ":" & "J245"), "1") - 1
lastrow2 = Application.WorksheetFunction.CountIf(x_admin.Range("L" & adrow & ":" & "L245"), "1") - 1

Ary1 = x_admin.Range("I" & adrow & ":" & "I" & adrow + lastrow).Value2
Ary2 = x_admin.Range("K" & adrow & ":" & "K" & adrow + lastrow2).Value2

ReDim Ary3(1 To UBound(Ary1) + UBound(Ary2))
For r = 1 To UBound(Ary1)
   nr = nr + 1
   Ary3(nr) = Ary1(r, 1)
Next r
For r = 1 To UBound(Ary2)
   nr = nr + 1
   Ary3(nr) = Ary2(r, 1)
Next r

query_combo.List = Ary3
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
hmm ok, I will try this, though I was trying to avoid looping
 
Upvote 0
Because it's looping through arrays it will be very quick.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,981
Members
448,934
Latest member
audette89

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