shapes - handling text with VBA

dineshbabu2000
07-07-2010, 01:24 AM
I have 3 values in different cells. I need a excel VBA code which could put these 3 values into a single shape (eg. oval), in 3 rows and 3 different colors (both are important criterion to be met). Its like comparing some physical quantity for 3 different cases. Can somebody help me with options??!!

Casey_2
07-08-2010, 01:25 AM
You could have done it with the macro recorder.

Here is the code:

Option Explicit
Public Type TT
TXT As String
LEN As Long
End Type

Sub Macro1()
Dim TheText(1 To 3) As TT
Dim j1 As Long, j2 As Long
'Store the text
TheText(1).TXT = SH1.Range("Text1"): TheText(1).LEN = Len(TheText(1).TXT)
TheText(2).TXT = SH1.Range("Text2"): TheText(2).LEN = Len(TheText(2).TXT)
TheText(3).TXT = SH1.Range("Text3"): TheText(3).LEN = Len(TheText(3).TXT)
With SH1.Shapes("Oval 1").TextFrame
'Write the text in the shape
.Characters.Text = TheText(1).TXT & Chr(10) & TheText(2).TXT & Chr(10) & TheText(3).TXT
'Put the colors
j1 = 1: j2 = TheText(1).LEN + 1: .Characters(Start:=j1, Length:=j2).Font.ColorIndex = 3
j1 = j1 + TheText(1).LEN: j2 = TheText(2).LEN + 2: .Characters(Start:=j1, Length:=j2).Font.ColorIndex = 4
j1 = j1 + TheText(2).LEN: j2 = TheText(3).LEN + 3: .Characters(Start:=j1, Length:=j2).Font.ColorIndex = 37
End With
End Sub

And you have a sample file in attachment.

Please further modify yourself the code to meet your specific needs.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum