Skip to main content
  1. About
  2. For Teams
Asked
Viewed 370 times
0

I am a complete VBA beginner so please dumb your answer down as much as possible.

I need to bold str2. I have worked out how to do it based on a set number of characters however, I have an issue because str02 is dynamic. It is a customers name and therefore, the length changes meaning I can't count characters because I don't know how many there will be.

i.e

Dear Mr Customer (length changes),

bold Your request has been approved bold

Thank you

Public Sub ExampleConcatenate()

    Dim str1 As String
    Dim str01 As String
    Dim str02 As String
    Dim str03 As String
    Dim str2 As String
    Dim str3 As String

    str1 = Range("A14").Value
    str01 = " "
    str02 = Worksheets("Restructure Calculator").Range("D5").Value
    str03 = vbNewLine
    str2 = Range("A15").Value
    str3 = Range("A16").Value

    Range("A1").Value = str1 & str01 & str02 & str03 & str03 & str2 & str03 & str3

End Sub
9
  • VBA strings have no notion of bold vs. ordinary characters. You have to look into the Text in the cell you want to display it in. As a hint, make part of the text in a cell bold while the macro recorder is turned on then look at the recorded code. Also, you can use len() to determine which characters in the final string that you want to highlight.
    John Coleman
    –  John Coleman
    2020-04-13 23:23:27 +00:00
    Commented Apr 13, 2020 at 23:23
  • Does this answer your question? excel vba: make part of string bold
    John Coleman
    –  John Coleman
    2020-04-13 23:24:43 +00:00
    Commented Apr 13, 2020 at 23:24
  • Hi John - thank you but I have already tried this. My original set up was a concatenate formula (not done with VBA) and then a macro to copy, paste special by values (to remove the formula) and then bold the relevant parts. The problem is, it always copies and pastes my original customer at the time of recording and does not pick up the new customer information. Even with the VBA above and a separate macro to bold the text, it still always copies and pastes my original customer information. I have a lot of dynamic information that I need it consider.
    Holly
    –  Holly
    2020-04-13 23:31:30 +00:00
    Commented Apr 13, 2020 at 23:31
  • Your comment seems to be shifting the question. There are two questions: 1) how to assemble the string you want to assemble and 2) how to bold part of the resulting string. You seemed to be asking about the second question but seem to be shifting to the first. Unfortunately, for that you haven't really provided any necessary information. If you want a macro to dynamically change the sheet in response to changes in the sheet, you could use events such as Worksheet_Change
    John Coleman
    –  John Coleman
    2020-04-13 23:34:39 +00:00
    Commented Apr 13, 2020 at 23:34
  • Yes. I need to bold str2. The problem is str02 is not a static number of characters. So how do I bold str2?
    Holly
    –  Holly
    2020-04-13 23:40:49 +00:00
    Commented Apr 13, 2020 at 23:40

1 Answer 1

1

This is what your code might look like. Please try it.

Sub ExampleConcatenate()

    Dim Output As String
    Dim Project As String
    Dim ProjectID As String
    Dim Decision As String
    Dim BodyText As String
    Dim n As Integer

    Project = Range("A14").Value
    ProjectID = Worksheets("Restructure Calculator").Range("D5").Value
    Decision = Range("A15").Value
    BodyText = Range("A16").Value

    Output = Project & " " & ProjectID & String(2, Chr(10))
    n = Len(Output)
    Output = Output & Decision & Chr(10) & BodyText

    With Cells(1, "A")
        .Value = Output
        With .Characters(Start:=n + 1, Length:=Len(Decision)).Font
            .Name = "Calibri"
            .FontStyle = "Bold"
            .Size = 12
            .Underline = True ' xlUnderlineStyleNone
        End With
    End With
    Rows(1).AutoFit
End Sub

And then fix it.

  1. Your system of numbering strings will lead you to the need for a phonebook to keep track of your numbers. Give meaningful names to your variables and don't give generic names to existing variables that already have meaningful names, such as Str03 = vbNewLine, which, incidentally, doesn't make much logical sense if one considers that Str03 is declared as string whereas vbNewLine is an enumeration of Long data type.
  2. I gave names to the variables that enabled me making some sense. I'm sure it won't be the sense you intended. You can use Edit > Replace to rename variables in the code.
  3. vbNewLine doesn't show up as a CR in an Excel cell (at least, in connection with the String() function it didn't for me). I replaced it with Chr(10).
  4. With the exception of a reference to Worksheet("Restructure Calculator") all your sheet references are on the ActiveSheet, meaning no worksheet is declared for them. It's only a matter of time until your code accidentally writes to Worksheet("Restructure Calculator"), destroying data there. Better to create a variable for the worksheet you want to write to before that happens and make sure all the ranges and cells in your code are identified with the worksheet they refer to.
  5. My code shows you how to identify the location of the text you wish to highlight. It also shows how to set the font for that section of text. You may wish to alter the settings. It's easy with the setup I provide.

Last but not least, you may like to bear in mind that Excel starts hitting some limits when you have more than 255 characters in a single cell. To avoid possible problems in the future consider spreading your text over several rows if its total length exceeds 255 characters.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.