Using a macro to edit cells?

  • Thread starter Thread starter bdunk
  • Start date Start date
B

bdunk

Is there a way to edit cells similiar to conditional formatting but us
a macro button to perform the desired action? This macro would d
something based on cell contents. I.e. bold all text that starts wit
a certain set of characters
 
You could even use Format|Conditional Formatting:

You could use a formula like:
=LEFT(A1,2)="SP"
(to bold A1 if it starts with SP.)

You could select your range and run a macro like this:

Option Explicit
Sub testme()

Dim myCell As Range
Dim myRng 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 some cells with text"
Exit Sub
End If

'clear existing bolding???
myRng.Font.Bold = False

For Each myCell In myRng.Cells
If LCase(Left(myCell.Value, 2)) = "sp" Then
myCell.Font.Bold = True
End If
Next myCell

End Sub

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 
Back
Top