Bold ends

vegas808

New Member
Joined
Dec 17, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I'm wondering if this program could be optimized for speed if I have 1000+ rows. Here's my simple program. I have 3 columns, on a worksheet, I really want to have this formatting. See my attached samples

Sub BoldEnds()

Dim rw As Long
Dim endrw As Long: endrw = Range("C" & Rows.count).End(xlUp).Row

Application.ScreenUpdating = False

For rw = 11 To endrw

With Range("C" & rw)
.Font.Bold = False
.Characters(start:=1, Length:=InStr(1, .Value, "-") - 1).Font.Bold = True
.Characters(Len(.Value) - 1, 2).Font.Bold = True
End With

Next rw

Application.ScreenUpdating = True

End Sub
 

Attachments

  • Screenshot 2022-12-19 154446.jpg
    Screenshot 2022-12-19 154446.jpg
    13.3 KB · Views: 5
  • Screenshot 2022-12-19 154352.jpg
    Screenshot 2022-12-19 154352.jpg
    14.7 KB · Views: 5

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
The only thing I can think of is to bold it all outside of the loop, then un-bold from the first dash to the last one inside the loop. That would eliminate one formatting step and a loop to un-bold. I suspect you won't notice any difference until you start getting up to something like 5 to 10 thousand rows though.
EDIT - If the numeric portion is always the same length, could use Mid function.
 
Last edited:
Upvote 0
Actually, could use it regardless, but maybe not necessary or of much help. Working on something . . .
 
Upvote 0
10,000 rows took about 6 seconds:
VBA Code:
Sub BoldEnds()

Dim rw As Long, endrw As Long
Dim strStart As Integer, strEnd As Integer

endrw = Cells(Rows.count, "C").End(xlUp).Row
Application.ScreenUpdating = False
Range("C1:C" & endrw).Font.Bold = True

For rw = 11 To endrw
    With Range("C" & rw)
        strStart = InStr(.Value, "-") + 1
        strEnd = InStrRev(.Value, "-")
        .Characters(strStart, strEnd - strStart).Font.Bold = False
    End With
Next rw

Application.ScreenUpdating = True

End Sub
You could speed it up even more if you didn't start at C1. Instead, perhaps start with active cell or row, or use SelectionChange event to run from the selection. Anything that doesn't loop over rows that are already formatted this way.
 
Upvote 0
The only thing I can think of is to bold it all outside of the loop, then un-bold from the first dash to the last one inside the loop. That would eliminate one formatting step and a loop to un-bold. I suspect you won't notice any difference until you start getting up to something like 5 to 10 thousand rows though.
EDIT - If the numeric portion is always the same length, could use Mid function.
Thanks for your input.

"If the numeric portion is always the same length, could use Mid function."
The 2nd and 4th strings are at constant size.
 
Upvote 0
Numeric portion, not the string. Doesn't matter since the code I posted finds the first and last "-" and calculates the length from that. I doubt you could use Mid with the Characters property anyway.
 
Upvote 0
10,000 rows took about 6 seconds:
Wow. I have a pretty old machine so was expecting that time or longer but it did 10,000 rows for me in just under 1 second.
The code below (it accesses the worksheet less often) was about 10% faster when timed accurately, but at both under 1 second, on my machine the difference is not readily noticeable.

VBA Code:
Sub Bold_Ends()
  Dim a As Variant
  Dim i As Long, st As Long
 
  Application.ScreenUpdating = False
  With Range("C11", Range("C" & Rows.Count).End(xlUp))
    .Font.Bold = True
    a = .Value
    For i = 1 To UBound(a)
      st = InStr(1, a(i, 1), "-")
      .Cells(i).Characters(st, InStrRev(a(i, 1), "-") - st + 1).Font.Bold = False
    Next i
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
That looks pretty slick. If you don't mind answering a few questions I'd appreciate it.
With this: a = .Value
you assign the entire range contents to an array in one go? If so, what if some cells are blank? Do those end up in an array of variants as Nulls or are they skipped?
The first element index in the array is one (not zero) because this is an array of ranges (cells)? I think I've got to read up on that array type.
TIA.
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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