UserForm Listbox issue

B

bach

Hi,

After some searching on the forum I found a article which was what
was looking for.

WHAT AM I DOIN

I have a sheet with data on it, I want to populate a list box with dat
displayed on this sheet.

CODE CURRENTLY USIN


Code
-------------------

Dim ws As Worksheet
Set ws = Worksheets("Member_list")

With ws
Me.lstMembers.List = Application.Transpose(.Range(.Range("A2"), .Range("A2").End(xlDown)).Value)
End With

-------------------


PROBLEM


- The list box populates with every row in the worksheet. I want th
list box to populate with only the data on the sheet so I guess
need to do a check on available data first any idears ???

- The listbox is only using the first colum A2 but since I don
really understand the code I not sure what to change. Could someon
explain and give possible solutions


Kind Regard
 
D

Dave Peterson

..range("a2").end(xldown)

is the equivalent of selecting A2, then hitting the End key and then the down
arrow.

If you only have data in A2 (or nothing in A2 all the way down to A65536), then
that .end(xldown) goes all the way to the bottom of the worksheet.

So you either have to make sure A2 and A3 (at a minimum) are populated or add
some checks to your code. I also like to start at the bottom of the column
(A65536) and work my way up the column. (Like going to A65536, hitting End,
then up arrow--but this can suffer the same problem if there's nothing in A2.)

I guess the next question is what should happen if you don't have data in that
range.

This may give you some ideas:

Option Explicit
Private Sub Worksheet_Activate()

Dim ws As Worksheet
Dim ListBoxArray As Variant

Set ws = Worksheets("Member_list")

With ws
If IsEmpty(.Range("a2")) Then
'do nothing
ElseIf IsEmpty(.Range("a3")) Then
ListBoxArray = Array(.Range("a2").Value)
Else
ListBoxArray = .Range(.Range("A2"), .Range("A2").End(xlDown)).Value
End If
End With

If IsArray(ListBoxArray) = False Then
MsgBox "what happens here?"
Me.lstMembers.Clear
Else
Me.lstMembers.List = ListBoxArray
End If

End Sub
 
B

bach

Hi,

Thanks for your help, it was very usefull although I am still a little
lost over some of the code.

DUM

The line of code which sets the array

range("A2"), .Range("A2")

What does this do why is the range in twice.

Also would it be possible to implement more than one colum. I would
like to implement a mixture of colums for the list box but they are not
next to each other.

Basically I would like there userid - A, surname - E, forename - F etc
 
D

Dave Peterson

I'm guessing you're asking about this line:

ListBoxArray = .Range(.Range("A2"), .Range("A2").End(xlDown)).Value

If you select A2, then hit the End key, followed by ctrl-shift-down arrow,
you'll be selecting A2 until your data has a gap in it (no gaps in the data
means that A2 through the last used cell of column A will be selected).

This is the equivalent in code.

And you can get more columns using something like:

Option Explicit
Private Sub Worksheet_Activate()

Dim ws As Worksheet
Dim ListBoxRng As Range
Dim myCell As Range

Set ws = Worksheets("Member_list")

Set ListBoxRng = Nothing

'don't forget to change this!
Me.lstMembers.ColumnCount = 3

With ws
If IsEmpty(.Range("a2")) Then
'do nothing
ElseIf IsEmpty(.Range("a3")) Then
Set ListBoxRng = .Range("a2")
Else
Set ListBoxRng = .Range(.Range("A2"), .Range("A2").End(xlDown))
End If
End With

If ListBoxRng Is Nothing Then
MsgBox "what happens here?"
Me.lstMembers.Clear
Else
With Me.lstMembers
.Clear
'don't forget to change this!
.ColumnCount = 3
.ListFillRange = ""
For Each myCell In ListBoxRng.Cells
.AddItem myCell.Value
.List(.ListCount - 1, 1) = myCell.Offset(0, 2).Value
.List(.ListCount - 1, 2) = myCell.Offset(0, 3).Value
Next myCell
End With
End If

End Sub

I used 3 columns: A, C, D
(.offset(0,2) is two to the right)
(.offset(0,3) is three to the right)
 
B

bach

Hi,

Thanks for your help, I have got the following with errors lol


Code
-------------------
Dim ws As Worksheet
Dim ListBoxArray As Variant

Set ws = Worksheets("Member_list")

With ws
If IsEmpty(.Range("A2")) Then
ElseIf IsEmpty(.Range("A3")) Then
ListBoxArray = Array(.Range("A2").Value)
Else
ListBoxArray = .Range(.Range("A2"), .Range("A2").End(xlDown)).Value
End If

End With


If IsArray(ListBoxArray) = False Then
Me.lblTotalNo.Caption = "There are no members currently in the database."
Me.lstMembers.Clear
Else
With Me.lstMembers
.Clear
.ColumnCount = 3
.ListFillRange = ""
For Each myCell In ListBoxArray.Cells
.AddItem myCell.Value
.List(.ListCount - 1, 1) = myCell.Offset(0, 2).Value
.List(.ListCount - 1, 2) = myCell.Offset(0, 3).Value
Next myCell
End With
End I
 
D

Dave Peterson

The code changed. Try using the newer version.


Hi,

Thanks for your help, I have got the following with errors lol

Code:
--------------------
Dim ws As Worksheet
Dim ListBoxArray As Variant

Set ws = Worksheets("Member_list")

With ws
If IsEmpty(.Range("A2")) Then
ElseIf IsEmpty(.Range("A3")) Then
ListBoxArray = Array(.Range("A2").Value)
Else
ListBoxArray = .Range(.Range("A2"), .Range("A2").End(xlDown)).Value
End If

End With


If IsArray(ListBoxArray) = False Then
Me.lblTotalNo.Caption = "There are no members currently in the database."
Me.lstMembers.Clear
Else
With Me.lstMembers
.Clear
.ColumnCount = 3
.ListFillRange = ""
For Each myCell In ListBoxArray.Cells
.AddItem myCell.Value
.List(.ListCount - 1, 1) = myCell.Offset(0, 2).Value
.List(.ListCount - 1, 2) = myCell.Offset(0, 3).Value
Next myCell
End With
End If
 
B

bach

Dave Peterson,

Thanks for you help with this, I have copied the code and it does no
like *.listfillrange*. I have commented this bit of the code out an
it seems to populate the listbox.

I will have to play with the code to ensure all the colums they wan
are included inside the list box.

If I have any issues will post back.

P.S
-Would you have an idea why it does not like *.listfillrange* and is i
required.
 
D

Dave Peterson

Maybe if you swap the order...

With Me.lstMembers
.ListFillRange = ""
.Clear

But I was just trying to make sure you didn't assign a range address to the
listbox. If you didn't do that, then this line of code isn't really necessary.
 
B

bach

Hi,


Thanks for your help the generattion of data inside a textbox seem
fine.

Now I need to write some code to handle a double click event and brin
up another form with the details displayed any tips??

Bac
 
D

Dave Peterson

Use the lstMembers_DblClick event???
Hi,

Thanks for your help the generattion of data inside a textbox seems
fine.

Now I need to write some code to handle a double click event and bring
up another form with the details displayed any tips??

Bach
 
B

bach

That was the easy bit lol.

I was thinking along the lines of storing the listbox index of the item
selected. Using this variable to select the row in the spreadsheet and
display the data in the appropriate form elements does this sound
right.

Bach.
 
D

Dave Peterson

Yeah, I wondered what you were fishing for...

Seems ok to me.

But if the listbox only supports a single selection, you could use .value, too.
But the index seems nicer if you're populating the original listbox using
..rowsource (well, and you want to use that worksheet info for the next
userform).
 
B

bach

Hi,

This is harder than i thought it would be, I have the listbox index
stored with +2 so that it matches the first bit of data on the sheet
(row2).

This is stored in a variable listSelect, how do i use this to select a
row and display the results

I am thinking that i would need to use this

ws.Cells(irow, 2).Value = UCase(Me.cboYourDeacon.Value)
but in reverse, but this used the irow which was generated to find the
last row.

I need irow to equal the row which i have attempted with

irow = ws.Cells(Rows(lstSelect & ":" & lstSelect))

which didn't work any idears.

Bach
 
D

Dave Peterson

I'm kind of confused about how you populated the listbox.

I used a range on sheet1 and this seemed to work ok.

Option Explicit
Dim myInputRng As Range
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
Dim myRow As Long
With Me.ListBox1
If .ListIndex < 0 Then
Beep 'nothing to do
Else
myRow = myInputRng.Cells(1).Offset(.ListIndex).Row
MsgBox myRow & vbLf & .Value & vbLf & _
myInputRng.Cells(1).Offset(.ListIndex).Value
End If
End With
End Sub
Private Sub UserForm_Initialize()

With Worksheets("sheet1")
Set myInputRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
End With

With Me.ListBox1
.List = myInputRng.Value
End With
End Sub
 
B

bach

Thanks for your reply

Using the above posts I have populated the listbox with the followin
code.



Code
-------------------
Private Sub AllMembers()
'Variables for worksheet, and range
Dim ws As Worksheet
Dim ListBoxRng As Range
Dim myCell As Range

'Sets the worksheet for the listbox
Set ws = Worksheets("Member_list")

'Clears the listboxrng variable
Set ListBoxRng = Nothing

'Sets the columns to be used in the list box
Me.lstMembers.ColumnCount = 4

With ws
If IsEmpty(.Range("a2")) Then
Me.lblTotalNo = "There are no members in the Database"
ElseIf IsEmpty(.Range("a3")) Then
Set ListBoxRng = .Range("a2")
Else
Set ListBoxRng = .Range(.Range("A2"), .Range("A2").End(xlDown))
End If
End With

If ListBoxRng Is Nothing Then
Me.lblTotalNo = "There are no members in the Database"
Me.lstMembers.Clear
Else
With Me.lstMembers
.Clear
'don't forget to change this!
.ColumnCount = 4
For Each myCell In ListBoxRng.Cells
.AddItem myCell.Value
.List(.ListCount - 1, 1) = myCell.Offset(0, 4).Value
.List(.ListCount - 1, 2) = myCell.Offset(0, 5).Value
.List(.ListCount - 1, 3) = myCell.Offset(0, 10).Value
Next myCell
End With
End If

End Su
-------------------


I had to take out .listfillrange it doesn't like it, what I am no
trying to achieve is on the double click of an item on this lis
userform, a members userform is loaded with all the details of th
selected member from the list userform.


Code
-------------------
Private Sub lstMembers_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim lstSelect As Integer
Dim ws As Worksheet
Dim irow As Integer

Application.ScreenUpdating = False
lstSelect = Me.lstMembers.ListIndex + 2

Select Case ListType
Case 1 'Members
Set ws = Worksheets("Member_list")
irow = ws.Cells(Rows(lstSelect & ":" & lstSelect))
MemberType = 3
Load frmMember
frmMember.txtMemID.Value = irow
Case 2 'Deacons
ws = Worksheets("Deacon_list")
End Select

MsgBox lstSelect

End Su
 
D

Dave Peterson

I'm confused about what's happening in here:

Private Sub lstMembers_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim lstSelect As Long
Dim ws As Worksheet
Dim iRow As Long
Dim membertype As Long

Application.ScreenUpdating = False
lstSelect = Me.lstMembers.ListIndex + 2

Select Case ListType
Case 1 'Members
Set ws = Worksheets("Member_list")
iRow = ws.Cells(Rows(lstSelect & ":" & lstSelect))
membertype = 3
Load frmMember
frmMember.txtMemID.Value = iRow
Case 2 'Deacons
ws = Worksheets("Deacon_list")
End Select

MsgBox lstSelect

End Sub

I don't know what ListType is.

It kind of looks like you want this:
iRow = lstSelect
instead of
iRow = ws.Cells(Rows(lstSelect & ":" & lstSelect))
 
B

bach

Dave,

That code was my attempt at starting it, I throught I would need to
select the entire row of data so when the frm loads all the texts and
other objects can be populated with the data (14 objects).

Not sure through
 
D

Dave Peterson

You don't need to select a cell/row to work with it.

But I still don't know what you're doing with those variables or what they're
populated with....

I put this in a General module:

Option Explicit
Public ListBoxRng As Range

I put this behind the first userform:

Option Explicit
Private Sub UserForm_Initialize()
Call AllMembers
End Sub
Private Sub AllMembers()
'Variables for worksheet, and range
Dim ws As Worksheet
Dim myCell As Range

'Sets the worksheet for the listbox
Set ws = Worksheets("Member_list")

'Clears the listboxrng variable
Set ListBoxRng = Nothing

With ws
If IsEmpty(.Range("a2")) Then
Me.lblTotalNo = "There are no members in the Database"
ElseIf IsEmpty(.Range("a3")) Then
Set ListBoxRng = .Range("a2")
Else
Set ListBoxRng = .Range(.Range("A2"), .Range("A2").End(xlDown))
End If
End With

If ListBoxRng Is Nothing Then
Me.lblTotalNo = "There are no members in the Database"
Me.lstMembers.Clear
Else
With Me.lstMembers
.Clear
'don't forget to change this!
.ColumnCount = 4
For Each myCell In ListBoxRng.Cells
.AddItem myCell.Value
.List(.ListCount - 1, 1) = myCell.Offset(0, 4).Value
.List(.ListCount - 1, 2) = myCell.Offset(0, 5).Value
.List(.ListCount - 1, 3) = myCell.Offset(0, 10).Value
Next myCell
End With
End If

End Sub

Private Sub lstMembers_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

Dim ws As Worksheet
Dim iRow As Long
Dim ListType As String
Dim MemberType As Long
Dim mySelectedCell As Range

With Me.lstMembers
If .ListIndex < 0 Then
Beep
Cancel = True
Exit Sub
End If

Application.ScreenUpdating = False
Set mySelectedCell = ListBoxRng.Resize(1).Offset(.ListIndex)

'pick up the 3rd column of the listbox????????
ListType = CLng(.List(.ListIndex, 2))

'or pick it up from the member_list worksheet
ListType = CLng(mySelectedCell.Offset(0, 8).Value)

Load frmMember

Select Case ListType
Case 1 'Members
Set ws = Worksheets("Member_list")
MemberType = 3
frmMember.txtMemID.Value = ListType
Case 2 'Deacons
ws = Worksheets("Deacon_list")
End Select
Me.Hide
frmMember.Show
Application.ScreenUpdating = True

End With

End Sub

I guessed you were either picking up the value from the userform or from the
worksheet.

One of these may help, but you won't need both:

'pick up the 3rd column of the listbox????????
ListType = CLng(.List(.ListIndex, 2))

'or pick it up from the member_list worksheet
ListType = CLng(mySelectedCell.Offset(0, 8).Value)

But I still don't know...
 
B

bach

Dave,

Thanks for your reply, I have used the code and i get a type
mismatch.

'pick it up from the list worksheet
ListType = CLng(mySelectedCell.Offset(0, 8).Value)
 
D

Dave Peterson

I have no idea what ListType was, so I declared it as long (an integer).

Since I didn't know what it was, I just took the value from that cell and tried
to make sure it was a nice number.

You never shared what ListType was and how it was derived--so I just guessed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top