PDA

View Full Version : [SOLVED] How to copy and paste only bold parts of string and not unbold parts of string?



thk12205
04-09-2018, 05:22 PM
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

mancubus
04-10-2018, 07:47 AM
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

thk12205
04-16-2018, 02:21 PM
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.