如何切一个子串?

时间:2019-06-26 06:41:27

标签: excel vba

我有很多可以包含斜体的字符串。我想用此字体复制此字符串。在每个新字符串中,我都有一个粗体字

示例:enter image description here

大字符串:enter image description here

我尝试过:

Public Function GetDefinition(ByVal rngText As Range) As String
Dim theCell As Range
Set theCell = rngText.Cells(1, 1)

For I = 1 To Len(theCell.Value)
    If theCell.Characters(I, 1).Font.Bold = False Then
        If theCell.Characters(I + 1, 1).Text = " " Then
            theChar = theCell.Characters(I, 1).Text
            Else
            theChar = theCell.Characters(I, 1).Text
        End If
        Results = Results & theChar
    End If
Next I
GetDefinition = Results
End Function

2 个答案:

答案 0 :(得分:1)

我认为您可以使用此

Option Explicit

Sub test()

    Dim LastRow As Long, i As Long, j As Long, PositionOfDot As Long

    With ThisWorkbook.Worksheets("Sheet1")

        'Find last row of column A
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        'Loop from row 1 to lastrow
        For i = 1 To LastRow
            'Copy paste from column A to C keeping formatting
            .Range("A" & i).Copy .Range("C" & i)
            'Find the position of "."
            PositionOfDot = InStr(1, .Range("A" & i), ".")
            'Delete characters starting from the first one up to PositionOfDot+1
            .Range("C" & i).Characters(1, PositionOfDot + 1).Delete

        Next i

    End With

End Sub

结果:

enter image description here

答案 1 :(得分:0)

如果您的粗体字符串始终以点结尾,则将为您完成

Option Explicit
Public Function GetDefinition(ByVal rngText As Range) As String

    Dim SplitBold As Variant

    SplitBold = Split(rngText, ". ")
    GetDefinition = Trim(SplitBold(1))

End Function