使用特殊字符在Excel VBA用户表单中编辑字符串

时间:2014-08-11 00:21:31

标签: excel vba

我试图在Excel中为userform编写一个程序来编辑图表标题和其他内容。我想编写一个使用特殊字符的代码,例如{like this},并将可爱括号内的文本更改为下标,我希望能够多次执行此操作: 以下代码仅适用于第一次出现。

Public Font_Name As String, Font_Style As String, Half_Height As Integer

Sub CommandButton1_Click()
'********************Define Standardized Plot Settings******************
    Font_Name = "Arial"
    Font_Style = "Normal"
    Title_Font_Size = 28
    Axes_Label_Font_Size = 22
    Tick_Lable_Font_Size = 20
    PlotArea_Border_Color_R = 0
    PlotArea_Border_Color_G = 0
    PlotArea_Border_Color_B = 0
    PlotArea_Border_Weight = 3
    PlotArea_Border_Weight_Pass = PlotArea_Border_Weight
    Grid_Color_R = 150
    Grid_Color_G = 150
    Grid_Color_B = 150
    Grid_Weight = 2
    Grid_Weight_Pass = Grid_Weight
'*****************End Define Standardized Plot Settings*****************

'****************************Format the plot********************************
'----------------------------Format the Title-------------------------------

'*****Searches Char Title for {} and replaces everything indside as subscript***

With ActiveChart
        .HasTitle = True
        .ChartTitle.Text = Me.Chart_Title.Text
        .ChartTitle.Characters.Font.Name = Font_Name
        .ChartTitle.Characters.Font.FontStyle = Font_Style
        .ChartTitle.Characters.Font.Size = Title_Font_Size  'works
          If Me.FontOveride <> "" Then
        .ChartTitle.Characters.Font.Size = Me.FontOveride
         Else
        .ChartTitle.Characters.Font.Size = Title_Font_Size  'works
         End If

searchString = Me.Chart_Title.Text
Char1 = "{"
Char2 = "}"

For i = 1 To Len(searchString)
    If Mid(searchString, i, 1) = Char1 Then
    startPos = i
    Exit For
    Else:
    End If
    Next i

For j = 1 To Len(searchString)
    If Mid(searchString, j, 1) = Char2 Then
    endPos = j
    Exit For
    Else:
    End If
    Next j

If startPos >= 1 Or endPos >= 1 Then
    .ChartTitle.Characters(startPos, endPos - startPos).Font.Subscript = True
    .ChartTitle.Characters(startPos, 1).Delete
    .ChartTitle.Characters(endPos - 1, 1).Delete
    Else:
    End If

End With


'***************************************************************************


'***************************************************************************
'----------------------------Format the X Axis-------------------------------
With ActiveChart.Axes(xlCategory)
        .HasTitle = True
        .AxisTitle.Characters.Text = Me.X_Axis_Title
        .AxisTitle.Characters.Font.Name = Font_Name
        .AxisTitle.Characters.Font.FontStyle = Font_Style
        .AxisTitle.Characters.Font.Size = Axes_Label_Font_Size
        .TickLabels.Font.Name = Font_Name
        .TickLabels.Font.FontStyle = Font_Style
        .TickLabels.Font.Size = Tick_Lable_Font_Size
        .MajorTickMark = xlTickMarkNone
        .MinimumScale = Me.X_Axis_Start
        .MaximumScale = Me.X_Axis_Stop
        .MajorUnit = Me.X_Axis_Step
        .CrossesAt = Me.X_Axis_Start
        .HasMajorGridlines = True
        .MajorGridlines.Border.Color = RGB(Grid_Color_R, Grid_Color_G, Grid_Color_B)
        .MajorGridlines.Border.Weight = Grid_Weight_Pass
        .Border.Color = vbBlack

'*****Searches X-Axis for {} and replaces everything indside as subscript*******
searchString = Me.X_Axis_Title
Char1 = "{"
Char2 = "}"
For i = 1 To Len(searchString)
    If Mid(searchString, i, 1) = Char1 Then
        Pos1 = i
    Exit For
    Else:
   'End If
   End If
Next i

For j = 1 To Len(searchString)
    If Mid(searchString, j, 1) = Char2 Then
    Pos2 = j
    Exit For
    Else:
     'End If
    End If
Next j

If Pos1 >= 1 And Pos2 >= 1 Then
.AxisTitle.Characters(Pos1, Pos2 - Pos1).Font.Subscript = True
.AxisTitle.Characters(Pos1, 1).Delete
.AxisTitle.Characters(Pos2 - 1, 1).Delete
Else:
End If

End With
'----------------------------Format the Y Axis-------------------------------
With ActiveChart.Axes(xlValue)
        .HasTitle = True
        .AxisTitle.Characters.Text = Me.Y_Axis_Title
        .AxisTitle.Characters.Font.Name = Font_Name
        .AxisTitle.Characters.Font.FontStyle = Font_Style
        .AxisTitle.Characters.Font.Size = Axes_Label_Font_Size
        .TickLabels.Font.Name = Font_Name
        .TickLabels.Font.FontStyle = Font_Style
        .TickLabels.Font.Size = Tick_Lable_Font_Size

On Error GoTo Skip
Decimal_Position = Len(Me.Y_Axis_Step.Text) - WorksheetFunction.Search(".", Me.Y_Axis_Step.Text)
Format_String = "#,##0." & WorksheetFunction.Rept("0", Decimal_Position)
        .TickLabels.NumberFormat = Format_String
GoTo Skip2
Skip:
On Error GoTo 0
        .TickLabels.NumberFormat = "#,##0"
Skip2:

        .MajorTickMark = xlTickMarkNone
        .MinimumScale = Me.Y_Axis_Start
        .MaximumScale = Me.Y_Axis_Stop
        .MajorUnit = Me.Y_Axis_Step
        .CrossesAt = Me.Y_Axis_Start
        .HasMajorGridlines = True
        .MajorGridlines.Border.Color = RGB(Grid_Color_R, Grid_Color_G, Grid_Color_B)
        .MajorGridlines.Border.Weight = Grid_Weight_Pass
        .Border.Color = vbBlack


'*****Searches Y Axis for {} and replaces everything indside as subscript*******
searchString = Me.Y_Axis_Title
Char1 = "{"
Char2 = "}"
For i = 1 To Len(searchString)
    If Mid(searchString, i, 1) = Char1 Then
        Pos3 = i
    Exit For
    Else:
   'End If
   End If
Next i

For j = 1 To Len(searchString)
    If Mid(searchString, j, 1) = Char2 Then
    Pos4 = j
    Exit For
    Else:
     'End If
    End If
Next j

If Pos3 >= 1 And Pos4 >= 1 Then
.AxisTitle.Characters(Pos3, Pos4 - Pos3).Font.Subscript = True
.AxisTitle.Characters(Pos3, 1).Delete
.AxisTitle.Characters(Pos4 - 1, 1).Delete
Else:
End If
End With
'****************************End Format the Plot*******************************

1 个答案:

答案 0 :(得分:1)

您可以使用模式{[\w]*}的正则表达式。

如果要使用早期绑定,则需要引用Microsoft VBScript Regular Expressions 5.5。

除了其他信息外,RegEx还会为您提供开始位置&amp;每个子字符串的长度,然后您可以根据需要使用它来应用下标或其他格式。

Sub regTest()
Dim R As Object 'New RegExp
Dim matches As Object 'MatchCollection
Dim m As Variant
Dim str As String

Set R = CreateObject("VBScript.RegExp")

str = "hello {world} this is my {title}"

R.Pattern = "{[\w]*}"
R.Global = True
R.IgnoreCase = True

If R.test(str) Then
    Set matches = R.Execute(str)
    For Each m In matches
        Debug.Print m.Value
        Debug.Print "Starts at: " & m.FirstIndex
        Debug.Print "Lenght: " & m.Length
    Next
End If

End Sub