Consulting

Results 1 to 3 of 3

Thread: How to copy and paste only bold parts of string and not unbold parts of string?

  1. #1
    VBAX Regular
    Joined
    Oct 2017
    Posts
    22
    Location

    How to copy and paste only bold parts of string and not unbold parts of string?

    Is there a way to identify and copy only the bold text of a cell that has both bold and unbolded text?

    example:
    Range ("A1") = title details
    string1 = BoldOnly(Range("A1"))
    string1 = title
    string2 = unBoldOnly(Range("A1"))
    string2 = details

    Attached is my first attempt. It only provide values if entire cell is bold.
    Function BoldOnly(WorkRng As Range)
    If WorkRng.Font.Bold Then
        BoldOnly = WorkRng.Value
    End If
    End Function
    
    
    
    Sub CopyPasteBoldOnly()
        For rowNum = 2 To 49
            Cells(rowNum, "C") = GetBold(Cells(rowNum, "L"))
        Next
    End Sub

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Sub BoldOnlyC_UnBoldOnlyD()
        
        Dim rowNum As Long
        
        For rowNum = 2 To 49
            Cells(rowNum, "C") = BoldText(Cells(rowNum, "L"))
            Cells(rowNum, "D") = UnBoldText(Cells(rowNum, "L"))
        Next
    
    End Sub
    Function BoldText(rng As Range) As String
    
        Dim i As Long
        
        For i = 1 To Len(rng.Value)
            If rng.Characters(i, 1).Font.FontStyle = "Bold" Then
                BoldText = BoldText & rng.Characters(i, 1).Text
            End If
        Next i
    
    End Function
    
    
    
    Function UnBoldText(rng As Range) As String
    
        Dim i As Long
        
        For i = 1 To Len(rng.Value)
            If rng.Characters(i, 1).Font.FontStyle <> "Bold" Then
                UnBoldText = UnBoldText & rng.Characters(i, 1).Text
            End If
        Next i
    
    End Function

    PS: you may add below statement before the variable declarations in the functions to make formulas recalculate each time when cells change.
    Application.Volatile True
    Last edited by mancubus; 04-10-2018 at 08:00 AM.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Regular
    Joined
    Oct 2017
    Posts
    22
    Location
    Thank you for this! I thought there was a native feature, but I guess it was not to be. I'll use method for future character specific macros.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •