VBA格式化字符串

时间:2015-07-30 14:08:34

标签: vba excel-vba powerpoint-vba excel

截至目前,一切都是红色的。我需要结肠后的所有东西都是黑色而不是斜体。

3 个答案:

答案 0 :(得分:0)

您可以更改此行以使文字变黑:

.Color.RGB = RGB(216, 3, 33)

此行删除斜体:

.Italic = msoFasle

编辑:

如果您有单元格位置,可以稍后对字符串执行此操作:

With Cells(1, 1).Characters(9, 21).Font
     .Color = vbBlack
     .Italic = False
     .Name = "Corbel"
     .Size = 20

This显示如何使用字符

编辑:

要更改文本框的部分文本,可以声明两个包含不同格式的字符串的变量,并将它们附加到文本框中。

它将遵循以下形式:

MyTextBox.Value = MyStringVariable

或在你的情况下:

MyTextBox.Value = MyStringVariable1& MyStringVariable2

答案 1 :(得分:0)

宏录制器发现了以下内容:

Sub test()
    ActiveSheet.Shapes.Range(Array("txtbox1")).Select
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
        "Choisir: wefhweufhwef 344tr saefaefa" 'Entering some text'
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 5).Font 'Selecting some font and changing some settings
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 255)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
End Sub

我没有设法先没有选择文本框(??也许有人可以提供帮助,通常并不难)。您可以使用字符串输入文本,在该字符串中使用

计算冒号的位置
dim MyString as string
dim intColon as integer
Mystring = "cwsvws:wifvwhivw"
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = MyString
intColon = instr(MyString, ":")

并使用此字符计数来插入宏录制器的代码。然后,您可以根据需要格式化不同的文本块。

With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(intColon, 5).Font
             .Fill.ForeColor.RGB = RGB(0, 0, 255)
             .Fill.Transparency = 0
             .Fill.Solid
             .Size = 11
             .Name = "+mn-lt"
End With

希望这有帮助。

答案 2 :(得分:0)

您只需要在冒号后分隔字符并根据需要设置颜色:

编辑:忘了斜体......

Sub test()

    sCallOut = "ACTION: [Insert Callout Here]"
    Set oShp = pptSld.Shapes.AddTextbox(msoTextOrientationHorizontal, 110, 100, 557.28, 94.32)
    oShp.Line.Visible = msoFalse
    oShp.TextFrame.TextRange.Text = sCallOut
    oShp.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter

    With oShp.TextFrame.TextRange.Font
       .Name = "Corbel"
       .Italic = msoTrue
       .Size = 20
       .Color.RGB = RGB(216, 3, 33)
       .Bold = msoTrue
    End With

    With oShp.TextFrame.TextRange
        With .Characters(InStr(.Characters, ":") + 1, .Length).Font
            .Color.RGB = RGB(0, 0, 0)
            .Italic = msoFalse
        End With
    End With

    Set oShp = Nothing

End Sub