Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.newusers
Alex
 
Posts: n/a
Default Button or key to conditionally change text foreground color?

Kurt

Glad to hear that the code worked ok.

If you want to have multiple colours then the simplest way is to have
multiple buttons and just use the same macro but change the colorindex. You
can rename the VBA procedures accordingly e.g.

RedButton
BlueButton
GreenButton

etc.

There are other ways e.g. press button, get a dialog box which you pick
colour from, and then execute. But too complicated when you can just
reporduce the buttons.

Regards


Alex
"Kurt Swanson" wrote:

Kurt Swanson writes:
"Alex" writes:
Add this code into the VBE (press ALT + F11) then <Insert<Module and cut
and paste the following code: into a module (most likely module 1):


Sub RedButton()
Dim rng As Range
Dim cl As Range


Set rng = Range("A1:I9")


For Each cl In rng
If IsEmpty(cl) Then
cl.Font.ColorIndex = 3
Else
cl.Font.ColorIndex = 0
End If
Next cl


End Sub


Now go back to worksheet and select <View<Toolbars<Forms and then click
the 'Button' icon and drag onto yor worksheet and 'assign' the macro
'RedButton'. Now right-click the button and <Edit Text and give the button a
name.


Each time you now press the button the empty cells in range A1:I9 will have
a red foreground.


Alex, thanks--this is (almost) exactly what I want. I don't want to
forcibly change existing values to black, so I took out the two
"Else...ColorIndex = 0" lines.


However I was not able to make it work. I could define the
subroutine, but I was not able to create a button--all the buttons on
the Forms toolbar are greyed out. Secondly, if I simply manually run
the macro with alt-F8, I get an error: "unable to set the colorindex
property of the font class". I did some googling on this and found
there is some sort of bug in Excel XP about this. There seems to be a
work-around but I was not able to find it.


Ok, I was a little hasty. It seems I had very high security for macros
turned on, and certain cells were protected. I was able to unprotect,
add the button(s), then re-protect the specific cells. And thus all
is well. Strange thought, that it wouldn't let me add the buttons
when only unrelated cells were protected...

BTW, I actually need multiple buttons with different colors. Can I
make a macro with a parameter specified in the button, or just make
one macro per button that simply calls a sub with a specific
parameter? (The latter of which I've already done successfully...)
--
© 2005 Kurt Swanson AB

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
in excel, how do i change the fill color? [email protected] Excel Worksheet Functions 1 November 29th 05 06:33 PM
Macro to change cell text color James C Excel Discussion (Misc queries) 1 August 12th 05 06:59 PM
how do I change cell background color only when cursor is over it Victor Cassano Excel Discussion (Misc queries) 1 August 10th 05 09:42 PM
Change Case button? Hiking Excel Discussion (Misc queries) 1 February 4th 05 09:23 PM
Change cell back color on click Dave Peterson Excel Discussion (Misc queries) 0 January 24th 05 10:50 PM


All times are GMT +1. The time now is 02:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"