macro-add text

P

puiuluipui

Hi, i need to add some text to some cells, but i need a macro linked to a
button to do this. If i select a cell and than i click the macro button, then
"(anul)" to appear in that cell, at the end of the text.

ex:-without button
a
John C
John C (ed)


Ex:-with button
a
John C (anul)
John C (ed) (anul)


Can this be done?
Thanks!
 
J

Jacob Skaria

try selecting a cell and running the below macro

Sub Macro AddText()
Cells(activecell.Row,activecell.Column)= Activecell.Text & " (anul)"
End Sub

If this post helps click Yes
 
D

Dave Peterson

Option Explicit
Sub testme()

Dim myRng As Range
Dim myCell As Range

Set myRng = Nothing
On Error Resume Next
Set myRng = Intersect(Selection, _
Selection.Cells.SpecialCells(xlCellTypeConstants, xlTextValues))
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "Please select a range with text values"
Exit Sub
End If

For Each myCell In myRng.Cells
With myCell
.Value = .Value & " (anul)"
End With
Next myCell

End Sub
 
P

puiuluipui

It's working! But i forgot to say that i need "(anul)" to be red. Can the
text that i need to appear "(anul)", be red?
Thanks allot!

"Dave Peterson" a scris:
 
J

Jacob Skaria

Please try this

Sub AddText()
Dim intTemp
Dim strAddtext As String
strAddtext = " (anul)"
For Each c In Selection
Cells(c.Row, c.Column) = Cells(c.Row, c.Column).Text & strAddtext
Cells(c.Row, c.Column).Characters(Start:=Len(Cells(c.Row, c.Column)) - _
Len(strAddtext) + 1, Length:=Len(strAddtext)).Font.Color = vbRed
Next
End Sub

If this post helps click Yes
 
D

Dave Peterson

I would still limit my changes to cells in the selection that are not formulas
and non-numeric:

Option Explicit
Sub testme()

Dim myRng As Range
Dim myCell As Range

Set myRng = Nothing
On Error Resume Next
Set myRng = Intersect(Selection, _
Selection.Cells.SpecialCells(xlCellTypeConstants, xlTextValues))
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "Please select a range with text values"
Exit Sub
End If

For Each myCell In myRng.Cells
With myCell
.Value = .Value & " (anul)"
.Characters(Start:=Len(.Value) - 5, Length:=6).Font.ColorIndex = 3
End With
Next myCell

End Sub
 
P

puiuluipui

Hi, it's working great!
Thanks allot!


Dave Peterson said:
I would still limit my changes to cells in the selection that are not formulas
and non-numeric:

Option Explicit
Sub testme()

Dim myRng As Range
Dim myCell As Range

Set myRng = Nothing
On Error Resume Next
Set myRng = Intersect(Selection, _
Selection.Cells.SpecialCells(xlCellTypeConstants, xlTextValues))
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "Please select a range with text values"
Exit Sub
End If

For Each myCell In myRng.Cells
With myCell
.Value = .Value & " (anul)"
.Characters(Start:=Len(.Value) - 5, Length:=6).Font.ColorIndex = 3
End With
Next myCell

End Sub
 
P

puiuluipui

Hi, it's working great!
Thanks allot!


Dave Peterson said:
I would still limit my changes to cells in the selection that are not formulas
and non-numeric:

Option Explicit
Sub testme()

Dim myRng As Range
Dim myCell As Range

Set myRng = Nothing
On Error Resume Next
Set myRng = Intersect(Selection, _
Selection.Cells.SpecialCells(xlCellTypeConstants, xlTextValues))
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "Please select a range with text values"
Exit Sub
End If

For Each myCell In myRng.Cells
With myCell
.Value = .Value & " (anul)"
.Characters(Start:=Len(.Value) - 5, Length:=6).Font.ColorIndex = 3
End With
Next myCell

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

count 6
Macro partial autofill 9
Macro if condition 5
Macro help 4
find a record with a missing value 1
Excell 2000 macro will not open file! 1
macro search 15
Macro help 3

Top