ListBox (MultiSelect) Click Event - excel

I want to trigger some event upon clicking certain options in listbox1. I can achieve this when listbox1 is set to SelectSingle. But when using fmMultiSelectMulti property, it does not work. Below is simple code I am using to test the clicking.
Private Sub ListBox1_Click()
If ListBox1.Selected(0) Then MsgBox "Nice"
End Sub
I click the first option and I thought it would trigger the MsgBox. Am I doing something wrong? How can I fix this?
Thanks
Edit: Using the change event worked!

Related

VBA Excel button inactive after one-click Option button (ActiveX controls)

I have 2 Option buttons.
The problem is, that when I click on one of them I can't execute of the process when clicking on the same option button again.
Could anyone tell me how to do it?
I found some threads here, but without solution:
https://social.msdn.microsoft.com/Forums/en-US/f942f0e2-3daf-4a06-a7c9-3904a88e49bc/macro-called-twice-on-single-button-click
I can also do the DoubleClick
Private Sub SchematicImage2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
But it doesn't solve my problem, because I want to have the result after one click and be able to click on the same button with code execution again. The button seems to be no longer active after one click. I don't want that.
How can I do this?
Try this DblClick event, please:
Private Sub SchematicImage2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox IIf(Me.SchematicImage2.value = True, "True", "False")
End Sub

How do I make the Form disappear when a button is clicked?

I need to make a button which forwards me to the Print Preview page in Excel.
The annoying thing is, that the form with the button will stay open and I am also unable to interact with it directly (I can close the whole thing). So I can't move it out of the way to look at the preview or press any other button on the form.
I have already tried the .hide and .visible=false but neither of them worked. It spits out errors or won't make a difference at all.
Private Sub Drucken_IST_Click()
Worksheets("Bestand").PrintOut Preview:=True
End Sub
Have you tried to unload the form?
Here is an example of how to do it. Let me know if it works! :)
Option Explicit
Private Sub CommandButton1_Click()
Unload YourForm1 ' unload
Sheets("YourSheet").Select
YourForm1.Show
End Sub

Command button not re-enabling after condition not met automatically

I have created a Command button and written a small code using if condition to match my criteria. Its working fine if their is any value in the cell/range, its doing according to code "asking to enter the data" and if their is any value "saying data is already enter" and disabling the button(what exactly i wanted).
But, when i am re-entering the data/value into those cell, it's not re-enabling the button back automatically. I have to manually have to go to properties to enable it, which i don't want to do everytime.
Below is my code"
Private Sub RESET_Click()
If Range("A10").Value = "" Then
Reset.Enabled = False
MsgBox "Data has already updated for this period"
Else
Reset.Enabled = True
MsgBox "Please, enter the data"
End If
End Sub
It's not re-enabling the button even after i have listed True in else condition.
Please, also let me know if their is way. So, i can reset/re-enable this button on 1st of every month automatically using VBA.
I would appreciate your response.
Thanks
It seems that your code is triggered by the button press. This way, once the first condition of the if statement is met, the button is disabled and won't run its code anymore.
Maybe what you want is to re-eanable the button when cell changing. So put this code on the worksheet after setting your sub to public:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A10")) Is Nothing Then call RESET_click
End Sub
EDIT:
Maybe you also have to set the button object inside your main sub:
Set Reset = yourworksheet.Buttons("yourbuttonname")
I would also recomend not to use Reset as name inside VBA, since it is already a statement, and may cause confusion.

How to combine listbox code in Excel VBA

I am trying to do an Excel project for work. It involves a lot of listboxes. I needed the listboxes to have a double click event where the single click selects and a double click deselects. After some research, I was able to find some code that would perform that function perfectly:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Application.EnableEvents = False
Me.ListBox1.ListIndex = -1
Application.EnableEvents = True
End Sub
However, there is so much code for all of the listboxes that I think it is making the file glitch. Excel has crashed several times. Each listbox just needs the double click event. Is there a way to combine all this into one general command? Please dumb down your responses, as I am not a programmer. I am in way over my head. Thanks!

How to trigger an event after the user lose focus on a?

I am new to visual basic. I have a TextBox, and I want to trigger an event when the user lose focus on the textBox.
I tried writing
Private Sub TextBox_LostFocus()
something
End Sub
and
Private Sub TextBox_Leave()
something
End Sub
I don't really understand how they work, I have
Private Sub TextBox_Change()
something
End Sub
and that works fine, so what am I missing? How do I trigger the event when the user is not writing in the textbox anymore?
This is what you want:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
There's a helper -- when you're in the code editor, you'll see two list boxes above the editor. The list on the left contains the available objects for the current module. Select TextBox from there if it isn't already selected. The list box on the right contains the available Events. You should see Exit in there. Clicking that will paste in the code above.

Resources