Reputation: 71
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
Upvotes: 0
Views: 344
Reputation: 14373
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.
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.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.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.
Upvotes: 1