首页 > 解决方案 > 如何切割子串?

问题描述

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

例子:在此处输入图像描述

大字符串:在此处输入图像描述

我试过:

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

标签: excelvba

解决方案


我认为你可以使用这个:

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

结果:

在此处输入图像描述


推荐阅读