Sie sind auf Seite 1von 8

Flashing Text In Excel

In Microsoft Word, it's ridiculously simple. Select Format | Font from the menu, click the Text Effects tab, and choose Blinking Background. You might expect similar help from Excel, but there's nothing remotely similar. Blinking text in Excel won't happen unless you make it happen with a little macro programming.

Suppose you want cell A1 to blink. Select Tools | Macro | Visual Basic Editor from the menu. Right-click the VBAProject item in the tree at left and choose Insert | Module from the pop-up menu. Now type and paste this text into the module:

Public NextFlash As Double Public Const FR As String = "Sheet1!A1" Sub StartFlashing() If Range(FR).Interior.ColorIndex = 3 Then Range(FR).Interior.ColorIndex = xlColorIndexNone Else Range(FR).Interior.ColorIndex = 3 End If NextFlash = Now + TimeSerial(0, 0, 1) Application.OnTime NextFlash, "StartFlashing", , True End Sub Sub StopFlashing() Range(FR).Interior.ColorIndex = xlColorIndexNone Application.OnTime NextFlash, "StartFlashing", , False End Sub

Arrange the VBA window and the Excel window so you can see cell A1 in Sheet1, then click within the StartFlashing macro, and press F5 to run it. The cell flashes! Click within StopFlashing and press F5 to stop the flashing.

How does it work? First, the constant FR defines the range that will flash; you can make different cells flash by changing its value. If the flash range has a red background, this macro sets it back to normal; if not, it makes the background red. Then it sets itself up to be called again in one second using the application's OnTime method.

The TimeSerial VBA function returns a numeric time-value corresponding to the number of hours, minutes, and seconds passed to it as input. TimeSerial takes only whole numbers, so the shortest time period it can calculate is the one second we use here. We set the NextFlash variable to the current time plus one second, and we call the application object's OnTime method to launch StartFlashing again at that time. Each call to the macro toggles the background from red

to normal or vice versa.

The StopFlashing macro simply restores the normal background and calls OnTime to cancel the pending event. To cancel an event, you have to pass the exact same time that was used to schedule it, which is why we had to store that time in the public variable available to both macros.

It's important to cancel all pending events when the workbook is closed, so we'll add an automatic macro to take care of that. And it should also start flashing automatically when the workbook is opened. In the tree at left of the VBA Editor, double-click the ThisWorkbook module and enter or Copy/Paste this text:

Private Sub Workbook_Close() StopFlashing End Sub Private Sub Workbook_Open() StartFlashing End Sub

Finally choose Close and Return to Microsoft Excel from the VBA Editor' File menu, and then save the workbook. That's a lot of work just to get flashing text, but it does the job. And of course, you can apply it to any range you like by changing the value of the FR constant.

Archive of Mr Excel Message Board


Back to Excel VBA archive index Back to archive home

If cell H21 >$1000.00, blink text??


Posted by Tony on December 14, 2001 5:39 AM I am tracking the amount of money being spent on cell phone charges for managers in my company. Each person is allowed up to $1000.00 worth of charges each quarter. On my spreadsheet, i would like to have the text, blink in cell H21 if the value is equal to or greater than $1000.00. Is this possible? Thanks.

Check out our Excel VBA Resources

Answer-Re: If cell H21 >$1000.00, blink text??


Posted by Dan on December 14, 2001 6:41 AM I modified some code that I found on jwalk.com written by Bill Manville (give credit where credit is due) and that I think this will suit your needs.

First, copy the following and paste in a Module on your workbook: Dim NextTime As Date Sub Flash() NextTime = Now + TimeValue("00:00:01") With Cells(21, 8).Font If .ColorIndex = 2 Then .ColorIndex = 3 Else .ColorIndex = 2 End With Application.OnTime NextTime, "Flash" End Sub Sub StopIt() Application.OnTime NextTime, "Flash" Application.OnTime NextTime, "Flash", schedule:=False Cells(21, 8).Font.ColorIndex = xlAutomatic End Sub Next: Copy and paste this code in to the "This Workbook" project sheet:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range) Calculate If Cells(21, 8).Value > 1000 Then Flash Else StopIt End If End Sub This should make your cell flash red if over 1000, else black. Let me know if you need more help. HTH.

Dan, One more question: If cell H21 >$1000.00, blink text??


Posted by Tony on December 14, 2001 7:42 AM Dan, thanks it worked great!!! Now, how do i edit the code to also flash if Cell c22 exceeds another number such as 6? I tried changing the 1000 to a 6 but, it didn't work.

I forgot to tell you: If cell H21 >$1000.00, blink text??

Posted by Tony on December 14, 2001 7:52 AM Dan, (1)i am using a spinner to show the values in H21 and H22. When the spinner number reaches 1000, nothing happens. But, if i use my KB to input 1000 in H21, the flashing works. Is there a way to use the spinner numbers?

Re: I forgot to tell you: If cell H21 >$1000.00, blink text??


Posted by Dan on December 14, 2001 9:20 AM Hmm.. Doesn't seem to want to work using the spin button. I'm taking off here shortly, let me think about it this weekend and get back to you. You can email me (darag2358@yahoo.com) with your e-mail address and I'll get back in touch. Dan,

Re: I forgot to tell you: If cell H21 >$1000.00, blink text??


Posted by Thersites on December 14, 2001 3:50 PM Instead of the Workbook_SheetChange procedure, use SpinButton1_Change (in the sheet module).

Final Answer?
Posted by Dan on December 14, 2001 8:35 PM Unfortunately it's not that easy. It errors out when trying to constantly change the font of a cell that is linked to a spin button. Here's my way around it. I'm dealing only with cell H21 here and made the code a little more readable. If you need help in adding the other cell, let me know. First, **remove the cell link from your spin button**. I'm assuming you know how to do that since you put it there in the first place. :) Then paste the following code in the "This Workbook" code sheet: Private Sub Workbook_BeforeClose(Cancel As Boolean) StopItH21 End Sub Private Sub Workbook_Open() If Range("H21") > 1000 Then FlashH21 Else StopItH21 End Sub

Then paste the following in the worksheet code that your spin button is on (I am assuming that the spinner for H21 is called SpinButton1: Private Sub SpinButton1_SpinDown() Range("H21") = Range("H21") - 1 'Decrements value of cell by 1 If Range("H21") > 1000 Then FlashH21 Else StopItH21 End Sub Private Sub SpinButton1_SpinUp() Range("H21") = Range("H21") + 1 'Increments value of cell by 1 If Range("H21") > 1000 Then FlashH21 Else StopItH21 End Sub Finally, insert a module and place this code there: Dim NextTime As Date Sub FlashH21() NextTime = Now + TimeValue("00:00:02") With Range("H21").Font If .ColorIndex = 2 Then .ColorIndex = 3 Else .ColorIndex = 2 End With Application.OnTime NextTime, "FlashH21" End Sub Sub StopItH21() Application.OnTime NextTime, "FlashH21" Application.OnTime NextTime, "FlashH21", schedule:=False Range("H21").Font.ColorIndex = xlAutomatic End Sub

I tested it and it seems to work. The flashing seems a little quirky (of course I didn't write that part :)) but I can't think of a better way to do that. Let me know if there are any other problems, you have my email address...

A further final answer ! .............

Posted by Thersites on December 14, 2001 10:59 PM Unfortunately it's not that easy. It errors out when trying to constantly change the font of a cell that is linked to a spin button. Here's my way around it. I'm dealing only with cell H21 here and made the code a little more readable. If you need help in adding the other cell, let me know. First, **remove the cell link from your spin button**. I'm assuming you know how to do that since you put it there in the first place. :) Then paste the following code in the "This Workbook" code sheet: Private Sub Workbook_BeforeClose(Cancel As Boolean) , schedule:=False

You said "It errors out when trying to constantly change the font of a cell that is linked to a spin button". I couldn't produce any error. The following works for me :Dim NextTime As Date Sub Flash() NextTime = Now + TimeValue("00:00:01") With Cells(21, 8).Font If .ColorIndex = 2 Then .ColorIndex = 3 Else .ColorIndex = 2 End With Application.OnTime NextTime, "Flash" End Sub Sub StopIt() Application.OnTime NextTime, "Flash" Application.OnTime NextTime, "Flash", schedule:=False Cells(21, 8).Font.ColorIndex = xlAutomatic End Sub Private Sub SpinButton1_Change() StopIt If Cells(21, 8).Value > 6 Then Flash End Sub Private Sub Workbook_Open() If Cells(21, 8).Value > 6 Then Flash End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) StopIt End Sub

Thanks for the help guy's!!


Posted by Tony on December 17, 2001 6:17 AM

This archive is from the original message board at www.MrExcel.com. All contents 1998-2004 MrExcel.com. Visit our online store to buy searchable CD's with thousands of VBA and Excel answers. Microsoft Excel is a registered trademark of the Microsoft Corporation. MrExcel is a registered trademark of Tickling Keys, Inc.

Das könnte Ihnen auch gefallen