Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Text color question?

Hi,VBA pro,

I have a column in a worksheet which hold text string like below;

1,-5,6,-11........
-1,2,-3,............

How can I made the the postive figures in one color and the negativ
figures in another color. I try the macro recorder but it won't work.

Regards,
Michael

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Text color question?

Michael,

The following code colors all minus numbers red.
It assumes...
All numbers are separated by a comma
All cells are formatted as text
You have selected the range containing the data

It looks for a "dash" then looks for the first comma occurring after the dash.
It then colors the text red from the dash to the character just before the comma.
If no comma after a dash is found, it colors everything after the dash red.
Only limited testing has been done.
Please let us know if it works for you.

'---------------------------------------
Sub ShowNegativesInRed()
Dim objCell As Range
Dim strText As String
Dim lngChar As Long
Dim lngNextChar As Long

For Each objCell In Selection
strText = objCell.Text
If Len(strText) Then
For lngChar = 1 To (Len(strText) - 1)
If Asc(Mid$(strText, lngChar, 1)) = 45 Then
lngNextChar = InStr(lngChar + 1, strText, ",", vbTextCompare)
If lngNextChar = 0 Then lngNextChar = Len(strText) + 1
objCell.Characters(lngChar, lngNextChar - lngChar).Font.ColorIndex = 3
End If
Next 'lngChar
End If
Next 'ojbCell
Set objCell = Nothing
End Sub
-----------------------------------------

Regards,
Jim Cone
San Francisco, CA

"Michael168 " wrote in message ...
Hi,VBA pro,
I have a column in a worksheet which hold text string like below;
1,-5,6,-11........
-1,2,-3,............
How can I made the the postive figures in one color and the negative
figures in another color. I try the macro recorder but it won't work.
Regards,
Michael.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Text color question?

Hi! Jim Cone,
Yes,the code works but how to modify it so that it will use column 2 a
range for the objcell. Is it possible to add in a sub or whatsoever s
that the positive values will become blue in color?

Thanks for helping.

Regards,
Michael.

Jim Cone wrote:
*Michael,

The following code colors all minus numbers red.
It assumes...
All numbers are separated by a comma
All cells are formatted as text
You have selected the range containing the data

It looks for a "dash" then looks for the first comma occurring afte
the dash.
It then colors the text red from the dash to the character jus
before the comma.
If no comma after a dash is found, it colors everything after th
dash red.
Only limited testing has been done.
Please let us know if it works for you.

'---------------------------------------
Sub ShowNegativesInRed()
Dim objCell As Range
Dim strText As String
Dim lngChar As Long
Dim lngNextChar As Long

For Each objCell In Selection
strText = objCell.Text
If Len(strText) Then
For lngChar = 1 To (Len(strText) - 1)
If Asc(Mid$(strText, lngChar, 1)) = 45 Then
lngNextChar = InStr(lngChar + 1, strText, ",", vbTextCompare)
If lngNextChar = 0 Then lngNextChar = Len(strText) + 1
objCell.Characters(lngChar, lngNextChar - lngChar).Font.ColorIndex
3
End If
Next 'lngChar
End If
Next 'ojbCell
Set objCell = Nothing
End Sub
-----------------------------------------

Regards,
Jim Cone
San Francisco, CA

"Michael168 " wrote i
message ...
Hi,VBA pro,
I have a column in a worksheet which hold text string like below;
1,-5,6,-11........
-1,2,-3,............
How can I made the the postive figures in one color and th

negative
figures in another color. I try the macro recorder but it won'

work.
Regards,
Michael.


--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Text color question?

Michael,

My code works on whatever cells are selected, so if you want column 2 then select it.
However,there is no reason to revise my code, just use the Dana DeLouis code.

In case Dana is still sleeping...

Replace "10" in the following line with 5.
.ColorIndex = IIf(Match.Value = 0, 10, 3)
(10 is green, 5 is blue)

Also, in the Visual Basic Editor, go to Tools | References and put a check by
"Microsoft VBScript Regular Expressions 5.5" in the list that appears.

Regards,
Jim Cone
San Francisco, CA



"Michael168 " wrote in message ...
Hi! Jim Cone,
Yes,the code works but how to modify it so that it will use column 2 as
range for the objcell. Is it possible to add in a sub or whatsoever so
that the positive values will become blue in color?

Thanks for helping.

Regards,
Michael.

Jim Cone wrote:
*Michael,

The following code colors all minus numbers red.
It assumes...
All numbers are separated by a comma
All cells are formatted as text
You have selected the range containing the data

It looks for a "dash" then looks for the first comma occurring after
the dash.
It then colors the text red from the dash to the character just
before the comma.
If no comma after a dash is found, it colors everything after the
dash red.
Only limited testing has been done.
Please let us know if it works for you.

'---------------------------------------
Sub ShowNegativesInRed()
Dim objCell As Range
Dim strText As String
Dim lngChar As Long
Dim lngNextChar As Long

For Each objCell In Selection
strText = objCell.Text
If Len(strText) Then
For lngChar = 1 To (Len(strText) - 1)
If Asc(Mid$(strText, lngChar, 1)) = 45 Then
lngNextChar = InStr(lngChar + 1, strText, ",", vbTextCompare)
If lngNextChar = 0 Then lngNextChar = Len(strText) + 1
objCell.Characters(lngChar, lngNextChar - lngChar).Font.ColorIndex =
3
End If
Next 'lngChar
End If
Next 'ojbCell
Set objCell = Nothing
End Sub
-----------------------------------------

Regards,
Jim Cone
San Francisco, CA

"Michael168 " wrote in
message ...
Hi,VBA pro,
I have a column in a worksheet which hold text string like below;
1,-5,6,-11........
-1,2,-3,............
How can I made the the postive figures in one color and the

negative
figures in another color. I try the macro recorder but it won't

work.
Regards,
Michael. *



---
Message posted from http://www.ExcelForum.com/

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
How do I fill one cell color with text html/rgb color from another thewris Excel Discussion (Misc queries) 2 January 22nd 09 12:24 AM
Can't format cell color/text color in Office Excel 2003 in fil Tony S Excel Discussion (Misc queries) 1 December 21st 07 01:41 PM
Make text color match cell color with macro? JoeSpareBedroom Excel Discussion (Misc queries) 1 June 26th 07 07:09 PM
Cond. format text color based on another's text color manxman Excel Discussion (Misc queries) 3 August 31st 06 06:27 PM
Can't format cell color/text color in Office Excel 2003 in files . albertaman Excel Discussion (Misc queries) 0 February 16th 06 03:56 AM


All times are GMT +1. The time now is 03:38 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"