Yes it is working sweet - I love it.
so .Listindex = 1 causes the first item to show in the cbo?
I would like to use column heads and fix the column widths to fit my
data.
Again, it is all greek to me. In Access, you just list the widths you
want with a semi-colon separating the measurements. Here in Excel it
seems to be a different thing becuause it didn't work for me.
So how can I make the widths the way I want and put column titles in the
col heads?
You said that in a cbobox the columns other than 1 are just to assist
the user. Is that also true in a list box. Before I go too far on this
project, which do you recommend that I use and why (for my future info
please). I will have the user click on the customer name in column 1 (or
will they really be clicking the entire entry in the row, it being an
index of the array that is loaded in the cbobox, or am I all wet here?,
and I want to fire my code off of that click. I hope this is possible.
Must look around more in the vb editor.
I have learned more from you in less time than it would take me to get
to my Community College for just 1 class, and they don't give me this
much useful info in one class.
Thank you for your expertise and time.
Joanne
Ron said:
Hi, Joanne
Your code works fine. After clicking the [A] button...the combobox
populates. You may be expecting it to show the first item, maybe? If
yes...then read on. Both the listbox and combobox are set to display the
first item after their respective lists are filled.
Notice, the same code (with slight changes) works for the listbox.
Here's the new code:
Private Sub cmd_A_Click()
FillCboBox MyLetter:="A"
FillListBox MyLetter:="A"
End Sub
Private Sub FillCboBox(MyLetter As String)
Dim SrcData As Range
Dim cCell As Range
Set SrcData = Range("MyDataRange")
With cboCustName
.Clear
For Each cCell In SrcData.Cells
If UCase(cCell.Value) Like MyLetter & "*" Then
.AddItem cCell.Value
.List(.ListCount - 1, 1) = cCell.Offset(ColumnOffset:=1).Value
.List(.ListCount - 1, 2) = cCell.Offset(ColumnOffset:=2).Value
End If
Next cCell
.ListIndex = 1
End With
End Sub
Private Sub FillListBox(MyLetter As String)
Dim SrcData As Range
Dim cCell As Range
Set SrcData = Range("MyDataRange")
With lbxCustName
.Clear
For Each cCell In SrcData.Cells
If UCase(cCell.Value) Like MyLetter & "*" Then
.AddItem cCell.Value
.List(.ListCount - 1, 1) = cCell.Offset(ColumnOffset:=1).Value
.List(.ListCount - 1, 2) = cCell.Offset(ColumnOffset:=2).Value
End If
Next cCell
.ListIndex = 1
End With
End Sub
I hope that helps.
***********
Regards,
Ron
XL2002, WinXP
Joanne said:
Thanks for the help. I am beginning to find my way around the Excel VB
environment a bit. Need to understand where the code goes. Your work is
helping me do it.
I have the FillCboBoxList sub in the userform module. I have the code
behind cmd_A. When I click on "A", I get this error on this line
.List(.ListCount - 1, 1) - cCell.Offset(ColumnOffset = 1).Value
The error is a Compile error telling me that ColumnOffset is a variable
not defined. Don't know what to do about this.
Couple other questions to help me understand the code.
in Like MyLetter & "*" what does the * do?
Also, when I look at my cbo box it looks to me like there is only one
column - may be because there is no data in there yet, but I was
thinking that I may not have the properties setup correctly. Column 1 is
bound, and column count is 3. I did not do anything else with the
properties and I wonder if I should be.
Joanne
(ps you are making this project fun after all the frustration I was
suffering - feels great to be moving forward. Thanks a bunch)
Ron Coderre wrote:
First...For your situation, the BoundColumns start numbering at 1. See
"BoundColumn" in VBA help for more details.
Next, for this example
On Sheet1:
1 range named: MyDataRange that refers to: A2:A11
Cells A2:C11 contain these values:
Alpha Col_2_Row: 2 Col_3_Row: 2
Bravo Col_2_Row: 3 Col_3_Row: 3
Charlie Col_2_Row: 4 Col_3_Row: 4
Delta Col_2_Row: 5 Col_3_Row: 5
Echo Col_2_Row: 6 Col_3_Row: 6
Adam Col_2_Row: 7 Col_3_Row: 7
Betty Col_2_Row: 8 Col_3_Row: 8
Carl Col_2_Row: 9 Col_3_Row: 9
Donna Col_2_Row: 10 Col_3_Row: 10
Ed Col_2_Row: 11 Col_3_Row: 11
I also created a UserForm containing:
1 CommandButton named: cmd_A
1 ComboBox named: ComboBox1
In the UserForm code module, I created a procedure for altering the ComboBox
data:
'------Start of Code------
Private Sub FillCboBoxList(MyLetter As String)
Dim SrcData As Range
Dim cCell As Range
Set SrcData = Range("MyDataRange")
With ComboBox1
.Clear
For Each cCell In SrcData.Cells
If UCase(cCell.Value) Like MyLetter & "*" Then
.AddItem cCell.Value
.List(.ListCount - 1, 1) = cCell.Offset(ColumnOffset:=1).Value
.List(.ListCount - 1, 2) = cCell.Offset(ColumnOffset:=2).Value
End If
Next cCell
End With
End Sub
'------End of Code------
This is the code for cmd_A:
'------Start of Code------
Private Sub cmd_A_Click()
FillCboBoxList MyLetter:="A"
End Sub
'------End of Code------
When the form is displayed, and the cmd_A button is clicked....The clicked
button sends its letter to the FillCboBoxList program....which clears the
ComboBox list and repopulates it.
You can attach that same code to each button...changing the letter, of course
Does that help?
***********
Regards,
Ron
XL2002, WinXP
:
Ron
Thank you, thank you, thank you. I am so relieved to have at least one
thing going right with this new project. And now I have a bit better
idea how to get to the right place in the VB editor and enter code.
You invited me to ask more questions and that I will do!!
I have a form with a button for each letter of the alphabet. When I
click on, say, letter A, I want the control to populate my cbo box with
all entries from my table (sheet1 in the workbook that holds the form)
that begin with the letter A in Column A.
The cbo box will have 3 columns, and when it populates, I want it to
take cols A, B and C from the table and put them in cols 0, 1, and 2 for
all records begining with the chosen alphabet. (I am assuming that
Excel, like Access, begins with col 0 in it's combo boxes.) Do I need to
choose 0 as a 'bound' column?
Does this sound feasible to you?
Joane
Ron Coderre wrote:
Hi, Joanne
You'll want to engage the Workbook.Open event.
Right-Click on the Excel icon (in the upper left of the Excel window)
Select: View Code
Click the "General" dropdown and select Workbook
The default event should be: Open
In its simplest form, your code would look like this:
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Does that help?
(Post back with more questions)
***********
Regards,
Ron
XL2002, WinXP
:
I am using winxp pro and msoffice 2003
I have created a user form in my workbook, but I don't have a clue how
to show the form.
When I click on the desktop icon for the workbook, I would like it to
open and show me the form so the user can use it. I don't know where to
put the code to show the form on open, or how to code it. I know coding
will be something simple like maybe frmName.Show - but I cannot figure
out or find the answer where to put the code so I see my form when I
open the dang workbook.
I have been googleing excel forms, but have yet to come up with a
definitive site on how to create and show and code behind the form I
created. Can do this in Access, and I'm afraid my Access knowledge is
making this all the tougher for me (certainly is creating a degree of
frustration).
If you know of a site that would take me from 'turn on the computer' to
'final form project' for the raw beginner, I sure would be grateful if
you would point me to it. Or perhaps there is a book out there that
would give some time to forms. Using MSOffice 2003 Inside Out right now
but not finding what I need in the index, and reading 1000 pages of
Excel is not in my cards!! The only index listing is 'Form Command' and
that is 3 pages long and is doing me no good as far as creating a user
form.
Any help/info you can give me, I thank you muchly
Joanne