PDA

View Full Version : Formatting a Cell



tmferreira
07-03-2007, 11:48 AM
Hi! I?m brazilian. :beerchug:


I will try to explain my doubt.

- I put a value in a cell: Worksheets("Sheet1").Range("A1").Value = "Formatting a Cell."
- I want to make the word "Cell" bold. Then, I made: Worksheets("Sheet1").Range("A1").Characters(14, 4).Font.Blod = True

I want to add " More words!" in this cell. Then, I made:
Worksheets("Sheet1").Range("A1").Value = Worksheets("Sheet1").Range("A1").Value & " More Words!"

The problem: The word "Cell" doesn?t keep bold.

How can I do?

Thank?s.

tmferreira.

ps.: sorry my bad English.

unmarkedhelicopter
07-03-2007, 12:33 PM
Your 'bad english' is far better than my 'cafe spanish' :)

mdmackillop
07-03-2007, 12:34 PM
Hi tmferreira
Welcome to VBAX
How about
Sub BoldText()
txt1 = "Formatting a Cell."
txt2 = " More Words!"
mbold = Array("Cell", txt2)
Cells(1, 1) = txt1 & txt2
For Each m In mbold
strt = InStr(1, Cells(1, 1), m)
Cells(1, 1).Characters(strt, Len(m)).Font.Bold = True
Next
End Sub

unmarkedhelicopter
07-03-2007, 12:42 PM
If you do it manually it makes everything after it BOLD also, if you use code it all goes to normal (non-bold).
But you already know that it starts at character 14 and continues for 4 characters, so just do this after you've added 'more words' :)

Charlize
07-03-2007, 01:53 PM
If you do it manually it makes everything after it BOLD also, if you use code it all goes to normal (non-bold).
But you already know that it starts at character 14 and continues for 4 characters, so just do this after you've added 'more words' :)You'll have to set bold to false starting from character 18.

unmarkedhelicopter
07-03-2007, 02:06 PM
You'll have to set bold to false starting from character 18.No, his problem is that ALL the text looses it's BOLDNESS

Charlize
07-03-2007, 02:42 PM
Ok footinmout , time to take a break :deepsleep until tomorrow.

tmferreira
07-03-2007, 06:38 PM
Hi tmferreira
Welcome to VBAX
How about
Sub BoldText()
txt1 = "Formatting a Cell."
txt2 = " More Words!"
mbold = Array("Cell", txt2)
Cells(1, 1) = txt1 & txt2
For Each m In mbold
strt = InStr(1, Cells(1, 1), m)
Cells(1, 1).Characters(strt, Len(m)).Font.Bold = True
Next
End Sub

Wohhhhh!

I think it?s just like I need. I?m at home now. Tomorrow I?ll test it and post here.

The string in bold will be dinamic. I don?t now what is its location.

I didn?t know the InStr function!

I like too much this forum. I?m Administrador of the biggest forum of Brazil ( forum.imasters.com.br ) and I know to recognize a GOOD forum.

Congratulations!!!!

Thanks!

tmferreira
07-04-2007, 11:29 AM
I got using an adaptation of the function BoldText.

I thank all.

mdmackillop
07-04-2007, 01:06 PM
Hi,
Can you post your adaption to see how you solved this?

tmferreira
07-05-2007, 03:46 AM
Hi,
Can you post your adaption to see how you solved this?
Certainly!


Sub formatar(planilha, linha, coluna, padrao, status)
mbold = Array(padrao)
For Each m In mbold
strt = InStr(1, Worksheets(planilha).Cells(linha, coluna), m)
With Worksheets(planilha).Cells(linha, coluna)
Select Case status
Case "Dentro do M?s", "M?s Seguinte", "Fora do Prazo"
.Characters(strt, Len(m)).Font.Color = RGB(0, 0, 255)
Case "Cancelada"
.Characters(strt, Len(m)).Font.Color = RGB(128, 128, 128)
Case "N?o Conclu?da"
.Characters(strt, Len(m)).Font.Color = RGB(255, 0, 0)
Case "Programada"
.Characters(strt, Len(m)).Font.Color = RGB(0, 0, 0)
' Case "Aberta"
' .Characters(strt, Len(m)).Font.Color = RGB(0, 175, 0)
' Case "Andamento"
' .Characters(strt, Len(m)).Font.Color = RGB(255, 127, 0)
End Select
End With
Next
End Sub
The example below find out the string "71" in the cell "E10" of the Sheet1 and format it with Silver.

Call formatar("Sheet1", 10, 5, "71", "Cancelada")
Could you understand?

mdmackillop
07-05-2007, 01:22 PM
Understood! Thanks for posting.