Home > Blockchain >  Extract bold Words in text with newline character
Extract bold Words in text with newline character

Time:09-05

Can anyone help me with my Excel Problem? I have a cell with text and newline symbols. I need to extract bol text to another cell in the Row. Example:

pc laptop
Google - home
some words
android
some words

I use code

    Public Function findAllBold(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 = True 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
   findAllBold = Results
End Function

But my result is "Google, -, homeandroid". Last words stuck together. I need them to be separated Please help me!

CodePudding user response:

Here is my two cents to your problem, assuming:

  • Each line in your input is either bold or not;
  • You'd like to find the entire line as single output in the concatenated result.

The function with optional delimiter (", " by default):

Function FindAllBold(rngText As Range, Optional Delimiter As String = ", ") As String

With CreateObject("vbscript.regexp")
    .Global = True: .Pattern = "^. ": .MultiLine = True
    For Each m In .Execute(rngText)
        If rngText.Characters(m.FirstIndex   1, 1).Font.Bold = True Then
            If FindAllBold = "" Then
                FindAllBold = m
            Else
                FindAllBold = FindAllBold & Delimiter & m
            End If
        End If
    Next
End With
    
End Function

Call through, for example: =FindAllBold(A1, CHAR(10))

enter image description here

  • Related