Listbox click error

MicroMouse

New Member
Joined
Jul 10, 2015
Messages
5
Hi All,
I am sure this is fundamental but I just don't understand why. I have a userform with a combobox and a listbox. I can add items to the listbox no problem but I wanted to be able to delete from the listbox. If I assign the following macro to a button then it works fine. However, when I assign it to the listbox_click event I get runtime error 1004.

Private Sub CommandButton1_Click()
Dim Item As String
Dim myRange As Range
Sheet1.Activate

Item = UserForm1.ListBox1.Value

Set myRange = Range("A1", Range("A65535").End(xlUp))

myRange.Find(Item, LookIn:=xlValues, Lookat:=xlWhole).Select
Selection.Delete Shift:=xlUp

UserForm1.ListBox1.RowSource = Range("A1", Range("A65535").End(xlUp)).Address


End Sub

Any thoughts?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If you loaded the listbox with Rowsource and you delete a data from the listbox (Selection.Delete Shift: = xlUp), the ListBox1_Click event is activated again. Also when you reload the listbox1 with UserForm1.ListBox1.RowSource = Range ("A1", Range ("A65535"). End (xlUp)).Address, the ListBox1_Click event is activated again, and then the error occurs.
To avoid the error, you can declare a global variable (at the beginning of the whole code), activate the variable when the click event is running, if the variable is active then you must exit the event.
Try the following:

Code:
Dim deleting As Boolean [COLOR=#0000ff]'at the beginning of the whole code[/COLOR]
'
Private Sub ListBox1_Click()
    Dim Item As String
    Dim myRange As Range
    If deleting = True Then Exit Sub
    '
    deleting = True     '[COLOR=#0000ff]activate the variable[/COLOR]
    Sheet1.Activate
    
    Item = UserForm1.ListBox1.Value
    
    Set myRange = Range("A1", Range("A65535").End(xlUp))
    myRange.Find(Item, LookIn:=xlValues, Lookat:=xlWhole).Select
    Selection.Delete Shift:=xlUp
    UserForm1.ListBox1.RowSource = Range("A1", Range("A65535").End(xlUp)).Address
    deleting = False    '[COLOR=#0000ff]Deactivate the variable[/COLOR]
End Sub
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...19-listbox-click-event-macro-not-working.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
If you loaded the listbox with Rowsource and you delete a data from the listbox (Selection.Delete Shift: = xlUp), the ListBox1_Click event is activated again. Also when you reload the listbox1 with UserForm1.ListBox1.RowSource = Range ("A1", Range ("A65535"). End (xlUp)).Address, the ListBox1_Click event is activated again, and then the error occurs.
To avoid the error, you can declare a global variable (at the beginning of the whole code), activate the variable when the click event is running, if the variable is active then you must exit the event.
Try the following:

Code:
Dim deleting As Boolean [COLOR=#0000ff]'at the beginning of the whole code[/COLOR]
'
Private Sub ListBox1_Click()
    Dim Item As String
    Dim myRange As Range
    If deleting = True Then Exit Sub
    '
    deleting = True     '[COLOR=#0000ff]activate the variable[/COLOR]
    Sheet1.Activate
    
    Item = UserForm1.ListBox1.Value
    
    Set myRange = Range("A1", Range("A65535").End(xlUp))
    myRange.Find(Item, LookIn:=xlValues, Lookat:=xlWhole).Select
    Selection.Delete Shift:=xlUp
    UserForm1.ListBox1.RowSource = Range("A1", Range("A65535").End(xlUp)).Address
    deleting = False    '[COLOR=#0000ff]Deactivate the variable[/COLOR]
End Sub

Hi Dante,
Thank you for your fast response. I have so much to learn. Having said that, I'm struggling with declaring a global variation at the moment so I can test your response.
 
Upvote 0
Hi Dante,
Thank you for your fast response. I have so much to learn. Having said that, I'm struggling with declaring a global variation at the moment so I can test your response.

Just put this line at the beginning of all your code, with that the declaration is made.

Dim deleting As Boolean
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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