ListBox2.AddItem ListBox1.Value becomes a String when being Integer?

S

SIGE

Hi Bob, Tom, ...all other Helping Hands,

I am reading in values (integers & strings) in listbox1 as follows:
UserForm1.ListBox1.AddItem Sheets("Sheet1").Cells(Userrange.row,
kolom)

I make a selection out of Listbox1 into Listbox2 as in sub:
AddButton_Click()

===>Does my Integer gets converted into String with:
ListBox2.AddItem ListBox1.Value ????

and therefore cannot find the Integer in the
"Application.Match(ListBox2.List(i), Range(Userrange.Address),
0)"-function?!

How to get the same cell "formats" in listbox2 as in listbox1?
Sige


Private Sub AddButton_Click()
Dim i As Integer
If ListBox1.ListIndex = -1 Then Exit Sub
If Not cbDuplicates Then
' See if item already exists
For i = 0 To ListBox2.ListCount - 1
If ListBox1.Value = ListBox2.List(i) Then
Beep
Exit Sub
End If
Next i
End If
ListBox2.AddItem ListBox1.Value
End Sub



Private Sub OKButton_Click()
Dim i As Integer
Dim col As Integer
Dim iLastRow As Integer
Dim Rng As Range

MsgBox "The 'To list' contains " & ListBox2.ListCount & " items."
For i = 0 To ListBox2.ListCount - 1
MsgBox ListBox2.List(i)

col = Application.Match(ListBox2.List(i),
Range(Userrange.Address), 0)
iLastRow = Cells(Rows.Count, col).End(xlUp).row
Set Rng = Range(Cells(AccountOnRow, col), Cells(iLastRow, col))

ActiveWorkbook.Names.Add Name:="VBA" & (i), RefersTo:=Rng
Next i
Unload Me
End Sub
 
T

Tom Ogilvy

Everything in a listbox is a string. Just convert it to a number to perform
your match

Application.Match(clng(ListBox2.List(i)), Range(Userrange.Address),0)

Note that sample code may contain syntax errors not introduced by me.
 
S

SIGE

Hi Tom,

Thanks!!!
A subtle piece of art!
:blush:)))
Sige

"NOSPAM" to be removed for direct mailing...
 

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