prevent Clicking "End" to close the form when run time error is shown

dss28

Board Regular
Joined
Sep 3, 2020
Messages
165
Office Version
  1. 2007
Platform
  1. Windows
In my program if the user does not proceed with the correct method of clicking the row in a listbox to select the row and then click on the delete row button, error message 381 is shown. if the user now clicks on "End" button the form totally closes and the login window is shown.

if the user selects the row to be deleted and then clicks the delete button the code runs smoothly without issues.

Is there any way to prevent the logout of the user if the user does not proceed correctly?

or I have to program in such a way so that the command button delete is not activated till the user selects any line in the listbox to delete. Any coding available for this?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi,
Try following

In your commandButton property window set the Enabled state of the control to False

In your userforms code page place this event

Rich (BB code):
Private Sub ListBox1_Change()
    Me.CommandButton1.Enabled = Me.ListBox1.ListIndex <> -1
End Sub

You will need to change the names shown in BOLD as required

Dave
 
Upvote 0
Solution
Hi,
Try following

In your commandButton property window set the Enabled state of the control to False

In your userforms code page place this event

Rich (BB code):
Private Sub ListBox1_Change()
    Me.CommandButton1.Enabled = Me.ListBox1.ListIndex <> -1
End Sub

You will need to change the names shown in BOLD as required

Dave
Thanks Dave, it worked perfectly. for my knowledge what does <>-1 indicate?
 
Upvote 0
Thanks Dave, it worked perfectly. for my knowledge what does <>-1 indicate?

When no rows are selected, ListIndex returns -1

Glad suggestion resolved your issue & appreciate feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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