views:

1226

answers:

1

In the code below, I take some input parameters, either text or a cell, and combine them to form one string using the formatting I need. I need to make Task_Name bold, as well as text like "Lead :". I know you cannot make text in a variable bold, but how do I go about this? This cell I'm storing the value in is eventually used in a Word mail merge.

I need to format part of a string. In the code below, I need to make Task_Name, "Lead : ", etc. all bold.

Function GENERATE_STAFFING_SECTION(Task_Name, Lead_By, Members, Instructions)
    Dim tmpSection As String

    If Len(Task_Name > 0) And Len(Lead_By) > 0 And Len(Members) > 0 And Len(Instructions) > 0 Then
        tmpSection = vbLf _
                    & Task_Name _
                    & vbLf & "Lead : " & Lead_By _
                    & vbLf & "Ambassadors : " & Members _
                    & vbLf & "Instructions : " & Instructions _
                    & vbLf
    Else
        tmpSection = ""
    End If

    GENERATE_STAFFING_SECTION = tmpSection
End Function

Also, I know it's not the cleanest code, so if there are any other suggestions for improving it, they are most welcome.

Thanks!

+3  A: 

You can't add anything to the string directly to make the cell have bold characters.

Once you've written the string out to the cell, you'll need to go back and reprocess the cell. For example:

With ActiveCell.Characters(Start:=11, Length:=6).Font 
    .Name = "Arial" 
    .FontStyle = "Bold" 
    .Size = 10 
    .Strikethrough = False 
    .Superscript = False 
    .Subscript = False 
    .OutlineFont = False 
    .Shadow = False 
    .Underline = xlUnderlineStyleNone 
    .ColorIndex = xlAutomatic 
End With

This snippet will set only a portion of the cell to bold.

EDIT:

This code could be used to implement the above and give you what you want. It could be written better, but should give you an idea of what you've got to write:

Public Sub FormatOuput()

    Dim i As Integer

    'Format Task_name
    i = InStr(1, ActiveCell.Text, vbLf)
    MakeBold 1, i

    'Format 'Lead'
    MakeBold i + 1, 4

    'Format 'Ambassadors'
    i = InStr(i + 1, ActiveCell.Text, vbLf)
    MakeBold i+1, 11

    'Format 'Instructions'
    i = InStr(i + 1, ActiveCell.Text, vbLf)
    MakeBold i+1, 10

End Sub

Public Sub MakeBold(startPos As Integer, charCount As Integer)
    With ActiveCell.Characters(start:=startPos, length:=charCount).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
End Sub
Sk93
So if in the spreadsheet, I set a cell to =GENERATE_STAFFING_SECTION(.....), how do I go about formatting its text based on the contents?
achinda99
+1: Very cool with the partial display formatting Sk93. I've always wondered how the partials were done.
RBarryYoung
without writing the full code, you know the "task name" will start at 1 and roll on until the first line feed, so use "Instr" to find the first linefeed char. you'll now have your start and end points of taskname to use in the above code.Next, you know that "Lead:" follows on from the linefeed you already have, and it's five chars long... theres you're next co-ords.And so on :)
Sk93
wait.. i'll edit my answer to help make more sense ;)
Sk93
Right, but how do you set the ActiveCell's value? ActiveCell.Text = "something" and ActiveCell.Value = "sdd" don't work
achinda99
ActiveCell.Characters.Insert <STRING>
Sk93
It doesn't seem to like that. I get a "Unable to get the Insert property of the Characters class" error
achinda99
what version of Excel are you using? Open a new excel window and add a single new method, with only the following line: Worksheets("Sheet1").Cells(1, "A").Characters.Insert "test". when running that method, does it work or throw an error?. If it works, try using: ActiveCell.Characters.Insert "test" and see if that still works.I've tested it on Excel 2007 only, but believe should work in at least Office XP and 2003...
Sk93