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.
Related
I want to restart the master userform for a project I'm working on excel.
I tried hide, unload other userforms, call UserForm_Initialize and show again but not worked. Some variables still in memory, close (X) button didn't work anymore etc.
I looked on internet and there is no solution . So i found that solution below.
I added another empty master userform and call my userform if user or my codes unload or close my userform, master userform automaticly show again. This works but i cant use vbModeless Because out of stack error
i wonder is there another (more proper) solution ?
my master userform codes:
Option Explicit
Private Sub UserForm_Click()
StartApp 'there is no escape :)
End Sub
Private Sub UserForm_Initialize()
StartApp
End Sub
Public Sub StartApp()
Me.Hide 'sometimes this form showing itself so i added everywhere :)
Splash.Show
Me.Hide
StartApp
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Cancel = True
StartApp
End Sub
Splash is my loader form. Load's other forms etc.
Edit 1: Solved vbModeless problem with this link
Still looking another (more proper) solution ? (Without master form)
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!
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
I have a UserForm, A, with a command button that opens UserForm B using the below code:
Private Sub cmd_click()
Me.Hide
B.Show
End Sub
B, when closed via the X button, runs the following:
Private Sub UserForm_Terminate()
Unload Me
A.Show
End Sub
After:
opening B (from A)
closing B and returning to A
and then opening B again
The close button on B ceases to work.
UserForm A is a menu form that leads to several others, so navigating back and forth between the menus is a pretty basic requirement. This behaviour is reproducible for every form linked from A, as well as for tertiary forms that open from those.
Given my target demographic is of the older generation, I want my UserForms to be intuitive to use, which means preserving regular close button functionality.
Does anyone have any info on this? It may be that my google-fu is lacking, but I can't seem to find anyone who has had the same issue. Any input at this stage is greatly appreciated!
Use a variable to refer to the 2nd Userform in the first as this gives you greater control over the second forms status - you can unset the variable after you have closed the 2nd form and returned to the event handler that instantiated it in the first place. This guarantees that each time you click the button to launch UserForm2 that you are working with a 'fresh copy'.
Also you should use the QueryClose event - see MSDN. This event:
Occurs before a UserForm closes
You need to be able to be able to control the re-activation of UserForm1 before the form is disposed.
The Terminate event is slightly different:
Occurs when all references to an instance of an object are removed from memory by setting all variables that refer to the object to Nothing or when the last reference to the object goes out of scope.
In the sample code below I am getting the correct behaviour using a reference to UserForm2 and handling QueryClose.
In this sample code I am using UserForm1 and UserForm2 but you can substitue for A and B in your app:
Code in UserForm1:
Option Explicit
Private m_frm2 As UserForm2
Private Sub CommandButton1_Click()
Me.Hide
Set m_frm2 = New UserForm2
m_frm2.Show
Set m_frm2 = Nothing
End Sub
Code in UserForm2:
Option Explicit
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
UserForm1.Show
Unload Me
End Sub
My problem is fairly trivial: I need to apply logic to the delete button in Excel. In a related question I asked for means to clear cells in a pivot table, and realising now that this might not be the right approach, this is yet another alternative I'm considering. Unfortunately, I admittedly have little experience with Visual Basic, and this task is proving such a challenge (quite surprisingly).
Is it possible to trap (as in listen to) the delete-key in Excel and trigger a subroutine in VBA when pressed?
I'd appreciate all kind of input! Thanks!
Yes.
You case use Application.Onkey to assign code to keys
If you
(1) add this code to a sheet module that contains your pivottable (right-click your sheet tab, View Code and past the code below)
This code activates and then de-activates the intercept when the user enters then leaves this particular sheet:
Private Sub Worksheet_Activate()
Application.OnKey "{DELETE}", "Intercept"
End Sub
Private Sub Worksheet_Deactivate()
Application.OnKey "{DELETE}"
End Sub
(2) add this code to a regular module
Sub Intercept()
MsgBox "user just pressed delete"
End Sub
then delete on that sheet will be trapped and the user given a message