Excel vba set bold specific text of string into the cell in Pivot table.

trishcollins

Board Regular
Joined
Jan 7, 2006
Messages
71
I found this code and altered it for my specific needs. I want a bunch of text highlighted in a Pivot table. The text is from a concatenated string from the source data table. The first time I run the code and refresh the Pivot table, it works great. Bolding and bolding and underlining on the text in the array. The second and subsequent times I refresh the Pivot table it bolds and underlines everything. Obviously, this is not ideal, given that I refresh the pivot table often as the source data changes. Is there any way to fix it?

VBA Code:
Sub Find_and_Bold()
Dim rCell As Range, sToFind As String, iSeek As Long
Dim Text(1 To 17) As String
Dim i As Integer
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables(1)


Text(1) = "Client Input"
Text(2) = "Results"
Text(3) = "Change Type:"
Text(4) = "Explanation:"
Text(5) = "Preferred Network Connectivity:"
Text(6) = "Preferred Network Path:"
Text(7) = "Preferred Network Connectivity for Remotely Connected Source Entity:"
Text(8) = "Network Path for Remotely Connected Source Entity:"
Text(9) = "Connectivity Notes:"
Text(10) = "Network Path Notes:"
Text(11) = "Business Need:"
Text(12) = "TBS Connectivity Pattern:"
Text(13) = "Target CSP:"
Text(14) = "Type:"
Text(15) = "Access Zone:"
Text(16) = "Conditions:"
Text(17) = "Source Entity:"

For Each rCell In pt.DataBodyRange
    For i = LBound(Text) To UBound(Text)
            sToFind = Text(i)
            iSeek = InStr(1, rCell.Value, sToFind)
        Do While iSeek > 0
            rCell.Characters(iSeek, Len(sToFind)).Font.Bold = True
            If i <= 2 Then
                rCell.Characters(iSeek, Len(sToFind)).Font.Underline = True
            Else
            End If
            iSeek = InStr(iSeek + 1, rCell.Value, sToFind)
        Loop
    Next i
Next rCell

End Sub

First time I refresh the pivot table:
1716314119734.png


Subsequent refreshes:
1716314219145.png


Trish ;)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I found this code and altered it for my specific needs. I want a bunch of text highlighted in a Pivot table. The text is from a concatenated string from the source data table. The first time I run the code and refresh the Pivot table, it works great. Bolding and bolding and underlining on the text in the array. The second and subsequent times I refresh the Pivot table it bolds and underlines everything. Obviously, this is not ideal, given that I refresh the pivot table often as the source data changes. Is there any way to fix it?

VBA Code:
Sub Find_and_Bold()
Dim rCell As Range, sToFind As String, iSeek As Long
Dim Text(1 To 17) As String
Dim i As Integer
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables(1)


Text(1) = "Client Input"
Text(2) = "Results"
Text(3) = "Change Type:"
Text(4) = "Explanation:"
Text(5) = "Preferred Network Connectivity:"
Text(6) = "Preferred Network Path:"
Text(7) = "Preferred Network Connectivity for Remotely Connected Source Entity:"
Text(8) = "Network Path for Remotely Connected Source Entity:"
Text(9) = "Connectivity Notes:"
Text(10) = "Network Path Notes:"
Text(11) = "Business Need:"
Text(12) = "TBS Connectivity Pattern:"
Text(13) = "Target CSP:"
Text(14) = "Type:"
Text(15) = "Access Zone:"
Text(16) = "Conditions:"
Text(17) = "Source Entity:"

For Each rCell In pt.DataBodyRange
    For i = LBound(Text) To UBound(Text)
            sToFind = Text(i)
            iSeek = InStr(1, rCell.Value, sToFind)
        Do While iSeek > 0
            rCell.Characters(iSeek, Len(sToFind)).Font.Bold = True
            If i <= 2 Then
                rCell.Characters(iSeek, Len(sToFind)).Font.Underline = True
            Else
            End If
            iSeek = InStr(iSeek + 1, rCell.Value, sToFind)
        Loop
    Next i
Next rCell

End Sub

First time I refresh the pivot table:
View attachment 111719

Subsequent refreshes:
View attachment 111721

Trish ;)
Thought I found my own fix. I added the following code before running the loop, and cleared the Pivot table of bold and underline. But doesn't fix the problem

pt.DataBodyRange.Font.Bold = False
pt.DataBodyRange.Font.Underline = False
 
Upvote 0
Thought I found my own fix. I added the following code before running the loop, and cleared the Pivot table of bold and underline. But doesn't fix the problem

pt.DataBodyRange.Font.Bold = False
pt.DataBodyRange.Font.Underline = False
This did work when I made this specific to the worksheet, as I had more than one Pivot table in the workbook.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top