I'm using several excel files and each one is making some calculations using VBA. Now I'm working on one file which will run each file one after another and calculate them all with one click. Unfortunately at the end of calculation for each file there is Msgbox "Report updated" and my macro stops and is waiting to confirm "OK" manually before it will close first file and open next one.
I cannot remove this lines with msgboxes because some people will use just one file and they need to be informed that everything is finished.
And here is my question: How can I close this msgbox automatically?
Thanks in advance
I think it is impossible, because MsgBox is modal. But why your macro displays it at first? You may add conditional instruction that checks if the sheet on which it works is displayed. Something like:
If ActiveSheet.Name = wsProcessedSheet.Name Then MsgBox "Report updated"
So, the message will be displayed for normal user and not for your supermacro. Alternatively, you could disable the MsgBox only for your machine:
If Environ("username")<> "your user name" Then MsgBox "Report updated"
Thank you for interesting ideas.
I've just figured out different solution. Originally I had button which run macro Update()
Sub Update()
...calculations....
Msgbox "Report updated"
End Sub
Now I removed msgbox from this macro and created two macroslike below:
Sub ButtonClick()
Call Update
Msgbox "Report updated"
End Sub
Sub Update()
...calculations....
End Sub
And now button is running macro ButtonClick and in external file I can use macro Update() which didn't contain msgbox.
But I still wondering if there is any way to confirm msgbox alerts
Related
I am trying to make basic msaccess form. And I created this save button through command button wizard. (Record Operations/ Save Record)
As you can see this save button's On_Click event =>>Embedded Macro.
I wanna write some additional code after insertion has occured.
Private Sub Command28_Click()
MsgBox "Done!"
End Sub
My question is: If On_Click=Macro; button save function works but msgbox code doesnt work. If On_Click=Procedure "Done!" message appears but save function doesnt work. How to work both of them (save function and message; in other words embedded macro and VBA code)
This should be so simple BUT!
My opening form displays a dropdown to select user name (filled from a select query) and an unbound text box to enter password. The password is compared with a fixed string and if it matches I need to close this form and open the main menu. There is also a button to close the whole system if you don't know the password.
The tab order is dropdown, text box, exit button.
After many varients of AfterUpdate, BeforeUpdate,lostFocus I put this code in the Got Focus event of the exit button, so that exit from the password box will trigger it. Each function sets the focus back to an appropriate incorrect entry but if all is correct the program flows to the last two lines.
Private Sub btnExitAll_GotFocus()
If PasswordNotOk() Then Exit Sub
If UserNotOK() Then Exit Sub
DoCmd.OpenForm "Main Menu"
DoCmd.Close acForm, "Opening screen", acSaveNo
End sub
The "Close" line fails with 2585. I have tried reversing the order of the last two lines.
I have even put the last two line in the click event of an temporary button, where they work exactly as intended! So I tried calling that button's click event from this sub, but get the same error.
I have tried Unload me but that throws error 361 Can't load or unload this objectbut I just can't get this form to close itself.
If it helps, when I tried to do this in the before update event of the UNBOUD text box I got errors saying I couldn't do this before I saved the data.
Please someone what am I doing wrong?
I am using Me.Name to close a form from itself. You might need to clear changes before you close.
Private Sub btnExitAll_Click()
If PasswordNotOk() Then Exit Sub
If UserNotOK() Then Exit Sub
DoCmd.OpenForm "Main Menu"
' Discard changes so we can close without saving
If Me.Dirty = True Then
Me.Undo
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
I am currently working on an project that contains forms. Therefore users pick a specific type of form from the drop down list. How do you create an alert in excel using macro when a user picks certain type of form from the dropdown list. And after completing the form and before changing it I want to issue an alert saying that please make sure to Save the form. How is this done in excel.
Thank you for your help.
You can using event "Change" in Combobox
Private Sub ComboBox1_Change()
MsgBox "Value changed"
End Sub
Or check value after change, like this
Private Sub ComboBox1_Change()
If ComboBox1.Value = "ABC" Then
MsgBox "Value changed"
End If
End Sub
And about "alert Save the form", because I don't know your form, so I can't give you ideal.
Have Excel (2010 in my case but I think it will be the same also in other versions) with two workbooks. Save first one with name "1.xlx" (example) via Save As dialog. Save second one with the same name "1.xlx" to different location. Excel will not allow it with following warning message:
"You cannot save this workbook with the same name as another open workbook or add-in. Choose a different name, or close the other workbook or add-in before saving."
So far so good. But my problem is that I need to invoke dialog via VBA. I am using following code:
Sub test()
Application.Dialogs(XlBuiltInDialog.xlDialogSaveAs).Show
End Sub
Now I am trying to save second workbook (with the same name to different location) but when I click to 'Save' button nothing happen, no warning message. If I wouldn't know what is wrong it would be very difficult to tell. I didn't change any setting (there is nothing as DisplayAlerts set to true or so). Any idea how make SaveAs dialog invoked via VBA to display similar warnings?
I'm not sure why that doesn't give you an error, but it doesn't me either. If you use Application.FileDialog, you can get that error.
Sub testts()
With Application.FileDialog(msoFileDialogSaveAs)
.Show
.Execute
End With
End Sub
Or you could use GetSaveAsFileName and check the names of all the open workbooks and generate the error yourself.
Can you try with the below code on starting on your code.
Application.DisplayAlerts = True
Upon pressing my CommandButton, I would like to have a pop-up that asks "These changes cannot be undone. It is advised to save a copy before proceeding. Do you wish to proceed?"
And I want to have three options:
Yes - pop-up window is closed and CommandButton Macro is executed
No - This closes the pop-up window and changes nothing
Save - closes pop-up window and opens "Save As" (macro is not executed)
I don't really know where to start with this. Could you please give me a hand?
Thank you very much indeed.
You can use a message box, but that is somewhat limited. You can rephrase the question slightly to use the vbYesNoCancel buttons, since Save As is not an optional button on Message Box.
Then you can work with the result of the message box button-click:
Dim mbResult as Integer
mbResult = MsgBox("These changes cannot be undone. Would you like to save a copy before proceeding?", _
vbYesNoCancel)
Select Case mbResult
Case vbYes
'Modify as needed, this is a simple example with no error handling:
With ActiveWorkbook
If Not .Saved Then .SaveAs Application.GetSaveAsFilename()
End With
Case vbNo
' Do nothing and allow the macro to run
Case vbCancel
' Do NOT allow the macro to run
Exit Sub
End Select
I suggest you put code at the top of your macro to ask this question and respond to the answer.
This would look something like:
Sub YourMacro()
if MsgBox("These changes cannot be undone. It is advised to save a copy before proceeding. Do you wish to proceed?", vbYesNo + vbQuestion) = vbNo then
exit sub
end if
... the rest of your macro.
Note that this will not give the user the Save option. You can't do that with a standard MsgBox. If you want to do that, you will need to create your own userform, show that instead of the MsgBox and respond to what button in the Userform the user pushed. That is a lot more work for you than just using a MsgBox, but if you want your UI to be fancy, it may be worth it.