UserForm vbmodeless - vba

How can I keep a Userform in modeless on top and update the text while operation is running? I am trying to my UserForm on top while operation is running and update the text on the UserForm as module works through different sections. The form gets pushed to the background by the continuing routine and you can't read it until the routine is finished and I change the text to "Scan completed".
Ex:
With Notice
.caption = "Gain/Loss Tables"
.text = "Starting the scans"
.show vbmodeless
end with
At certain points I wish to update the text to indicate progress
with Notice
.text = "Starting scan for new members"
.show vbmodeless
end with
I would appreciate any suggestions
Sincerely,
Hobart Gay
I have attempted using Timevalue delays of 2-3 seconds to allow the update with no success.

Related

Pause VBA Word macro, allow user to make a selection, and restart where it left off

I have a requirement for VBA script in Microsoft Word to pause so that the user can select text that will be copied to the clipboard so that it can exported to an Excel file. The user will make a number of selections and finally indicate he/she is done when the contents of the clipboard will be copied to a template Excel file.
I have the code working to copy each selection to the clipboard and then all rows to the Excel file. But I need assistence in figuring out how to pause the code to allow the user to make the selection and then restart the code to copy the selection to the clipboard. I am able to get the userform with toggle switch to switch states and labels when pressed. But have not figured out how to pause the VBA code to allow the user to navigate to the next section of the Word document for the next selection.
The Stakeoverflow question/answer below appears to address this requirement but I have not been able to get it to work. It appears that the code is incomplete.
Pause VBA macro, allow user to make a selection, and restart where it left off
Can someone provide example VBA code that accomplishes this?
Your assistence is much appreciated as I have been beating my head against the wall and it is starting to hurt!
There's no way in VBA to "pause" a macro. Code must run to completion... Unless there's a command for user input.
Input can be requested via the InputBox and MsgBox methods, but those block access to the document because they're modal. A UserForm, however, can be set to display as non-modal, meaning it stays on top, but doesn't block access to the document or the application features. Since you're already working with a UserForm, this can be implemented relatively easily.
In the small example below, the Continue button runs the code to perform an action on the user selection. When Done is clicked the entire code is exited and the form unloaded.
Code behind the user form
Option Explicit
Private Sub cmdContinue_Click()
Debug.Print Selection.Range.Text
End Sub
Private Sub cmdDone_Click()
Me.Hide
End Sub
Private Sub UserForm_Activate()
'Position the form near the top-left of the window
'So that the user can work with the document
Me.Top = Application.ActiveWindow.Top + 50
Me.Left = Application.ActiveWindow.Left + 50
End Sub
Code in a regular module
Option Explicit
Sub DisplayModeless()
Dim frm As frmModelessForInput
Set frm = New frmModelessForInput
frm.Show False 'Display as non-modal
Set frm = Nothing
End Sub

Set focus on textbox after performing print command

Please help me case below:
When I write code for print command by vba then it run OK.
But after run print command then cursor don't focus on textbox.
what do I must use command to cursor focus on textbox?
Thank you so much.
If TextBox1.Text = "PRINT" Then
ActiveSheet.PrintOut
TextBox1.Value = ""
Call CData
ThisWorkbook.Save
ThisWorkbook.Activate
UserForm1.TextBox1.SetFocus
End If
Because I am writing macro for scan barcode on textbox, so I need to automatic process after perform print, it can continue receive scan barcode on textbox, no must click on textbox before scan barcode.
Check first if there is any error message and where your code is located (module, userform, worksheet or workbook code).
Are you sure that your If TextBox1.Text = "PRINT" condition is executed? Check this by inserting a beep command, some Debug.Print info for the immediate window or a Stop command allowing you to check code continuation manually by pressing F8.
You could try Windows(ThisWorkbook.name).Activate instead of only ThisWorkbook.Activate, especially if you are using more than one workbook and do some selection or activating.
If the TextBox situated on a page of a MultiPage control, you should activate this page first: e.g. Multipage1.Value = 0 (...) and then set focus via TextBox1.SetFocus.
Don't reference :-) UserForm1.TextBox1.SetFocus, but use Me.TextBox1.SetFocusor TextBox1.SetFocus :-) if this is code withIN your UserForm module.
You could also perform a Click() event on your TextBox, though that isn't best programming style: TextBox1_Click

Allow user to make entries on worksheet during run-time

All:
Thank you in advance, you all have been a tremendous resource!!!
I have a couple of spreadsheets where the sheet is protected, but users can still use filters. I'm processing most of the sheets automatically, but what I need to do, is present the user with the sheets that need to be filtered, then have them select a "Finish" type button or toolbar entry, which I already have.
What I need to be able to do, is to bring this sheet up, pause the macro, if possible, while they make their changes (could be up to 5 filters that they select before the sheet is ready.
Then, copy the visible cells only to a specific sheet and then resume the macro.
I don't think that Worksheet change event will do this.
I'm thinking more on the lines of maybe setting a flag on a spare sheet, firing up the next macro and then see if it can find the original macro and pick up where it is flagged?
I thought about a modeless userform that the user could click OK on and then call the next macro, but that does not work.
The calling code is:
UserForm3.Show
CopyToDisplay "AEP"
LastPos = LastPos + 1
Where AEP is the sheet name to copy the filtered rows from.
Userform displays, but clicking ok does nothing and of course, the macro keeps on going.
Any suggestions would be greatly appreciated!
Thanks,
Jeff
Jeff let's try this. Your current code:
UserForm3.Show
CopyToDisplay "AEP"
LastPos = LastPos + 1
When we display a UserForm, the default behavior is vbModal, which essentially freezes the application and the user cannot interact with anything but the UserForm, that is not what you want. What you need is a way to display the form, and then just wait for the user to signal that s/he is finished with the input.
So we need to modify a few things:
The UserForm needs to effectively "pause" while also allowing the user to interact with the worksheet. A vbModal form can't do this (it pauses, without interaction), and really neither can a vbModeless (it continues execution AND allows interaction).
Conundrum? No. we can simulate a pause with the vbModeless form, and preserve the user's ability to interact with the sheet. The best of both worlds!!
We will show the form with the optional vbModeless, this allows the user to interact with the rest of the Application /worksheets/etc. Since a modeless form continues code execution, we need to simulate a pause and we can do this with a Loop. The loop will run indefinitely, and only break once the UserForm is closed, at which point the rest of the code will continue to execute.
UserForm3.Show vbModeless
Do While UserForm3.Visible
DoEvents
Loop
LastPos = LastPos + 1
'You MAY need to reset some variables, if the Filter/Autofilter has affected these/etc.
Design-wise, give your form a single Label control and set its .Caption property (and the form's .Caption property) in some useful/instructive way. You could add a command button but that seems unnecessary, since all the button would do is invoke the Terminate event (which can always be done with the red "X")
For your issue with copying (apparent failure to paste), try changing this:
Sheets("AEP").Select
With ActiveSheet
.UsedRange.SpecialCells(xlCellTypeVisible).Copy _
Destination:=Sheets("Display").range("A" & LastPos)
.AutoFilterMode = False
Application.CutCopyMode = False
End With
To this:
With ActiveSheet
.UsedRange.SpecialCells(xlCellTypeVisible).Copy
Sheets("Display").range("A" & LastPos).PasteSpecial
.AutoFilterMode = False
Application.CutCopyMode = False
End With

Can I display something while a macro is running?

I have a macro that takes roughly five seconds to execute and I'm using the code Application.ScreenUpdating = False so it looks like Word has locked up when it's running.
I use a message box at the end to tell them it's complete, but I'd like to display an image during its execution that tells the user the macro is running (primarily so they don't get worried, but also because it's nicer to look at).
There might be an easier way to go about this, but I decided to create an image, create a very basic user form, and simply set my image as its background. Then, I have that user form pop up as soon as the code starts to run.
The problem I'm having is that unless I close it, the rest of the code won't execute. It just hangs until I do something. Is there a way I can get the user form to be displayed without stopping the rest of the process? If not, is there another approach I can take to display an image while the macro is running?
You can show the userform as non-modal:
Userform1.Show False
'do more stuff (doesn't wait for form to close)
Userform1.Hide
I too have found that if you .show a userform at the beginning of the process, images contained within userform take a while to display and the result is less than smooth.
I have solved it by inserting a 1 second "wait" immediately after the userform.show instruction. In that split second, the image loads and displays successfully and the result is seamless.
Here the code. I have 2 macros: 1 to display and 1 to hide. I call the first macro at the beginning of the process just before freezing the screen and the second at the end of the process just before unfreezing the screen
Userform name is "myuserform"
Sub message_show()
userform.Show False
Application.Wait (Now + TimeValue("0:00:01"))
End Sub
Sub message_hide()
Application.Wait (Now + TimeValue("0:00:02"))
userform.Hide
End Sub

Control User Focus during processing VBA

When a user clicks "go" to run my vba application, it is processing data for about 30 seconds, is there any way to have a box come up during that time that says like "processing" or something, and not allow the user to click anywhere until it is done/goes away?
Thanks in advance,
Joe
Have you tried displaying a message box, then disabling updating the screen and events until the process is over? The user would be able to close out the message box, but they would be unable to interact with the sheet while you are processing. It would like something like this:
MsgBox "Please wait until the process is complete. This may take some time."
Application.ScreenUpdating = False
Application.EnableEvents = False
'Your Code Here
Application.ScreenUpdating = True
Application.EnableEvents = True