Error 381 VBA Listbox Error

Celticfc

Board Regular
Joined
Feb 28, 2016
Messages
152
Hi, I have a Userform with 2 listboxes, users can add from listbox 1 to listbox 2. Submit button sends Listbox2 lists as a string to a cell with delimiter.

Everything works fine except when Listbox 2 is empty; it gives Runtime 381 error. i.e. if a user wants to remove everything from list2.


Code:
x = ListBox2.List(0)

 For i = 2 To ListBox2.ListCount
        x = x & ", " & ListBox2.List(i - 1)
    Next I

 Range.Offset(0, 17).Value = x

I need a condition where if the listbox 2 is empty (hence x has no value), then offset(0,17) value should be also 0.

Many thanks in advance.
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
How about
Code:
   If Me.ListBox1.ListCount > 0 Then
      X = ListBox2.List(0)
      For i = 2 To ListBox2.ListCount
         X = X & ", " & ListBox2.List(i - 1)
      Next i
   Else
      X = 0
   End If
   Range.Offset(0, 17).Value = X
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top