Userform keeps clearing values when reopened - excel

How do I keep the values that where inputted into the excel Userform from clearing itself out once I click on the finish button? So whenever I call upon the Userform through a Commandbutton the previously filled field information is left the way it is and not to be cleared basically.
This Userform consist of MultiPage which is designed to take in the inputs from the user and place them in excel cells once the user has hit the finish button. Within the Userform contains Commandbuttons which toggles an hide/unhide feature which will also have to be saved somewhere
The Finish page (where the user will close/exit the page) looks like this:
Code for the finish button so far:

I'm not sure why everyone is saying there is no way to do this - you should be able to use:
Private Sub BtnFinish_Click()
Me.Hide
End Sub
And all previous values will be kept. Someone correct me if I'm wrong :).
I should add that once the EXCEL.EXE is closed, the values are lost.

Related

Stop message box on a given situation Excel VBA

I am creating a userform using Excel VBA that is meant to be used to register some sales. The form looks like this:
As you may have noticed, I am using an image as a button. This is because the CommandButton included in VBA looks very outdated. However, using an image as a button also creates me a new error (or not, depending on how you see it) that is driving me crazy. The usual process for filling this is entering a product, a quantity, a price, a customer and clicking the button to save all the information to a worksheet. The payment textbox is only filled sometimes.
I created a data validation mechanism for all these fields, including the customer combobox. If the user types an invalid entry or leaves the field empty after clicking it, a message box appears. The code for that is the following:
Private Sub cmbCustomer_AfterUpdate()
If cmbCustomer.ListIndex > -1 Then
Else
MsgBox "Please choose a valid customer", vbExclamation
cmbCustomer.Value = ""
End If
End Sub
This works great for avoiding invalid entries. The tricky part is that, once the button is clicked, all the fields are automatically erased. The data is correctly saved, but if the last field used before clicking was cmbCustomer (or any other, actually, because all of them have a similar mechanism to avoid empty or invalid data) and the user decides to begin filling the form again starting by the product, the message box appears, because it is empty and the code detects the invalid entry. I know this is the expected behavior for my code, but this doesn't happen if I use a traditional CommandButton because when clicking it the focus goes to said button. If I use my image-based button the focus remains on the last text field used before clicking it.
One solution would be to override the message box in this specific situation (when saving the data). The second one would be to reset the focus of the form, or set focus to the image like what happens with a regular CommandButton. Any suggestions would be greatly appreciated.
You can do yourself what the traditional CommandButton does automatically: Set the focus where you want it:
Private Sub cmbCustomer_AfterUpdate()
If cmbCustomer.ListIndex > -1 Then
Else
MsgBox "Please choose a valid customer", vbExclamation
cmbCustomer.Value = ""
myButton.SetFocus
End If
End Sub
If SetFocus doesn't work, be mindful of where you are in the UI event chain: Why is my .setfocus ignored?
As mentioned in the comments, an image button can't acquire the focus. A transparent CommandButton behind it can be used as a proxy.

Userform needed only once

I'm a new VBA user. I have a workbook with multiple worksheets. Each one has it own userform for data entry. The userform shows when I click on the sheet. After I'm done entering data, the worksheet is populated and the userform closes (unload). All this works well. However, after the initial data entry is complete, the goal is to use the data on the worksheets for other applications and the userform is no longer needed. What is the code or the terminology to say the userform should not reappear again when the worksheet is clicked on? Currently, I red X out of the userform. If I click the command button to close, it repopulates and I lose all my data.
Thanks!
As A.S.H commented; you could store the information in a number of ways. An easy example is declaring a variable outside of the Macro:
Public FormOpened as boolean
And then set FormOpened as true once the form has been shown. Then you could add a check to the start of the mouse-click macro:
If FormOpened = True then Exit sub

DeActivating an Excel userform

I have a code which processes an excel sheet row by row.
Under certain conditions, certain manual manipulations are to be done on the data in the rows. For this, the program fires a UserForm. The UserForm contains certain fields which are populated from the underlying excel rows. These fields are then manipulated by the user and certain buttons on the userform are clicked. Once the manual processing is done, the program then needs to proceed with the rest of the rows in the Excel sheet. When the UserForm is triggered, before taking action on the USERFORM, user needs to to see
a) What is pushed up in the UserForm
b) Contents of the Underlying Excel Sheet
The program needs to wait till the user takes action
However, there seems to be no easy way to let the user see the underlying rows as the Userform (on top) prevents access to the Excel Sheet below. To allow access to the Excel Worksheets, I tried using
UserForm.Show vbModeless
UserForm.Show False
Both of these are totally bypassing the Userform and the program runs for the remaining rows without waiting for the user input.
I researched here and found this.
deactivate Excel VBA userform
In my Case, the MainProgram calls the UserForm and has to wait for the user input in the USERFORM before processing further rows. If the conditions triggering the userform are not met, the program proceeds to the next rows.
Help highly appreciated. Thanks in advance.
EDIT:
As a workaround I have written a line to select the cell where the manual intervention is triggered. This brings that cell right under the manual intervention UserForm. The UserForm can now be moved around to look at what is below.
How about adding a de-activate Button to your form using this script:
Private Sub CommandButton1_Click()
UserForm1.Hide
MsgBox "hit OK to continue", vbOKOnly, "Script Paused"
UserForm1.Show
End Sub

VBA textbox changes trigger macro

I am trying to monitor a sheet which has several text boxes on it.
The idea behind it is the user enters text into these boxes and then you can click the submit button to send it all to a sql database.
Now I want to have it so if the user has made changes, and they go to leave the sheet a macro is triggered to tell them that they haven't saved it.
I've already got this triggering on the deactivate worksheet event, but I was wondering if I can monitor all of the textboxes on the sheet (oleobjects) under one change event
I am already assuming this isn't possible with just one but was hoping.
Thanks in advance
Tom
One way to do this would be to write a separate subroutine that is called within the Change event of all the Textboxes. Keep in mind though that this will be raised every time the Textboxes change at all- every keystroke.
In each TextBox_Change Event:
Private Sub TextBox1_Change()
TextChanged TextBox1
End Sub
New subroutine:
Sub TextChanged(fromTextBox As TextBox)
'validation code goes here
End Sub

Keep the previous userform open after Unload.Me using Excel VBA

I use the Unload.Me function to close a userform.
I have two userforms, when I close one, the second userform will close automatically as well.
I want try to keep the previous userform open.
A quick workaround, try to specify the exact form you are unloading.
i.e. use Unload UserForm2
If that doesn't help, check if the forms are modal forms. If not, set them to modal.
I tried adding a subsequent instruction in the same sub procedure that called/showed Userform2 (thinking that it might not close Userform1 until it finished the rest of the code after Userform2.show). Instructions like assigning a random value to a random variable did not work, but making a message box pop up did work. For example in the sub procedure calling userform2 you might try:
Userform2.show
Msgbox("Userform2 has already closed. Close this message box to see Userform1.")
This seems despotic to me too, but it worked.
This's caused by a code that executes when the form that you don't want to close becomes the active form.
It's 2022 now, but anyway, for those interested.
You do not need a messagebox, you can just set focus to an item in userform1.
That way you avoid having to click OK to close the messagebox

Resources