type error

D

David Gerstman

Here's my code:

Private Sub cbAddContents_Click()

Dim ind As Integer
Dim tot_items As Integer

tot_items = ListBox1.ListCount
For ind = 1 To tot_items

If ListBox1(ind).Checked = True Then
Worksheets("SF 701").Cells(8, 1).Offset(0, ind).Value =
ListBox1(ind).Value
End If
Next ind

When I get to the if statement, I get a Type Mismatch error. What am I doing
wrong?

David
 
C

Charles Chickering

David, I'm assuming you've allowed multiple select in your listbox and are
trying to move those selections into the spreadsheet. Is that correct? I
think you need to reference the list item of the listbox:
If ListBox1.Selected(ind) Then
Worksheets("SF 701").Cells(8, 1).Offset(0, ind).Value = ListBox1.List(ind)
End If

Let me know if this is not what you are trying to do.
 
D

David Gerstman

Charles,

Thanks a lot, that is the situation and that's what I'm trying to do.

However the if statement is now getting an error:

Run time error -2147024809 (80070057)
Could not get the Selected property. Invalid argument.

David
 
C

Charles Chickering

Try dexrementing your index, the list is 0 based
For ind = 0 to tot_items - 1

Let me know how that goes.
 
D

David Gerstman

Thanks, but it'll have to wait until Friday.

Have a happy thanksgiving!

David
 
D

David Gerstman

Charles,

Right before I left on Wednesday, I checked and you were right. (I didn't
need to decrement though.) The problem was that the index was out of range, I
changed it to 0 to tot_items - 1 and it worked fine.

If the error message had been: "Out of range" I would have realized that on
my own. Thanks a lot for your help!

David
 

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