Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2009, 02:26 AM
Authorized User
 
Join Date: Mar 2008
Posts: 74
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via ICQ to sektor
Default Iteration thru named range

I wanna fill Combobox with items in named range Range("divisions"), but I get error. Here's code.

Code:
Private Sub Worksheet_Activate()
    Dim division As Range
    cmbDivisions.Clear
    With cmbDivisions
        For Each division In Range("divisions")
            .AddItem division.Value
        Next
    End With
End Sub
How to solve this problem?
 
Old April 9th, 2009, 01:59 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Not sure why you're not using ListFillRange because it would be easier. Either that or have a range specified in a cell your code points to for that combo.

It's best not to fill every time you click the dropdown so both my examples below will check to make sure it's not pulling the same information twice. The second example is what I'd suggest doing instead.

Since Excel doesn't for whatever reason have .tag for this element you'll need to do something like this for the way you're trying to do it:
----------------------------------------------
Option Explicit
Dim sDivRange As String

Private Sub Division_Combo_DropButtonClick()

'Clears and repopulates dropdown to range
If sDivRange = Range("divisions").Address Then Exit Sub
Dim oDiv As Range
Division_Combo.Clear
For Each oDiv In Range("divisions")
Division_Combo.AddItem oDiv.Value
Next
sDivRange = Range("divisions").Address

End Sub
----------------------------------------------

It would be better to use a range that adjusts itself to the end of your data. The F1 initial setting and check for null strings for the cells prevents errors or improper ranges caused by how range.end works.

----------------------------------------------
'Clears and repopulates dropdown to range
Dim sFill As String
If Range("F1").Value = "" Then Exit Sub
sFill = "F1"
If Range("F2").Value <> "" Then sFill = "F1:" & Range("F1").End(xlDown).Address(False, False)
If Division_Combo.ListFillRange <> sFill Then Division_Combo.ListFillRange = sFill
----------------------------------------------

A final way of doing this is to do your own fill manually by adding each line within a range to the newly cleared drop but then you'd want to perform a check to see if that range has changed since last load to prevent from having to constantly reload it which really isn't worth the effort considering the ease of the above solution.

Hope this helped,
Allen
 
Old April 10th, 2009, 04:48 AM
Authorized User
 
Join Date: Mar 2008
Posts: 74
Thanks: 2
Thanked 0 Times in 0 Posts
Send a message via ICQ to sektor
Default Thanks a lot!

Thanks for advice! All works as expected!
Yeah, loading Combobox everytime I activate sheet adds some overhead.
Now all works faster and reliable. :) Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Named Range and Combobox bahachin Excel VBA 5 November 23rd, 2006 01:16 PM
iteration bostek Excel VBA 5 August 30th, 2006 01:23 AM
ComboBox & Named Range bahachin Excel VBA 2 June 13th, 2006 12:59 PM
iteration movenext weazy Excel VBA 0 June 9th, 2006 05:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.