Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Find & Extract Text

0

Is there VBA code to find all underlined or bold text and extract it to new sheet and rows?

This would be extremely useful for studying. This one is beyond me, I really hope you can help me. 

Answer
Discuss

Answers

0

Brad

In the attached revision to your file, I've added the code below to Module 1. It can be triggered from the form button labelled "Extract only bold and underlined text" on the sheet Table 1.

It runs through the range defined in the first line in bold below. The later sub-loop (also in bold) goes through each character in a cell's text and, if bold or underlined, adds it to an output string called Ext. That string gets tested after a cell is done and is added to column A of a second sheet called Extract.

The code is commented (ahead of the action) to explain broadly what is happening:

Sub Extract()

Dim Ext As String, Cl As Range, n As Long

Set SourceRange = Sheet1.Range("A1:W65") ' change to suit but not too large!

On Error Resume Next

' clear results and add title
With Worksheets("Extract")
    .UsedRange.ClearContents
    .Range("A1").Value = "Extract from " & SourceRange.Address & " created " & Now
    .Range("A1").Font.Bold = True
End With

' reduce processing time
Application.ScreenUpdating = False

'check cells in SourceRange
For Each Cl In SourceRange
    'reset output string
    Ext = ""
    'avoid a problem when cell is just a bold/underlined number
    If IsNumeric(Cl.Value) Then
        If Cl.Font.Underline = 2 Or Cl.Font.Bold = True Then Ext = Cl.Value
        ' skip down
        GoTo ContinueForEachloop
    End If
    ' otherwise loop through characters in string
    For n = 1 To Len(Cl.Text)
        With Cl.Characters(n, 1)
            If .Font.Underline = 2 Then
            ' add to output string
                Ext = Ext + .Text
            Else
                If .Font.Bold = True Then Ext = Ext + .Text
            End If
        End With
    Next n

ContinueForEachloop:

    ' if there's an output, write to next row on Extract sheet
    If Ext <> "" Then
        With Worksheets("Extract")
            ' find last populated row
            LastRw = .Cells(.Rows.Count, 1).End(xlUp).Row
            .Cells(LastRw + 1, 1).Value = Ext
        End With
    End If

Next Cl
' restore
Application.ScreenUpdating = True
' tell user
MsgBox "Extracted only bold and underlined text from " & SourceRange.Address & " to Extract sheet "


End Sub

Note however that this code isn't perfect- SourceRange is set to  A1:W65 (1,495 cells) above since it seems to hang if the range includes cells A70 or A79 (for example) but if you set SourceRange to just one of those cells, the macro runs okay. I'm pretty it's not a string length issue so it might be a memory issue on my old PC. Please let me know if you encounter similar problems. 

Giving you this code isn't to say I think it's a good study aid (though it might be handy as an intro or to recap)- personally I like details and think that knowing a subject in depth is preferable to just knowing the headlines!

Hope this gives you a good start.

Discuss

Discussion

Talking of detail, I just noticed that your Forum profile says you use "MS Office Version: Sroncey21". Please correct that (since some potential solutions might need Excel 365)
John_Ru (rep: 6152) Jan 27, '22 at 12:14 pm
Hi Brad. Did I do something wrong or give you a problem? I was alerted to say you had selected my Answer then that you'd modified your question (seems you deleted your file*). Now I see the Answer is deselected - what's the problem please?

* It might be that Don had deleted your file- I see that another question file was deleted since it contained personal data (and your may have contained proprietary information) 
John_Ru (rep: 6152) Jan 28, '22 at 5:07 am
No comment?
John_Ru (rep: 6152) Feb 1, '22 at 12:39 pm
Brad. I've provided answers to two of your recent questions but got no response from you.- am I doing something wrong or are you posting the questions (and getting better answers) elsewhere?
John_Ru (rep: 6152) Feb 4, '22 at 8:27 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login