Microsoft Office 2007 Macro - Odd behavior. Cursor Jumps - vba

I hope someone have some insight into this.
I have a check box on page 1 and when it's clicked, it will launch a macro & insert a value to a text box on page 10. Simple
The problem is, the script in the macro is looking for the value of another text box on page 5 to do some calculation. Whenever i do
text5value = Val(oFFld("Text5").Result)
or
If (Val(oFFld("Text5").Result) = "") Then
The cursor will suddenly move from page 1 to page 5. Very confusing to user.
This behavior happens whenever I try to get the value of a textbox. Wherever that textbox is in the current document, it will jump to it.
Please help

Try setting Screen Updating to false for the duration of the macro.
Application.ScreenUpdating = False
Not only will this hide the various screen jumps it is doing while the macro runs, the macro will also run faster if it doesn't have to constantly update the screen.

Related

Show "loading" userform whilst another userform runs a routine in the background

I have a series of userforms (the intention is that there will be no interaction with spreadsheets) and i need one userform to run script in the background whilst another userform/window pops up and lets the user kmnow that the operation will take some time.
This is used in multiple instances (e.g. combobox_change with a label updating its value based on a loop that goes through 25k-30k lines).
I have tried:
code as per below (which throws back an error as (i believe) there can be a modal=True and Modal=False userform running concurrently:
z_loading.Show vbModeless
z_loading.StartupPosition = 1
'additional vba routine here
Unload z_loading
restructuring the location of code
Step 1: changing combobox or clicking button shows z_loading userform
Step 2: z_userform_initialize contains the vba code and ends with unload z_loading
Step 3: Another userform populates based on z_loading_initialize code
The problem with the above is that when the user closes the userform coming after is, it errors.
Apologies - found the solution.
Modal was true for all userforms. By making modal false the .vbmodeless method works.

Excel VBA - Pause code execution

I have a complex VBA function and a workbook with multiple sheets.
Let's say the code goes through every row of Sheet1 and does something with that data.
I would like to be able to pause on SourceRow = 16, check out results I'm getting on different sheets and then continue on (press ok, or some key stroke)
I've tried
If SourceRow = 16 Then
MsgBox "ReachedRow 16"
End If
But the Message box is modal and I can not switch to a different sheet(s) to see the data.
P.S. 16 is just as an example, hardcoded for now but will not be later on.
You can use the Stop statement and then press F5 to resume the code execution. This is akin to adding a breakpoint.
If SourceRow = 16 Then
Stop
End If
The row will be highlighted yellow while it is paused and you should be able to navigate work sheets.
Example:
In similar fashion you can use Debug.Assert (sourcerow <> 16) which will pause the code when sourcerow<>16 evaluates to false, that is to say when sourcerow equals 16. (Thanks to RBarryYoung for the comment)
If you don't want a permanent stop in your code, consider adding a breakpoint by clicking the gray region to the left of the editing window in the VB editor, which should will look like this:
The code will stop when the line is reached. Again press F5 to resume code execution. Alternatively you can continually press F8 in order to step through the code line by line. This eliminates the need to set many breakpoints.
Simply click the maroon circle to dismiss the breakpoint. Breakpoints are not saved in your code, so this is a temporary method (as opposed to the two methods listed above which would stay in your VBA code if you save the workbook).
The images were taken from this tutorial from Wise Owl regarding breakpoints.
Right-click on SourceRow (anywhere in your code) and click Add Watch....
In the dialog that appears, enter the following in the Expression textbox:
SourceRow = 16
Under Watch Type, select Break when value is True.
Click OK.
This will automatically cause your program to break/pause when the value is reached and there's no need to add any new code just for debugging/pausing purposes.

Access 2010 Me.Refresh/Me.Requery

I have a form that I want refreshed when the submit button is clicked. Preferably those that have default values will be restored and those without will be blank.
The submit button has an attached OnClick Macro that, checks to make sure all fields are filled, if so an action query runs that inserts a new row into a table.
So its after this action query that I want the refresh to occur. I have tried researching and come across suggestions that suggest using VBA code Me.Requery or Me.Refresh. I'm not 100% on how to incorporate this into the macro. The RunCode command doesn't recognize the function I put the code in, and The convert macro to VBA option in the top left is grey'd out.
I'm new to Access and just not seeing where the communications are for code and macros if someone could please elaborate for me I would be greatly appreciated.
Please consider my solution as a brief introduction to VBA. If you care to learn the language, you will find there is very little you cannot do.
Within your submit button properties, there should be an 'Event' tab. In this tab, the On Click should be set to [Event Procedure]. Click the three dot button just to the right of that, and it will launch the VBA editor.
All you need to have here between the Private Sub and End Sub lines are these lines of code:
DoCmd.RunMacro ("Mac_1")
Me.TextBox1.Value = "Null"
Me.CombBox1.Value = "Null"
Me.Refresh
MsgBox "Your Save Was Successful.", vbOKOnly, "Saved"
"Mac_1" is the name of the macro you want to execute. the ME.Refresh executes as soon as mac_1 finishes, and will refresh the page. Be sure to enclose it in a proper quote (") and not a double tick ('').
The Requery command can be used from both within VBA code or a macro. It sounds like you are using the macro designer so you can use the Requery macro action instead of VBA.
Just add this to your macro after you have inserted your new data.
This macro action lets you specify a control to requery. The parameter can be left blank to requery the source of the active object. More information can be found here.
Edit
In response to your comments, I think you should try experimenting with the SetProperty macro action (more details here).
I have attached this macro to a button click event and the value from the TextBox called txtInputValue is cleared. The Value field is left blank as you want to fully remove the value from the text box.

Microsoft office Macro - Why is the document jumping around?

This if the first time I write a Microsoft Office Macro.
Basically, we have a few check boxes on page 1 and when one of them is checked, I need to auto-fill a text box on page 10 depending on which check box was checked. So each checkbox "on Entry" runs a macro.
Say the options are "Outstanding", "Exceeded" and "Unsatisfactory". When "Outstanding" is checked, that text box will be filled with "O", when "Exceeded" is checked, the text box will be filled with "E"
So the issue right now is, whenever I check the check box on page 1, suddenly, the document will jump to Page 10.
All I did was doing something like
oFFld("Text23").Result = "O"
I need it to stay on the same page! If I got rid of the above code, setting a value to the textbox, then nothing happens and I will stay on page 1.
What's going on? Any advice is greatly appreciated it.
ps:
it's word 2007
Never mind.
I was using an evaluation expression as in
If OFFld("Text23").Result = "blah" Then
whenever I do that, it'll jump to the page 10. Took care of it in another way. solved.
See answer in Suppress unwanted jumping/scrolling on Word 2013 VBA Script for details, but essentially you want to use the Bookmark object rather than the FormField object to access the result property.

How to wrap comment.text in vba

I have a situation in which text in one of the cells is massive.When I change the contents of this cell the previous value becomes a comment to that cell.Now the problem is, this text is so big that I cannot see complete text.If I use .Shape.Textframe.Autosize=true then I have to go on browsing till god knows when to see the text.
What I need to do is whatever and however big the text might be in Commnet.text,I want to show it in one and one place only.i.e when I hover on comment.
Place a TextBox where you will on your worksheet adjusted to the size you want, this still reads the comment although this is not on a hover, but perhaps will help
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not ActiveCell.Comment Is Nothing Then
ActiveSheet.Shapes("Text Box 1").TextFrame.Characters.Text = ActiveCell.Comment.Text
Else: ActiveSheet.Shapes("Text Box 1").TextFrame.Characters.Text = ""
End If
End Sub
Actually after considering this further you really are trying to get comment to act as you want. So I recommend Resize all comments in the selected area and on the same link further down the page Show Excel Comments in Centre of Active Window.
These get you closer to what i think you want.
How about a pop-up text box? I.e., the user clicks on the cell and a pop-up form with the text appears. The best part is you can make the pop-up as big as you want.
Personally, I would make the pop-up resizable, but with only the Close button at top. I can't think of a way to trigger this form with just a mouse rollover, but I'm only using 2003. Later versions allow you more tricks.
xlSheet.Range(xlSheet.Cells(rowCount,1), xlSheet.Cells(rowCount,8)).Merge
xlSheet.Range(xlSheet.Cells(rowCount,1), xlSheet.Cells(rowCount,8)).HorizontalAlignment = 1
xlSheet.Range(xlSheet.Cells(rowCount,1), xlSheet.Cells(rowCount,8)).WrapText = True
xlSheet.Range(xlSheet.Cells(rowCount,1), xlSheet.Cells(rowCount,8)).RowHeight=45