I'm using vba for checking a spreadsheet for strikethrough text. As
ActiveCell.Font.Strikethrough
only detects strikethrough in the entire cell, I used following code that counts individual characters with strikethrough.
Dim iCh As Long
Dim StrikethroughFont As Long: StrikethroughFont = 0
If Len(ActiveCell) > 0 Then
For iCh = 1 To Len(ActiveCell)
With ActiveCell.Characters(iCh, 1)
If .Font.Strikethrough = True Then
StrikethroughFont = StrikethroughFont + 1
End If
End With
Next iCh
End If
Code works as it should. Problem is that execution time increases exponentially with cell content length.
- with less than 100 characters in every cell, code runs superfast.
- with 1000 characters somewhere in 1 cell execution time is 30 seconds - still acceptable for the project
- with 3000 characters somewhere in 1 cell execution time about half an hour.
- with 5000 characters somewhere in 1 cell Excel continues to run seemingly forever, sometimes it crashes
I know that Excel is not intended for writing stories in a cell and revising them with strikethrough. But I have not control over what people do with these spreadsheets. Most people behave, but sometimes an individual exaggerates. I don't want this individual to make my work look bad. A not-so-nice workaround I found is adding a
And Len(ActiveCell) < 1000
statement to the first If, so that it completely skips cells with over 1000 characters.
I'm fearing that Excel 2010 SP2 that I'm using is not handling the ActiveCell.Characters(iCh, 1) very well.
Any suggestions to speed things up?
Question update after reading the many valuable replies & comments As pointed out, I made an incorrect statement in my question on line 3 and update it now in order not to mislead readers who haven't read all comments yet:
ActiveCell.Font.Strikethrough
Can actually detect partial strikethrough text in a cell: The possible return values are FALSE, TRUE and NULL, the latter meaning that there is a mix of strikethrough and normal font in the cell. This has no influence on the 'exponential' part of the question, but a lot on the 'workaround' part.