Range.Delete causes unexpected Click event

CPerdue

New Member
Joined
May 6, 2011
Messages
27
Good, bleary-eyed, morning folks,

Today's conundrum is that using Range.Delete(xlUp) is causing unexpected _Click events (two in fact) in a ListBox.

I have a named range, "List", covering $A$3:$A$7 which is used in the box's ListFillRange property and as data in the Sub UpdateList. A second range, "Destination" is at $K$3:$M$4. The ListBox itself is physically at ~$C$3:$D$5. Finally, a count of times the ListBox1_Click() runs is kept at $D$11.

Code:
Private Sub ListBox1_Click()
    Range("$C$11").Value = ListBox1.Value
    Range("$D$11").Value = Range("$D$11").Value + 1
End Sub
Public Sub UpdateList()
    Application.EnableEvents = False
    For i = 1 To 5
        Range("Destination").Resize(1, 1).Offset(1 + i, 2).Value = Range("List")(i)
    Next i
 
    Range("Destination").Resize(10, 1).Offset(0, 2).Find("c").Delete (xlUp)
    Application.EnableEvents = True
End Sub

I've set a breakpoint at the _Click, when I get there and look at the call stack I see:

VBAProject.Sheet1.ListBox1_Click
Non-Basic Code (with a format that makes drives this window nuts)
VBAProject.Sheet1.UpdateList
The "Application.EnableEvents = False" didn't seem to help.

I've seen similar things happen with CheckBox_Click, that is a different post I suppose.

Thanks much,

C.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe you could use the _MouseDown event instead of the _Click event.

Code:
Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Range("$C$11").Value = ListBox1.Value
    Range("$D$11").Value = Range("$D$11").Value + 1
End Sub


Also on an unrelated issue, the (xlUp) isn't the correct syntax. It shouldn't have the parenthesis, and it should be xlShiftUp (though both xlConstants are actually the same value).

Code:
Range("Destination").Resize(10, 1).Offset(0, 2).Find("c").Delete [COLOR="Red"]xlShiftUp[/COLOR]
 
Upvote 0
Huh, must be some equivalence for old syntax...anyway, it works the same.

I'm not actually ever clicking on the ListBox.

Just tested
Code:
Private Sub ListBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Range("$e$11").Value = ListBox1.Value
    Range("$f$11").Value = Range("$f$11").Value + 1
End Sub
... it works and does NOT go off unexpectedly.

OK, this is a valid workaround but geeze, I wonder what is actually happening?

Thanks for the help.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,451
Members
452,915
Latest member
hannnahheileen

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