Consulting

Results 1 to 10 of 10

Thread: Solved: Bold part of text inserted into cell?

  1. #1
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location

    Solved: Bold part of text inserted into cell?

    I need to insert text into a cell, via macro, and bold part of the text. How would I do that?

    Example: "Regular text and bolded text"

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    With Range("A1")
    .Value = "Regular text and bolded text"
    .Characters(19, 11).Font.Bold = True
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    But what if some of the text is always the same and some (the part I want bolded) is always different? How do I bold it then?

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Cheryl

    How would you, we or VBA determine what's to be bold and what's not?

  5. #5
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Originally, the macro was in a Word template. In the Word table cell, the macro inserted the static part of the variable text, toggled bold on, then inserted the rest of the varible text. I've never really worked with Excel macros before, so I have no idea how to do the same thing in an Excel macro.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    With Range("A1")
    .Value = "Regular text and bolded text"
    .Characters(len_of_always_the_same_bit + 1, Len(.Value) - len_of_always_the_same_bit).Font.Bold = True
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Ok, Here's the code you gave me for adding text to column B of the active row. How do I modify it to apply bold to "bolded text"? Since this doesn't use a range, I'm not sure how to alter it.

    [VBA]ActiveSheet.Cells(ActiveCell.Row, "B").Value = "Regular text and bolded text"
    [/VBA]

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It does use a range, Cells is a range property, so it works the ame way

    [vba]

    Dim mpStart As Long

    With ActiveSheet.Cells(ActiveCell.Row, "B")

    .Value = "Regular text and bolded text"
    mpStart = InStr(.Value, "bolded text")
    If mpStart > 0 Then

    .Characters(mpStart, Len(.Value) - mpStart).Font.Bold = True
    End If
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    The only thing the above code didn't do was bold the last letter in "bolded text".

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It is pretty simple to work out the fix

    [vba]

    With ActiveSheet.Cells(ActiveCell.Row, "B")

    .Value = "Regular text and bolded text"
    mpStart = InStr(.Value, "bolded text")
    If mpStart > 0 Then

    .Characters(mpStart, Len(.Value) - mpStart + 1).Font.Bold = True
    End If
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •