How to get a macro to update the document?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
I have a macro that changes the colors in the selected text. Before we upgraded to Win 10 and Office 365, I'm pretty sure the page used to show the results as the marco operated. Now it doesn't until I exit the macro.

What command do I need to execute in the macro to get it to show the results while the macro is still working?

In Excel, I think the command is something like Activesheet.refresh, but I can't even find that.

Thanks
 
I believe this is in Word...

Try adding DoEvents after the If MsgBox... line.
Thank you, Rory. That did it. Here's the final (test) code:

VBA Code:
Sub RandColorsTemp()

Dim RGBRed As Long:    RGBRed = RGB(255, 0, 0)
Dim RGBGreen As Long:  RGBGreen = RGB(0, 255, 0)
Dim NextColor As Long
Dim Char As Range
Dim msg As String

'Application.ScreenUpdating = True
NextColor = RGBRed
For Each Char In Selection.Characters
  If NextColor = RGBRed Then
    NextColor = RGBGreen
  Else
    NextColor = RGBRed
  End If
  Char.Font.Color = NextColor
  msg = "'" & Char & "' done, continue?"
  If MsgBox(msg, vbYesNo) <> vbYes Then Exit For
  DoEvents
Next Char

End Sub

I also commented out the Application.ScreenUpdating statement, which doesn't appear to be necessary.

Thank you
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
PS: Is there a reason you chose to put the DoEvents after the If statement rather than immediately after the Char.Font.Color statement?

I moved it up there and it seems to work the same:

Rich (BB code):
For Each Char In Selection.Characters
  If NextColor = RGBRed Then
    NextColor = RGBGreen
  Else
    NextColor = RGBRed
  End If
  Char.Font.Color = NextColor
  DoEvents
  msg = "'" & Char & "' done, continue?"
  If MsgBox(msg, vbYesNo) <> vbYes Then Exit For
  DoEvents
Next Char
 
Upvote 0

Forum statistics

Threads
1,223,983
Messages
6,175,777
Members
452,668
Latest member
mrider123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top