How to get a macro to update the document?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,676
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
 
Selection needs to be a single cell. If a range is selected the len function will be passed an array instead of a string yielding a type mismatch error.

If the original goal was for this to work on a range of cells then it would need to be modified.
If that is the case, I would probably recommend adding in some checking for that to avoid errors.

You can get the number of cells in a selection with this calculation:
VBA Code:
Selection.CountLarge
So you can check to see if that is greater than 1 (and if it is, return some sort of warning & exit).

Or, you can just use ActiveCell instead of Selection, which will always just be one cell (if a multi-cell selection, it is the first cell in that selection).
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
try

VBA Code:
Sub RandColorsTemp()
    Dim RGBRed As Long:    RGBRed = RGB(255, 0, 0)
    Dim RGBGreen As Long:  RGBGreen = RGB(0, 255, 0)
    Dim Colors, i As Long
    Application.ScreenUpdating = True
    Colors = Array(RGBRed, RGBGreen)
    For i = 1 To Len(Selection)
        Selection.Characters(i, 1).Font.Color = Colors(i Mod 2)
        If MsgBox("Continue", vbYesNo) <> vbYes Then Exit For
    Next i
End Sub
Your code gets a compile error on the Selection.Characters line. It was not immediately obvious to me how to fix it, so I just added the screenupdating line to my code. It still works the same.

Here's my 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("'" & Char & "' done, continue?", vbYesNo) <> vbYes Then Exit For
Next Char

End Sub

Here's the text before running the marco:
1640202384239.png

After 1 iteration, the "M" is colored and the macro is still running.
1640203060185.png

After 2 iterations, the "e" is "done", but the screen is nopt updated:
1640203115925.png

After 7 iterations, everything up to the "C" have been colored, but the screen is not updated:
1640203250554.png

After the macro finishes and exits, the screen is updated.
1640203428623.png


Why does it show the first character, but not the rest?
 
Upvote 0
PS: The original code used a lot more than just 2 colors and it offered the user choices for the set of colors. It then chose them randomly. After the entire selection was colored, it would ask the user if was OK. If not, it would redo it. It did not stop after each character. I provided this simplified example to avoid posting hundreds of lines of code.
 
Upvote 0
A bit belated but had a quick play & came up with this

VBA Code:
Sub RandColorsTemp()
    Dim NextColor   As XlRgbColor
    Dim i           As Long
    
If Selection.CountLarge > 1 Then Exit Sub

    For i = 1 To Len(Selection)
        NextColor = IIf(NextColor = RGBRed, RGBGreen, RGBRed)
        Selection.Characters(Start:=i, Length:=1).Font.Color = NextColor
    Next i

End Sub

Not sure if it is what you were looking for but may give some further idea.

You will note that I have declared your variable NextColor as XLRgbColor the built-in enumeration for RGB colors. XlRgbColor enumeration (Excel)

I have also include @Joe4 suggestion to check for multi-cell selection

Hope Helpful

Dave
 
Upvote 0
Try changing it to ActiveCell

VBA Code:
If ActiveCell.CountLarge > 1 Then Exit Sub

and if still have issue try

VBA Code:
If ActiveCell.Count > 1 Then Exit Sub

the main purpose of my post was to show the built in XLRGBColor enumeration which negates the need to do this

VBA Code:
Dim RGBRed As Long:    RGBRed = RGB(255, 0, 0)
Dim RGBGreen As Long:  RGBGreen = RGB(0, 255, 0)

Dave
 
Upvote 0
Try changing it to ActiveCell

VBA Code:
If ActiveCell.CountLarge > 1 Then Exit Sub

and if still have issue try

VBA Code:
If ActiveCell.Count > 1 Then Exit Sub

the main purpose of my post was to show the built in XLRGBColor enumeration which negates the need to do this

VBA Code:
Dim RGBRed As Long:    RGBRed = RGB(255, 0, 0)
Dim RGBGreen As Long:  RGBGreen = RGB(0, 255, 0)

Dave
I don't believe that ActiveCell can ever be anything more than one cell, can it?
If so, that line would really serve any purpose.

Not sure why "CountLarge" would return any sort of error. It is a valid VBA property.
 
Upvote 0
I believe this is in Word...

Try adding DoEvents after the If MsgBox... line.
 
Upvote 0
Solution
I don't believe that ActiveCell can ever be anything more than one cell, can it?
If so, that line would really serve any purpose.

@Joe4 Its what you call a senior moment which I seem to have plenty of these days.
I did realize the daft error after I had posted but too late to delete it.

Dave
 
Last edited:
Upvote 0
@Joe4 Its what you call a senior moment which I seem to have plenty of these days.
I did realize the daft error after I had posted but too late to delete it.

Dave
No worries.
I find myself having more and more of those these days too!
 
Upvote 0

Forum statistics

Threads
1,223,367
Messages
6,171,669
Members
452,416
Latest member
johnog

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