【问题标题】:How do you format text/strings in VBA?如何在 VBA 中格式化文本/字符串?
【发布时间】:2009-09-10 15:26:03
【问题描述】:

在下面的代码中,我采用一些输入参数,文本或单元格,并使用我需要的格式将它们组合成一个字符串。我需要将 Task_Name 以及像“Lead :”这样的文本加粗。我知道您不能以可变粗体显示文本,但我该怎么做呢?我存储值的这个单元格最终用于 Word 邮件合并。

我需要格式化字符串的一部分。在下面的代码中,我需要将 Task_Name、“Lead :”等全部加粗。

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

另外,我知道这不是最干净的代码,所以如果有任何其他改进它的建议,我们非常欢迎。

谢谢!

【问题讨论】:

    标签: vba text formatting excel string-formatting


    【解决方案1】:

    您不能直接在字符串中添加任何内容以使单元格具有粗体字符。

    将字符串写入单元格后,您需要返回并重新处理该单元格。 例如:

    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 
    

    这个 sn-p 只会将单元格的一部分设置为粗体。

    编辑:

    此代码可用于实现上述内容并为您提供所需的内容。 它可以写得更好,但应该让你知道你必须写什么:

    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
    

    【讨论】:

    • 因此,如果在电子表格中,我将一个单元格设置为 =GENERATE_STAFFING_SECTION(.....),我该如何根据内容对其文本进行格式化?
    • +1:部分显示格式 Sk93 非常酷。我一直想知道部分是如何完成的。
    • 不写完整代码,你知道“任务名称”将从 1 开始并滚动到第一个换行符,所以使用“Instr”找到第一个换行符。您现在将拥有要在上述代码中使用的任务名称的起点和终点。接下来,您知道“Lead:”从您已有的换行符开始,它有五个字符长......您是下一个协调员。等等:)
    • 对,但是如何设置 ActiveCell 的值呢? ActiveCell.Text = "something" 和 ActiveCell.Value = "sdd" 不起作用
    • ActiveCell.Characters.Insert
    猜你喜欢
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2021-04-10
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多