Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2014, 10:30 AM
NobodysPerfect NobodysPerfect is offline Find max number of same subsequent character Windows 7 64bit Find max number of same subsequent character Office 2010 32bit
Competent Performer
Find max number of same subsequent character
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 05-24-2014, 06:09 PM
macropod's Avatar
macropod macropod is offline Find max number of same subsequent character Windows 7 32bit Find max number of same subsequent character Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 05-24-2014, 11:07 PM
NobodysPerfect NobodysPerfect is offline Find max number of same subsequent character Windows 7 64bit Find max number of same subsequent character Office 2010 32bit
Competent Performer
Find max number of same subsequent character
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

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
NP
Reply With Quote
  #4  
Old 05-24-2014, 11:20 PM
macropod's Avatar
macropod macropod is offline Find max number of same subsequent character Windows 7 32bit Find max number of same subsequent character Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 05-24-2014, 11:31 PM
NobodysPerfect NobodysPerfect is offline Find max number of same subsequent character Windows 7 64bit Find max number of same subsequent character Office 2010 32bit
Competent Performer
Find max number of same subsequent character
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Much faster .

Find, simply is the wrong approach.

You made my (Sun)day. Thx

NP
Reply With Quote
  #6  
Old 05-25-2014, 12:00 AM
NobodysPerfect NobodysPerfect is offline Find max number of same subsequent character Windows 7 64bit Find max number of same subsequent character Office 2010 32bit
Competent Performer
Find max number of same subsequent character
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Quote:
You made my (Sun)day. Thx
Not yet ... the next problem to solve: I only want to count the blanks if the text is formatted in a specific style. Text formatted in any other style has to be ignored/skipped.

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
Reply With Quote
  #7  
Old 05-25-2014, 12:16 AM
macropod's Avatar
macropod macropod is offline Find max number of same subsequent character Windows 7 32bit Find max number of same subsequent character Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Simply change 'Normal' to your Style name.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 05-25-2014, 12:48 AM
NobodysPerfect NobodysPerfect is offline Find max number of same subsequent character Windows 7 64bit Find max number of same subsequent character Office 2010 32bit
Competent Performer
Find max number of same subsequent character
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Quote:
Shifting the goal posts, eh?
Hi Paul,

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
Reply With Quote
  #9  
Old 05-25-2014, 12:53 AM
macropod's Avatar
macropod macropod is offline Find max number of same subsequent character Windows 7 32bit Find max number of same subsequent character Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #10  
Old 05-25-2014, 01:10 AM
NobodysPerfect NobodysPerfect is offline Find max number of same subsequent character Windows 7 64bit Find max number of same subsequent character Office 2010 32bit
Competent Performer
Find max number of same subsequent character
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find max number of same subsequent character Help!! find two number recurring numbers in row?? carrolld2 Excel 5 06-28-2014 05:43 PM
Find max number of same subsequent character macro to find a character and insert space so autocorrect will expand redzan Word VBA 3 05-22-2014 04:22 PM
Find max number of same subsequent character Help with the "find and replace" option; character problem martinn4 Word 3 02-20-2014 03:52 AM
Find max number of same subsequent character 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:18 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft