PC Review


Reply
Thread Tools Rate Thread

Automation: Adding Text to Cell With Different Colours

 
 
Manfred Senn
Guest
Posts: n/a
 
      24th Nov 2006
I'm using VbScript to automate Excel.
The only way I know to programmatically add text with different colours to a
cell is using the 'Characters' function on an existing text:

oExcel.Cells(1, 1) = "This is a Text"
oExcel.Cells(1, 1).Characters(6, 2).Font.ColorIndex = 5
This colors "is" in blue

What I want is a function or a way that colors directly when putting the
text in the cell:
oExcel.Cells(1, 1) = "This "
... then the function that makes text blue
oExcel.Cells(1, 1) = oExcel.Cells(1, 1) & "is "
.... then the function that makes text black
oExcel.Cells(1, 1) = oExcel.Cells(1, 1) & "a Text"

Does anybody know such a function or way?

Manfred


 
Reply With Quote
 
 
 
 
=?Utf-8?B?SkxHV2hpeg==?=
Guest
Posts: n/a
 
      24th Nov 2006
Does this help:

Sub diffClrs()
MyString = "This is a text"
With Worksheets(1).Cells(1, 1)
.Value = MyString
.Characters(1, 5).Font.ColorIndex = 5
.Characters(6, 3).Font.ColorIndex = 3
.Characters(9, 6).Font.ColorIndex = 1
End With
End Sub

"Manfred Senn" wrote:

> I'm using VbScript to automate Excel.
> The only way I know to programmatically add text with different colours to a
> cell is using the 'Characters' function on an existing text:
>
> oExcel.Cells(1, 1) = "This is a Text"
> oExcel.Cells(1, 1).Characters(6, 2).Font.ColorIndex = 5
> This colors "is" in blue
>
> What I want is a function or a way that colors directly when putting the
> text in the cell:
> oExcel.Cells(1, 1) = "This "
> ... then the function that makes text blue
> oExcel.Cells(1, 1) = oExcel.Cells(1, 1) & "is "
> .... then the function that makes text black
> oExcel.Cells(1, 1) = oExcel.Cells(1, 1) & "a Text"
>
> Does anybody know such a function or way?
>
> Manfred
>
>
>

 
Reply With Quote
 
=?Utf-8?B?SkxHV2hpeg==?=
Guest
Posts: n/a
 
      24th Nov 2006
This will apply the colors aftrer text is typed into the cell, but will not
format the cell so that text is colored as it is typed.

Sub FmtDiffClr()
With Worksheets(1).Cells(1, 1)
.Characters(1, 5).Font.ColorIndex = 5
.Characters(6, 3).Font.ColorIndex = 3
.Characters(9, 6).Font.ColorIndex = 1
End With
End Sub

It will not allow you to format the cell using the Characters.Font property
since Characters applies to a string object.

"Manfred Senn" wrote:

> I'm using VbScript to automate Excel.
> The only way I know to programmatically add text with different colours to a
> cell is using the 'Characters' function on an existing text:
>
> oExcel.Cells(1, 1) = "This is a Text"
> oExcel.Cells(1, 1).Characters(6, 2).Font.ColorIndex = 5
> This colors "is" in blue
>
> What I want is a function or a way that colors directly when putting the
> text in the cell:
> oExcel.Cells(1, 1) = "This "
> ... then the function that makes text blue
> oExcel.Cells(1, 1) = oExcel.Cells(1, 1) & "is "
> .... then the function that makes text black
> oExcel.Cells(1, 1) = oExcel.Cells(1, 1) & "a Text"
>
> Does anybody know such a function or way?
>
> Manfred
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple text colours in a single cell Ron Microsoft Excel Misc 1 18th Dec 2008 12:57 AM
Don't change cell colours when adding or deleting rows? Martin ©¿©¬ @nohere.net Microsoft Excel Misc 1 16th Apr 2007 03:32 PM
different text colours in the same cell in excel =?Utf-8?B?TmlwdW4=?= Microsoft Excel Worksheet Functions 8 28th Jan 2007 05:21 PM
adding cell by colours =?Utf-8?B?d2hhdHp6dXA=?= Microsoft Frontpage 1 17th Aug 2005 12:55 PM
Excel 2003 font colours and cell colours =?Utf-8?B?YnJldHRh?= Microsoft Excel Misc 1 17th Apr 2005 03:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:42 PM.