VBA format text before and after a colon

umurpna

New Member
Joined
Nov 4, 2009
Messages
2
Hi all,
I have a meeting agenda table (produced by power query) that contains text I'd like to format as follows -
in D7:D13, I need to format the text before the colon Bold and ensure the text after the colon remains regular, not bold.

Meeting Name: text after the colon
Meeting Date: text after the colon
Meeting Time: text after the colon
Attendees: text after the colon
Apologies: text after the colon
Minutes: text after the colon
Copy To: text after the colon

I have this code from another thread:
Sub BoldPreColon()
Worksheets("Agenda").Activate
Dim i As Long, N As Long, s As String, j As Long
N = Cells(Rows.Count, "D").End(xlUp).Row
For i = 1 To 13
s = Cells(i, 4)
j = InStr(4, s, ":")
If j <> 0 Then
Cells(i, 4).Characters(1, j - 1).Font.Bold = True

End If
Next i
End Sub

It works beautifully, but when I refresh the power query table, it applies Bold to the entire cell contents. I need to be able to run a macro that reinstates regular, non-bold text after the colons.

Would be extremely grateful for any help as I have no coding skills at all!

Thanks!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
To remove the bold font after the colons, use:
VBA Code:
Sub UnboldAfterColon()
Dim i As Long, j As Long

For i = 1 To 13
    j = InStr(4, Sheets("Agenda").Cells(i, 4).Value, ":")
    If j <> 0 Then Sheets("Agenda").Cells(i, 4).Characters(j + 1, Len(Sheets("Agenda").Cells(i, 4))).Font.Bold = False
Next i
End Sub

In your original code to bold the pre-colon text, you don't need to activate the Agenda sheet, you're not using the variable N other than to find the last row (which you then don't use in the actual macro), and you don't need to assign the cell value to a variable to process it in the loop (s). So it can be simplified to:
VBA Code:
Sub BoldPreColon()
Dim i As Long, j As Long

For i = 1 To 13
    j = InStr(4, Sheets("Agenda").Cells(i, 4).Value, ":")
    If j <> 0 Then Sheets("Agenda").Cells(i, 4).Characters(1, j - 1).Font.Bold = True
Next i
End Sub
 
Upvote 0
Solution
To remove the bold font after the colons, use:
VBA Code:
Sub UnboldAfterColon()
Dim i As Long, j As Long

For i = 1 To 13
    j = InStr(4, Sheets("Agenda").Cells(i, 4).Value, ":")
    If j <> 0 Then Sheets("Agenda").Cells(i, 4).Characters(j + 1, Len(Sheets("Agenda").Cells(i, 4))).Font.Bold = False
Next i
End Sub

In your original code to bold the pre-colon text, you don't need to activate the Agenda sheet, you're not using the variable N other than to find the last row (which you then don't use in the actual macro), and you don't need to assign the cell value to a variable to process it in the loop (s). So it can be simplified to:
VBA Code:
Sub BoldPreColon()
Dim i As Long, j As Long

For i = 1 To 13
    j = InStr(4, Sheets("Agenda").Cells(i, 4).Value, ":")
    If j <> 0 Then Sheets("Agenda").Cells(i, 4).Characters(1, j - 1).Font.Bold = True
Next i
End Sub
This is amazing, thank you! It does exactly what I need and you've saved me hours of mucking about doing this manually!

So grateful for your help; you've made my Saturday!
 
Upvote 0

Forum statistics

Threads
1,212,934
Messages
6,110,760
Members
448,295
Latest member
Uzair Tahir Khan

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