Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-08-2008, 03:22 AM
Tribos Tribos is offline
Novice
Find and Replace Macro - A Better Way
 
Join Date: Oct 2008
Posts: 1
Tribos is on a distinguished road
Default Find and Replace Macro - A Better Way

Hi



It started with hours of fruitless googling, then after a struggle I managed to record a find and replace macro to create subscripts. Finally I have come up with a working solution but IMHO the code is ugly. I'm sure there is a way to tidy it up... would love to know how.

In the meanwhile the Macro converts N2 to N2, CO2 to CO2, etc. The code is repetitive, I'm sure it's possible to put a Dim ### as Range or so... to tidy it up. Would love some feedback.

Kind regards

Steve

____________

Code:
Sub FixChemicals()
'
' FixChemicals Macro
' Macro recorded 10/8/2008 by STEVEN
'
'Nitrogen
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "N2"
        .Replacement.Text = "N2makesub"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
'Oxygen
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "O2"
        .Replacement.Text = "O2makesub"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
'Water
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "H2O"
        .Replacement.Text = "H2makesubO"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
'CarbonDioxide
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "CO2"
        .Replacement.Text = "CO2makesub"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll


'Convert into subscript
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "2makesub"
        .Replacement.Text = "2"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    
    With Selection.Find.Replacement
        .Font.subscript = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro Issue Basanth Excel 1 12-05-2008 08:07 PM
Macro Writing cweachter Excel 0 08-09-2008 08:30 PM
**FIND OUT WHO WAS BCC(BLIND CARBON COPIED) ON YOUR EMAILS?? sharpescalade Outlook 1 11-30-2006 08:07 AM
Find and Replace Macro - A Better Way How Do I Replace Nulls With Zeros? deekadelic Excel 1 08-11-2006 09:52 PM
Find and Replace Macro - A Better Way Find and Replace box flies off screen! M Skabialka Word 2 06-06-2006 10:05 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 07:37 AM.


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