PC Review


Reply
Thread Tools Rate Thread

Combobox will not fill

 
 
prahz
Guest
Posts: n/a
 
      6th Dec 2006
Hello:

I am a relative novice at VBA programming but here goes...

In Excel, I am trying to fill a combobox ("ComboBox1") in a user form
("inputBox") through a dynamic named range in the "MasterDataSheet"
worksheet of my workbook. The dynamic named range is named
"ClientList" and is referenced with the formula
=OFFSET(MasterDataSheet!$B$1,0,0,1,COUNTA(MasterDataSheet!$1:$1)-1)

The fact that the list I want to populate into the combobox is across
columns rather than rows is giving me some difficulty. Below is the
code for my user form. Can someone please help me with where I am gong
wrong?

Option Explicit

Private Sub UserForm_Initialize()
Dim cName As Range
Dim ws As Worksheet
Set ws = Worksheets("MasterDataSheet")

For Each cName In ws.Range("ClientList")
With Me.ComboBox1
.AddItem cName.Value
.List(1, .ListCount - 1) = cName.Offset(0, 1).Value
End With
Next cName
End Sub

 
Reply With Quote
 
 
 
 
Jon Peltier
Guest
Posts: n/a
 
      6th Dec 2006
Use

For Each cName In ws.Range("ClientList").Cells

instead of

For Each cName In ws.Range("ClientList")

What are you trying to do here:

.List(1, .ListCount - 1) = cName.Offset(0, 1).Value

What the code says is in the second row of the combo box (first parameter,
0=first row), in the Nth column, where N is the number of items already
added, add something. I think you have your row and column indices reversed.
To add something in the second column of the last row of the list, where you
just added something to the first row of this column, try this:

.List(.ListCount - 1, 1)

Likewise I think your row and column index in the .Offset are reversed,
because you're saying use the next cell to the right, which would be the
next cell in the defined range. I suspect you mean to use values from the
next row below the defined range:

cName.Offset(1, 0).Value

Altogether, now:

.List(.ListCount - 1, 1) = cName.Offset(1, 0).Value

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"prahz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello:
>
> I am a relative novice at VBA programming but here goes...
>
> In Excel, I am trying to fill a combobox ("ComboBox1") in a user form
> ("inputBox") through a dynamic named range in the "MasterDataSheet"
> worksheet of my workbook. The dynamic named range is named
> "ClientList" and is referenced with the formula
> =OFFSET(MasterDataSheet!$B$1,0,0,1,COUNTA(MasterDataSheet!$1:$1)-1)
>
> The fact that the list I want to populate into the combobox is across
> columns rather than rows is giving me some difficulty. Below is the
> code for my user form. Can someone please help me with where I am gong
> wrong?
>
> Option Explicit
>
> Private Sub UserForm_Initialize()
> Dim cName As Range
> Dim ws As Worksheet
> Set ws = Worksheets("MasterDataSheet")
>
> For Each cName In ws.Range("ClientList")
> With Me.ComboBox1
> .AddItem cName.Value
> .List(1, .ListCount - 1) = cName.Offset(0, 1).Value
> End With
> Next cName
> End Sub
>



 
Reply With Quote
 
prahz
Guest
Posts: n/a
 
      6th Dec 2006
Jon,

As you can see I had no clue...You are the man...thanks!


Jon Peltier wrote:
> Use
>
> For Each cName In ws.Range("ClientList").Cells
>
> instead of
>
> For Each cName In ws.Range("ClientList")
>
> What are you trying to do here:
>
> .List(1, .ListCount - 1) = cName.Offset(0, 1).Value
>
> What the code says is in the second row of the combo box (first parameter,
> 0=first row), in the Nth column, where N is the number of items already
> added, add something. I think you have your row and column indices reversed.
> To add something in the second column of the last row of the list, where you
> just added something to the first row of this column, try this:
>
> .List(.ListCount - 1, 1)
>
> Likewise I think your row and column index in the .Offset are reversed,
> because you're saying use the next cell to the right, which would be the
> next cell in the defined range. I suspect you mean to use values from the
> next row below the defined range:
>
> cName.Offset(1, 0).Value
>
> Altogether, now:
>
> .List(.ListCount - 1, 1) = cName.Offset(1, 0).Value
>
> - Jon
> -------
> Jon Peltier, Microsoft Excel MVP
> Tutorials and Custom Solutions
> http://PeltierTech.com
> _______
>
>
> "prahz" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hello:
> >
> > I am a relative novice at VBA programming but here goes...
> >
> > In Excel, I am trying to fill a combobox ("ComboBox1") in a user form
> > ("inputBox") through a dynamic named range in the "MasterDataSheet"
> > worksheet of my workbook. The dynamic named range is named
> > "ClientList" and is referenced with the formula
> > =OFFSET(MasterDataSheet!$B$1,0,0,1,COUNTA(MasterDataSheet!$1:$1)-1)
> >
> > The fact that the list I want to populate into the combobox is across
> > columns rather than rows is giving me some difficulty. Below is the
> > code for my user form. Can someone please help me with where I am gong
> > wrong?
> >
> > Option Explicit
> >
> > Private Sub UserForm_Initialize()
> > Dim cName As Range
> > Dim ws As Worksheet
> > Set ws = Worksheets("MasterDataSheet")
> >
> > For Each cName In ws.Range("ClientList")
> > With Me.ComboBox1
> > .AddItem cName.Value
> > .List(1, .ListCount - 1) = cName.Offset(0, 1).Value
> > End With
> > Next cName
> > End Sub
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fill combobox depending on selection from another combobox Adam Francis Microsoft Excel Misc 2 24th Jul 2008 07:39 PM
Better way to fill a ComboBox ? mike.r.harris@blueyonder.co.uk Microsoft Excel Programming 4 1st Nov 2006 03:18 PM
Can I fill a combobox based on another combobox =?Utf-8?B?U1dCb2RhZ2Vy?= Microsoft Access 2 25th Apr 2006 05:56 PM
How to fill one ComboBox from other ComboBox control? Sakharam Phapale Microsoft VB .NET 6 2nd Dec 2004 01:45 PM
Fill a ComboBox Pepehammer Microsoft VB .NET 10 18th Feb 2004 08:14 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:47 AM.