#1
|
|||
|
|||
Find max number of same subsequent character
Hi to all,
I'm looking for a quick way to find the maximum number a same subsequent character in a range. Right now I use the find method to find multiple subseqent spaces, compare the number of spaces found (</>) to current maximum and then continue search: "Do While .Find.Found". This method works, but is rather time consuming. So my question: any idea(s) how I could speed up my code? Any help - as usual - would be appreciated NP |
#2
|
||||
|
||||
It would be helpful if you actually posted the code ... How else do you expect anyone to say how you can make it better?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
You're right - sorry.
Code:
Function CountSpaces() As Integer Dim doc As Document: Set doc = ActiveDocument Dim rngFind As Range On Error GoTo EndOfSub Application.ScreenUpdating = False Call ClearFind If Selection.Type < 2 Then Set rngFind = doc.Range Else Set rngFind = Selection.Range End If CountSpaces = 0 With rngFind With .Find .Text = "^32{2;}" .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Execute End With Do While .Find.Found If Len(rngFind.Text) > CountSpaces Then _ CountSpaces = Len(rngFind.Text) .End = .End + 1 .Collapse wdCollapseEnd .Find.Execute Loop End With EndOfSub: End Function |
#4
|
||||
|
||||
How about:
Code:
Sub GetMaxSpaces() Dim StrTxt As String, StrTmp As String, i As Long StrTxt = ActiveDocument.Range.Text: StrTmp = " " While InStr(StrTxt, StrTmp) > 0 StrTmp = StrTmp & " " i = i + 1 Wend MsgBox "The maximum space sequence is: " & i End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Much faster .
Find, simply is the wrong approach. You made my (Sun)day. Thx NP |
#6
|
|||
|
|||
Quote:
How can I ensure that only blanks are counted for text in "myCount" style within you super fast code. Maybe it's just too early for programming and my brain is still in sleep mode ... Thanks again NP |
#7
|
||||
|
||||
Shifting the goal posts, eh? You could turn it into a function, thus:
Code:
Sub Demo() Application.ScreenUpdating = False Dim i As Long, j As Long, Rng As Range Set Rng = ActiveDocument.Range With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Format = True .Style = "Normal" .Forward = True .Wrap = wdFindStop .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Do While .Find.Found If InStr(.Text, " ") > 0 Then i = GetMaxSpaces(.Text) If i > j Then j = i End If If .End = Rng.End Then Exit Do .Collapse wdCollapseEnd .Find.Execute Loop End With Application.ScreenUpdating = True MsgBox "The maximum space sequence is: " & j End Sub Function GetMaxSpaces(StrTxt As String) Dim StrTmp As String, i As Long StrTmp = " " While InStr(StrTxt, StrTmp) > 0 StrTmp = StrTmp & " " i = i + 1 Wend GetMaxSpaces = i End Function
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Quote:
not really, just the wrong time for programming. Your 'Find' solution comes close to my original code and, thus, is as slow. Maybe I will have to rethink that formatting issue. NP |
#9
|
||||
|
||||
Another way would be to use Find/Replace to:
a) hide all text; b) unhide text in your Style. You could then pass the entire document range to the function (hidden text wont be passed) before you c) unhide all remaining text. Obviously, though, this won't work if you already have hidden text you need to keep hidden.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
Hi Paul,
as the macro will be for my use only, the hide - unhide method will do. 'My' other hidden text (the text that will have to remain hidden) is not only bound to the hidden attrib, but also to a font and shading color. So it will be easy to unhide all, 're-hide' other hidden. Thx NP |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help!! find two number recurring numbers in row?? | carrolld2 | Excel | 5 | 06-28-2014 05:43 PM |
macro to find a character and insert space so autocorrect will expand | redzan | Word VBA | 3 | 05-22-2014 04:22 PM |
Help with the "find and replace" option; character problem | martinn4 | Word | 3 | 02-20-2014 03:52 AM |
Conditional tied to trying to find a " " space character | Dart82 | Word VBA | 3 | 10-04-2013 01:47 PM |
Concatenated data in subsequent rows | doorsgirl | Excel | 4 | 09-15-2011 10:37 PM |