I am having an issue where I have some vba code that runs different functions. I have built out a userform that keeps the user updated on the progress. This isn't a progress bar. It is simply changing a label text. However, when I run it, nothing changes. The code does not finish until I exit out of the userform. Does anyone know how to fix this? The code is listed below. Thanks!
frmALL.Show
xUpload ("DEV")
frmALL.devProgress.Caption = "Complete!"
frmALL.devProgress.ForeColor = vbGreen
frmALL.qaProgress.Caption = "Uploading"
xUpload ("QA")
frmALL.qaProgress.Caption = "Complete!"
frmALL.qaProgress.ForeColor = vbGreen
frmALL.prodProgress.Caption = "Uploading"
xUpload ("PROD")
frmALL.prodProgress.Caption = "Complete!"
frmALL.prodProgress.ForeColor = vbGreen
frmALL.Header.Caption = "Success!"
devProgress, prodProgress and qaProgress all repersent labels in the userform. When I set the forms showModal=false, then it just appears as a white screen until it's done with the code then shows all "Success". This is an issue because it doesn't show the progress like I thought it would. Does my code run too fast? Thanks in advance!
Will try to give a quick example of the _Change() usage based on your code:
Sub Open_UserForm()
'make sure you have a userform_initialize subroutine for base info
frmALL.Show vbModeless
End Sub
In the code for your userform, you might have:
Private Sub ComboBox1_Change()
'ASSUMES DEVPROGRESS IS TRIGGERED BY COMBOBOX1 VALUE CHANGE
xUpload ("DEV")
frmALL.devProgress.Caption = "Complete!"
frmALL.devProgress.ForeColor = vbGreen
frmALL.qaProgress.Caption = "Uploading"
End Sub
Example images... background code for my exmaple:
Private Sub UserForm_Initialize()
ComboBox1.List = Array("cat", "dog")
End Sub
Private Sub combobox1_change()
Label1.Caption = "label 1 = updated"
End Sub
Userform from beginning (pressed F5 from IDE to launch):
Me selecting something in combobox 1:
Combobox1 value changed, label updated:
Related
I am stuck with a simple checkbox on excel vba userform. I have the following code in its Click event:
Private Sub CheckBox1_Click()
Me.Label1.Caption = "Checked"
Me.Label1.ForeColor = vbRed
End Sub
When I click the checkbox1 in userform, the Label's caption changes and forecolor too. But when I uncheck it, the label caption does't disappear. Where is my code wrong or am I firing the click event again?
Try this :
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Label1.Caption = "Checked"
Label1.ForeColor = vbRed
Else
Label1.Caption = "UnChecked"
Label1.ForeColor = vbBlack
End If
End Sub
After you paint it red, it won't turn green unless you buy another pot of paint. Nor will it change the caption it has been given without another instruction. Try this code.
Private Sub CheckBox1_Click()
Dim Cap As String ' = "" at this time
With CheckBox1
If .Value Then Cap = "checked"
Me.Label1.Caption = Cap
Me.Label1.ForeColor = IIf(.Value, vbRed, vbGreen)
End With
End Sub
The code intentionally demonstrates 3 ways by which you might achieve the desired result. The smartest is the one which, at once, uses the least amount of code and is the easiest to read (for you).
The VBA code below is meant to allow only certain users to tick & un-tick a checkbox. However, the problem is that if I check the box and then close the spreadsheet, when I re-open the excel file the 'tick' is no longer there. It's like the code does not save the 'tick' action. Basically, if I check the box I want that to stay like that, even after closing the spreadsheet. In the VBA code below i added ThisWorkbook.Save in order to save the "Tick" action but it simply saves the spreadsheet instead of saving the "Tick" in the check box. Could you please advise what's wrong in my code? I've asked this question before but unfortunately nobody seemed to be able to find a solution. thanks so much
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
If (UCase(Environ("username")) = "TESTNAME") Then
'Do nothing
Else
'Uncheck because user not matching
CheckBox1.Value = False
MsgBox ("You are not authorized to tick this box.")
End If
End If
ThisWorkbook.Save
End Sub
on my sheet1 I have a commandButton (bShow) and in the code behind:
Private Sub bShow_Click()
UserForm1.Show
End Sub
then I have a UserForm named UserForm1 with a checkbox (named checkBox1) and a button named saveButton, and with a code behind:
Private Sub saveButton_Click()
Sheets(1).Cells(1, 1).Value = UserForm1.CheckBox1.Value
End Sub
with this setup in cell("A1") appears TRUE or FALSE depending on the checkbox state
i hope it helps
EDIT1:
by opening the Form reading the value from the sheet:
Private Sub UserForm_Activate()
UserForm1.CheckBox1.Value = Sheets(1).Cells(1, 1).Value
End Sub
EDIT2:
be aware of error handling (eg.: what if cell value is neither TRUE nor FALSE) But that I leave to you
I am trying to figure out how to get a textbox in an excel userform to update like an HTML textbox. For instance, if you have a default value set to "First Name" and you click in that textbox, it will disappear and let you type in. Once you click out, if you didn't type anything, it will go back to the default "First Name".
I have found quite a few asking about this same type of thing, but none are working for me. Here is my code so far.
Sub QCToolsForm()
QCTools.Show vbModeless
End Sub
Private Sub RUBSTotalExpense_Enter()
Debug.Print "Enter"
If Me.RUBSTotalExpense.Value = "Total Expense" Then
Me.RUBSTotalExpense.Value = ""
End If
End Sub
Private Sub RUBSTotalExpense_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exit"
If Me.RUBSTotalExpense.Value = "" Then
Me.RUBSTotalExpense.Value = "Total Expense"
End If
End Sub
Private Sub UserForm_Initialize()
updateDefault
End Sub
Public Function updateDefault()
If Me.RUBSTotalExpense.Value = "" Then
Me.RUBSTotalExpense.Value = "Total Expense"
End If
End Function
The exit sub is not running until I close the userform. I have also tried lose and getfocus with no luck with those at all. I have also tried before and afterupdate, but they only work the first time. I want it to disappear every time you click in the box if the value is "Total Expense". How do I get the exit sub or something similar to run when I leave the textbox?
I tried using your code for Enter and Exit and it worked for me with a test userform. It's updating the field correctly, and printing "Enter" when you select the textbox and "Exit" when you select something else on the userform. Note that for Exit to trigger you need to give focus to something else on the userform. Simply clicking elsewhere will not take focus from the textbox, you must click on another textbox or other control.
I suggest using Debug.Print to observe exactly when each method you try is being called, so that you can be sure when your code is running.
Private Sub TextBox1_Enter()
Debug.Print "Enter"
If TextBox1.Value = "Total Expense" Then
TextBox1.Value = ""
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "Exit"
If TextBox1.Value = "" Then
TextBox1.Value = "Total Expense"
End If
End Sub
A basic simple VBA question that I havent been able to work out even though there are numerous tutorials about it.
I want a Userform to pop-up, give you two options (OptionButton1 and OptionButton2). You choose one, click ok. And depending on what Option is chosen a certain value is used (in an email, for which I DID finish the macro). For simplifying purposes i now just want the variable 'contents' to be printed in cell A1.
I have these parts so far:
Sub MailOption()
UserForm1.Show
End Sub
Private Sub OptionButton1_Click()
If OptionButton1.Value = True Then Var1 = "Text example 1"
End Sub
Private Sub OptionButton2_Click()
If OptionButton2.Value = True Then Var1 = "Text example 2"
End Sub
Private Sub SendEmail_Click()
Cells(1, 1).Value = Var1
End Sub
There are multiple problems: the variable is not shown in Cell A1 and when i press Send Email the Form is not closed. I am probably doing lots of stuff wrong but its the first time im using a userform. Ty very much
I would simply use this one without handling OptionButton_Click:
Private Sub SendEmail_Click()
Dim res As String
If OptionButton1.Value Then
res = "Text example 1"
ElseIf OptionButton2.Value Then
res = "Text example 2"
Else
res = "Nothing is selected"
End If
'write result in cell
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = res
'close form
Unload Me
End Sub
I have a textbox on a userform. It is the only textbox on the form. There are three labels and two buttons in addition to this textbox. Basically, I want the focus to remain on this textbox under all scenarios, other than the moment that one of the buttons would be clicked, but then I want the focus to come right back to the text box. Both buttons have "TakeFocusOnClick" and "TabStop" set to False. I was having problems with getting the focus set to the textbox, which is why I changed these two settings.
Once I changed these settings, the Enter key in the textbox stopped having any effect. I have events written for _AfterUpdate and _KeyPress for the textbox, but they don't fire. As you can see in the code, I have commented out the lines to set the focus to this textbox. Since it is now the only object that can take focus, these lines are not needed (theoretically). When I allowed the other objects to take focus, these lines weren't having any effect (focus was switching to the buttons despite these SetFocus lines).
Here is the code. It is very simple, except that the Enter key isn't triggering the event. Can anyone see why? Thanks.
Private Sub btnDone_Click()
Application.Calculation = xlCalculationAutomatic
formMath.Hide
'Clear statistics
Range("attempts").Value = 0
Range("correct").Value = 0
Sheet5.Range("A2:W500").ClearContents
End Sub
Private Sub btnSubmit_Click()
recordAnswer
'formMath.txtAnswer.SetFocus
End Sub
Private Sub txtAnswer_AfterUpdate()
recordAnswer
'formMath.txtAnswer.SetFocus
End Sub
Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
recordAnswer
End If
End Sub
Private Sub UserForm_Initialize()
'Initialize manual calculation
Application.Calculation = xlCalculationManual
Application.Calculate
'Initialize statistics
Range("attempts").Value = 0
Range("correct").Value = 0
Sheet5.Range("A2:W500").ClearContents
'Initialize first problem
newProblem
End Sub
Sub recordAnswer()
'Update statistics
Dim attempts, correct As Integer
attempts = Range("attempts").Value
correct = Range("correct").Value
Range("results").Offset(attempts, 0).Value = attempts + 1
Range("results").Offset(attempts, 1).Value = lblTopNum.Caption
Range("results").Offset(attempts, 2).Value = lblBotNum.Caption
Range("results").Offset(attempts, 3).Value = lblBop.Caption
Range("results").Offset(attempts, 4).Value = Range("Answer").Value
Range("results").Offset(attempts, 5).Value = txtAnswer.Text
If (Range("Answer").Value = txtAnswer.Text) Then
Range("results").Offset(attempts, 6).Value = 1
Else
Range("results").Offset(attempts, 6).Value = 0
End If
'Update attempts and success
Range("attempts").Value = attempts + 1
Range("correct").Value = correct + 1
newProblem
End Sub
Sub newProblem()
Application.Calculate
formMath.lblTopNum.Caption = Range("TopNum").Value
formMath.lblBotNum.Caption = Range("BotNum").Value
formMath.lblBop.Caption = Range("ProbType").Value
formMath.txtAnswer.Value = ""
'formMath.txtAnswer.SetFocus
End Sub
To start off
You can either in the design mode, set the TabIndex property of the Textbox to 0 or you can set the focus on the textbox in the UserForm_Initialize()
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Similarly after any operation that you perform, simply call the TextBox1.SetFocus to revert to the textbox.
Option Explicit
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub
Private Sub CommandButton1_Click()
MsgBox "Hello from Button 1"
TextBox1.SetFocus
End Sub
Private Sub CommandButton2_Click()
MsgBox "Hello from Button 2"
TextBox1.SetFocus
End Sub
Let me know if this is not what you want?
I found a way to accomplish this. In the code above I took out the _KeyPress and _AfterUpdate events and replaced them with:
Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 13: recordAnswer
End Select
End Sub
Not sure why the other methods didn't work, but this does.
Also not sure why simply setting the focus directly didn't work. I suspect that the focus was being set, but then something else was happening subsequently that was changing the focus off of the textbox. Just a guess.
Thanks for the help. I appreciate it.