Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2014, 09:56 AM
sistemalan sistemalan is offline VBA to Find and Format Text string defined using Inputbox within selection Windows 7 64bit VBA to Find and Format Text string defined using Inputbox within selection Office 2013
Novice
VBA to Find and Format Text string defined using Inputbox within selection
 
Join Date: Sep 2014
Posts: 5
sistemalan is on a distinguished road
Question VBA to Find and Format Text string defined using Inputbox within selection

Hi all,

I am a new but keen convert to the wonderful world of VBA, and am hoping that some kind and knowledgeable person might be able to give me a quick bit of help.

To help with the timetabling of staff and volunteers for a children's after school club I need to create a macro which will function as follows:
  1. Before the macro is read I will manually select a chunk of text
  2. I run the macro. An Input box appears. I type someone's name into it and hit OK.
  3. The Macro searches within the highlighted selection and formats each occurence of the inputted name (red and strikethrough)

This will provide a quick way of fool-proofing and speeding up a task which I currently have to do manually very often, namely seeing who is off on a certain day and making sure they are marked as such on the timetable so that I can see what gaps need to be filled.

Huge gratitude for anyone who can point me in the right direction.

Many Thanks,

Alan
Reply With Quote
  #2  
Old 09-29-2014, 12:51 PM
gmaxey gmaxey is offline VBA to Find and Format Text string defined using Inputbox within selection Windows 7 32bit VBA to Find and Format Text string defined using Inputbox within selection Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oRngProcess As Word.Range
  Set oRng = Selection.Range
  Set oRngProcess = oRng.Duplicate
  With oRng.Find
    .Text = InputBox("Enter text to find.")
    Do
      .Execute
      If oRng.InRange(oRngProcess) Then
        With oRng
          .Font.ColorIndex = wdRed
          .Font.StrikeThrough = True
          .Collapse wdCollapseEnd
        End With
      Else
        Exit Do
      End If
    Loop
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 09-30-2014, 03:54 AM
sistemalan sistemalan is offline VBA to Find and Format Text string defined using Inputbox within selection Windows 7 64bit VBA to Find and Format Text string defined using Inputbox within selection Office 2013
Novice
VBA to Find and Format Text string defined using Inputbox within selection
 
Join Date: Sep 2014
Posts: 5
sistemalan is on a distinguished road
Default

Hi Greg,

Thank you so much that seems like it should be perfect, however for some reason Word is freezing when I run this. My selection is a whole table. I run the macro, input the name and hit OK. I can see the formatting has been applied as expected but then the Spinning wheel of death just keeps going and Windows tells me that Word has stopped responding. Curious. Any ideas what might be wrong here? I'm running Word 2013 on Window 7.

Cheers!

Alan
Reply With Quote
  #4  
Old 09-30-2014, 06:28 AM
gmaxey gmaxey is offline VBA to Find and Format Text string defined using Inputbox within selection Windows 7 32bit VBA to Find and Format Text string defined using Inputbox within selection Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Well that is interesting. I can't claim to understand why, but use:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oRngProcess As Word.Range
  Set oRng = Selection.Range
  Set oRngProcess = oRng.Duplicate
  With oRng.Find
    .Text = InputBox("Enter text to find.")
    Do
      .Execute
      If oRng.InRange(oRngProcess) Then
        With oRng
          If Len(oRng) = 0 Then Exit Do
          .Font.ColorIndex = wdRed
          .Font.StrikeThrough = True
          .Collapse wdCollapseEnd
        End With
      Else
        Exit Do
      End If
    Loop
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 09-30-2014, 08:28 AM
sistemalan sistemalan is offline VBA to Find and Format Text string defined using Inputbox within selection Windows 7 64bit VBA to Find and Format Text string defined using Inputbox within selection Office 2013
Novice
VBA to Find and Format Text string defined using Inputbox within selection
 
Join Date: Sep 2014
Posts: 5
sistemalan is on a distinguished road
Default

Thanks Greg. That appears to work perfectly. Placebo effect perhaps? :-)

I'm really grateful, this will save me a lot of time and I'll be able to modify the code to make a few more of my repetitive tasks a lot easier.

Cheers!

Alan
Reply With Quote
  #6  
Old 10-03-2014, 03:46 AM
sistemalan sistemalan is offline VBA to Find and Format Text string defined using Inputbox within selection Windows 7 64bit VBA to Find and Format Text string defined using Inputbox within selection Office 2013
Novice
VBA to Find and Format Text string defined using Inputbox within selection
 
Join Date: Sep 2014
Posts: 5
sistemalan is on a distinguished road
Default

Hi again,

One more question. This works fine with a unique name, however If I type "Ali", then it affects the first three letters of Alison (we have lots of staff and volunteers so sometimes names are similar). Any simple way to avoid that?

Thanks!

Alan
Reply With Quote
  #7  
Old 10-03-2014, 03:57 AM
gmayor's Avatar
gmayor gmayor is offline VBA to Find and Format Text string defined using Inputbox within selection Windows 7 64bit VBA to Find and Format Text string defined using Inputbox within selection Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Add the match whole word option (or the wildcard option)
e.g

Code:
With oRng.Find
        .MatchWholeWord = True
        .Text = InputBox("Enter text to find.")
        Do
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #8  
Old 10-03-2014, 07:28 AM
sistemalan sistemalan is offline VBA to Find and Format Text string defined using Inputbox within selection Windows 7 64bit VBA to Find and Format Text string defined using Inputbox within selection Office 2013
Novice
VBA to Find and Format Text string defined using Inputbox within selection
 
Join Date: Sep 2014
Posts: 5
sistemalan is on a distinguished road
Default

Thanks Graham,

That does the trick. Only weird thing I've noticed now is that if the name is not found within the selection the macro applies the formatting to all text within the selection. Any way to avoid that?

Thanks!

Alan
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to Find and Format Text string defined using Inputbox within selection Find text, format part of text in italic d4okeefe Word VBA 18 06-30-2022 11:35 PM
Macro to find text in between two characters and then format selected text? qcom Word 5 02-19-2015 11:23 PM
VBA to Find and Format Text string defined using Inputbox within selection Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM
VBA to Find and Format Text string defined using Inputbox within selection Find and replace a string of text errtu Word 1 01-31-2013 02:09 PM
VBA to Find and Format Text string defined using Inputbox within selection Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:55 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