Excel-how (find/replace) for multifmat cells w/o changing fmat

For a client, i need to be able to find and replace words in cells with 2 
formats. But when you replace them,it changes the format of the whole cell, 
and not just what you changed.  Is there any way to work around this without 
changing the format after all of the find and replaces?
0
Schiano (1)
6/22/2005 2:44:02 PM
excel.misc 78881 articles. 5 followers. Follow

2 Replies
898 Views

Similar Articles

[PageSpeed] 12

Saved from a previous post, but this one has some wordwrap problems fixed:

I think you'd have to keep track of each character's font attributes.

This works very slowly, but works:

Option Explicit
Option Compare Text

Type myCharacter
    myChar As String
    myLen As Long
    myName As String
    myFontStyle As String
    mySize As Double
    myStrikethrough As Boolean
    mySuperscript As Boolean
    mySubscript As Boolean
    myOutlineFont As Boolean
    myShadow As Boolean
    myUnderline As Long
    myColorIndex As Long
End Type
Sub testme()

    Application.ScreenUpdating = False

    Dim myWords As Variant
    Dim myNewWords As Variant
    Dim myRng As Range
    Dim foundCell As Range
    Dim iCtr As Long 'word counter
    Dim lCtr As Long 'length of string counter
    Dim cCtr As Long 'character counter
    Dim usedChars As Long
    Dim FirstAddress As String
    Dim AllFoundCells As Range
    Dim myCell As Range
    Dim myStr As String
    Dim myCharacters() As myCharacter
        
    myWords = Array("xxx", "yy")
    myNewWords = Array("qqqq", "BBBB")
    
    Set myRng = Selection
    
    On Error Resume Next
    Set myRng = Intersect(myRng, _
                  myRng.Cells.SpecialCells(xlCellTypeConstants, xlTextValues))
    On Error GoTo 0
    
    If myRng Is Nothing Then
        MsgBox "Please choose a range that contains text constants!"
        Exit Sub
    End If
    
    
    For iCtr = LBound(myWords) To UBound(myWords)
        FirstAddress = ""
        Set foundCell = Nothing
        With myRng
            Set foundCell = .Find(what:=myWords(iCtr), _
                                LookIn:=xlValues, lookat:=xlPart, _
                                after:=.Cells(.Cells.Count))
                                
            If foundCell Is Nothing Then
                MsgBox myWords(iCtr) & " wasn't found!"
            Else
                Set AllFoundCells = foundCell
                FirstAddress = foundCell.Address
                Do
                    If AllFoundCells Is Nothing Then
                        Set AllFoundCells = foundCell
                    Else
                        Set AllFoundCells = Union(foundCell, AllFoundCells)
                    End If
                    Set foundCell = .FindNext(foundCell)
                    
                Loop While Not foundCell Is Nothing _
                    And foundCell.Address <> FirstAddress
            End If
                    
        End With
        
        If AllFoundCells Is Nothing Then
            'do nothing
        Else
            For Each myCell In AllFoundCells.Cells
                ReDim myCharacters(1 To Len(myCell.Value))
                usedChars = 0
                cCtr = 1
                lCtr = 0
                Do
                    usedChars = usedChars + 1
                    With myCell.Characters(cCtr, 1)
                        myCharacters(usedChars).myName = .Font.Name
                        myCharacters(usedChars).myFontStyle = .Font.FontStyle
                        myCharacters(usedChars).mySize = .Font.Size
                        myCharacters(usedChars).myStrikethrough _
                                                    = .Font.Strikethrough
                        myCharacters(usedChars).mySuperscript _
                                                    = .Font.Superscript
                        myCharacters(usedChars).mySubscript = .Font.Subscript
                        myCharacters(usedChars).myOutlineFont _
                                                       = .Font.OutlineFont
                        myCharacters(usedChars).myShadow = .Font.Shadow
                        myCharacters(usedChars).myUnderline = .Font.Underline
                        myCharacters(usedChars).myColorIndex = .Font.ColorIndex
                            
                        If Mid(myCell.Value, cCtr, Len(myWords(iCtr))) _
                                                         = myWords(iCtr) Then
                            myCharacters(usedChars).myChar = myNewWords(iCtr)
                            myCharacters(usedChars).myLen _
                                = Len(myNewWords(iCtr))
                            cCtr = cCtr + Len(myWords(iCtr))
                            lCtr = lCtr + Len(myNewWords(iCtr))
                        Else
                            myCharacters(usedChars).myChar _
                                                = Mid(myCell.Value, cCtr, 1)
                            myCharacters(usedChars).myLen = 1
                            cCtr = cCtr + 1
                            lCtr = lCtr + 1
                        End If
                        If cCtr > Len(myCell.Value) Then Exit Do
                    End With
                Loop

                myStr = Space(lCtr)
                lCtr = 1
                For cCtr = 1 To usedChars
                    Mid(myStr, lCtr, myCharacters(cCtr).myLen) _
                                              = myCharacters(cCtr).myChar
                    lCtr = lCtr + myCharacters(cCtr).myLen
                Next cCtr
                myCell.Value = myStr
                cCtr = 1
                lCtr = 1
                Do
                    With myCell.Characters(lCtr, myCharacters(cCtr).myLen)
                        .Font.Name = myCharacters(cCtr).myName
                        .Font.FontStyle = myCharacters(cCtr).myFontStyle
                        .Font.Size = myCharacters(cCtr).mySize
                        .Font.Strikethrough _
                                   = myCharacters(cCtr).myStrikethrough
                        .Font.Superscript = myCharacters(cCtr).mySuperscript
                        .Font.Subscript = myCharacters(cCtr).mySubscript
                        .Font.OutlineFont = myCharacters(cCtr).myOutlineFont
                        .Font.Shadow = myCharacters(cCtr).myShadow
                        .Font.Underline = myCharacters(cCtr).myUnderline
                        .Font.ColorIndex = myCharacters(cCtr).myColorIndex
                    End With
                    lCtr = lCtr + myCharacters(cCtr).myLen
                    cCtr = cCtr + 1
                    If lCtr > Len(myStr) Then
                        Exit Do
                    End If
                Loop
            Next myCell
        End If
    Next iCtr
    
    Application.ScreenUpdating = True

End Sub


The new word actually takes the attributes of the first letter in the old word.
(I wasn't sure how to handle this if the number of letters in the replacement
word was different than the number of letters in the original word.

DEF replaced with EFGHIJK



Joe Schiano wrote:
> 
> For a client, i need to be able to find and replace words in cells with 2
> formats. But when you replace them,it changes the format of the whole cell,
> and not just what you changed.  Is there any way to work around this without
> changing the format after all of the find and replaces?

-- 

Dave Peterson
0
ec357201 (5290)
6/23/2005 12:05:51 AM
ps...


If you're new to macros, you may want to read David McRitchie's intro at: 
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Joe Schiano wrote:
> 
> For a client, i need to be able to find and replace words in cells with 2
> formats. But when you replace them,it changes the format of the whole cell,
> and not just what you changed.  Is there any way to work around this without
> changing the format after all of the find and replaces?

-- 

Dave Peterson
0
ec357201 (5290)
6/23/2005 12:06:15 AM
Reply:

Similar Artilces:

Extract emails from cells with text
I have a row in column A which includes an email address in the text that I'd like to extract to column B. Is there a formula I can use to accomplish extracting the email address only to column B? Here's an example of different cells in column A: Please email example@law.ufl.edu to contact us...... OR Schedule an appointment for assistance, or email example@uga.edu with your questions... Thank you Try this... All on one line: =TRIM(RIGHT(SUBSTITUTE(LEFT(A1,FIND (" ",A1&" ",FIND("@",A1))-1)," ", REPT(" ",...

How do I create project schedule using excel
I need to create a project schedule using excel. Please help. Hi lack of details :-) Maybe give some more information what you're trying to do? -- Regards Frank Kabel Frankfurt, Germany raeisza wrote: > I need to create a project schedule using excel. Please help. ...

Printing an Excel doucument
In this example, I have a document of 100 pages, when I print the document, the column headers only print on the first page and not the subsequent pages. also I would like all the pages to be numbered. is therea fix for this? Hi Richard, For your first question goto File>Page Setup>Sheet tab and look for the option 'Rows to repeat at top' For your second question goto View>Header and Footer and look at the &[page] and &[pages] options in your footer. HTH Martin ...

my printer ejects a blank sheet prior to printing with EXCEL
...

Error in formula displayed for linear and 2nd order curve fits in Excel 2003
I have created trendlines for some pretty simple data using both 2nd order polynomial and linear fits. In both cases, the displayed formulas have their 1st and 2nd order coefficients wrong by a factor of 10!! (I have read lots of posts about increasing precision but this is not what I am up against). Has anyone seen this? What version of Excel? Could you include the data in a follow up post (not as an attachment), along with the coefficients you've computed? - Jon ------- Jon Peltier, Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http://PeltierT...

Excel: extract and sum numerals from mixed text/numeral cell range
I have a large (30x20) grid of cells with data, and I want to extract and then sum up certain numerals from this entire range. The catch is that the data is mixed numerals and text, as you'll see below. Here's an abbreviated 3x3 example, with a value in each of the nine cells: V7.1 T H P1 A T B V3 P4.5 If I just wanted to sum up the instances of "T" appearing, I could use COUNTIF() for the entire range to come up with answer ("T" appears 2 times). Easy enough. But, what I'm trying to accomplish is to sum up the numerals associated w...

Windows Update
My problem is similar to someone else's below(about Excel 2003 not being able to update), the only difference is that I don't want Excel to update at all. I don't even use it. But the yellow shield kept telling me that it had 1 update available, which was Excel 2003, so, out of annoyance, I installed it. After failing to install several times, I decided to uninstall Excel from Microsoft Office(Pro Edition 2003). Even after uninstalling Excel, I still got the windows update alert. How do I stop it? Extra info: When windows update first appeared, I did a custom instal...

excel axes
how do I create a secondary horizontal axis on an xy scatter plot? forgot to mention 2008 excel using Leopard (10.5.2) ...

Changing Find settings
The default settings for my system are : within sheet, search by rows, lookin formulas. Is there any way to set the default to be: within workbook, search by columns, lookin values? Thank you, Fred How about a workbook_open macro that sets to desired -- Don Guillett Microsoft MVP Excel SalesAid Software dguillett1@austin.rr.com "fgwiii" <fgwiii@discussions.microsoft.com> wrote in message news:37D1731B-3784-4E94-92DB-0534937C8266@microsoft.com... > The default settings for my system are : within sheet, search by rows, > lookin > formulas. > > Is there a...

Excel program
I have a simple program in excel for data entry. When I protect the sheet, and use tab to move about within the spreadsheet, sometimes the tab button will not move the cursor to the next cell. It will pick a random cell to go to next. Is there a way that I can designate what cell it will go to next. Thanks for your help, jb On a protected sheet, it is going to the next unprotected cell. So, if you don't want it to go to a cell, protect it. "JB" <jgblanton@hotmail.com> wrote in message news:093a01c391d0$ae57f190$a301280a@phx.gbl... > I have a simple program in exce...

2003 Excel and 2007 Trial Version Excel
I downloaded the 2007 trial version of Microsoft Office. Now my trial is over and I reloaded my 2003 version. But it will not let me remove the 2007 trial version. Also when I go into my 2003 Excel a Window Installer box comes up that says Preparing to install and this stays on for about 5 minutes before it goes away. I know that I should not have downloaded the trial version but I did. Can someone please help me. I have Windows XP and it is my office computer. Please help! ...

Error Message When opening Excel
I am using Windows VISTA 64 bit O/S with Office Professional 2003 All of a sudden I started to get the following error message: 'One of your object libraries (Stdole32.tlb) is missing or damaged. Please run setup to install it' I am at a loss as how to correct this. I checked online and found some instructions that were for Windows 2000 Those did not work for VISTA at all. Thank you in advance for your support and suggestions, HankL It is telling you to do a "setup to install" on the Office product, did you try that? Did you try doing a repair on ...

Creating a Chart from Cells that use a List Box
Hi, I am trying to create a number of charts (e.g., pie, line etc) based on data that is slelected from a list box (i.e., data validation). What i would like to do is create the charts before any data is entered (if that makes sense) and as data is entered the charts will display this. ...

MFC: Excel Automation Can't Quit from Excel. Please help...
Hi I use MFC Visual C++ 4.2. I am automating Excel from my application. My problem is I cannot quit the Excel after the automation process finished! I checked it still exists on the Process lists and the most DANGEROUS is everytime the automation function run, it create other Excel and so on. So it must be out of memory soon. I have used the app.Quit() method like all the people used but it DOES NOT work. What should I do? Thank you. I happened to answer this on the office.developer.vba newsgroup. You most likely have neglected to Release() the automation object. joe On Wed, 3 Sep...

Hey /w VB Code...
I had this code to switch my view every hour to the Outlook main screen; but it no longer works /w Outlook 2003 - can anyone help? Thanks in advance! Dim iHour Private Sub SwitchView() If Hour(Now) <> iHour Then Dim ctl As Office.CommandBarControl Dim cbpop As Office.CommandBarPopup Dim cb As Office.CommandBar Set cb = Application.ActiveExplorer.CommandBars("Menu Bar") Set ctl = cb.FindControl(ID:=5599, Recursive:=True) ctl.Execute iHour = Hour(Now) Set ctl = Nothing Set cb = Nothing End If End...

Excel charts should allow "switching" series on or off
A plot (chart) of time series data may be used for many purposes. From my use two reasons I use such plots are: Studying individual time series trends; and Overall impression of all results. But when studying multiple time series data a chart can become cluttered. It would be useful if individual series could be seleceted as visible or invisible so that they could be evaluated without losing the total plot. At the moment the only way to "unclutter" the plot is to remove the temporaily-unwanted series but it can be time consuming to reconstruct the plot. ---------------- Thi...

Excel number / date conversion
I've just pulled some data out of an ODBC connection into Access an then exported it to Excel. The dates are currently in the format 20031109. But the built in dat convertor (from Format Cells) doesn't do anything, even if I convert i to a 'proper' number first. How can I convert this to 09/11/2003 so that I can do some date - base arithmetic and work out difference in days -- markperr ----------------------------------------------------------------------- markperry's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=2759 View this thread: http://w...

Email from Excel #3
Is it possible to have Excel send an email if the value of a certain cell is changed? Hi see your post in public.excel -- Regards Frank Kabel Frankfurt, Germany "berj" <berj@adelphia.net> schrieb im Newsbeitrag news:CcSdndsVDJy4jcjcRVn-pQ@adelphia.com... > Is it possible to have Excel send an email if the value of a certain cell is > changed? > > ...

version excel
bonjour a tous, & a tous les pro bien sur es qu'il est possible ,que des l'instant on modifie le code VBA EXCEL on incremente un compteur pour les mise a jour cela me semble plus simple merci pour vos reponse patrick.villa@laposte.net +++++ http://www.mcgimpsey.com/excel/udfs/sequentialnums.html -- --- HTH Bob (change the xxxx to gmail if mailing direct) "pounet" <patrick.villa@laposte.net> wrote in message news:1165418782.552172.169740@j44g2000cwa.googlegroups.com... > bonjour a tous, & a tous les pro bien sur > > es qu'il est possible ,qu...

double line spacing using excel 2000
I have a paragraph of writing, how do i get the text to be double line spaced? I am using excel 2000. Excel doesn't have a line spacing setting. You could add line breaks by pressing Alt+Enter at the end of a line, then another Alt+Enter to create a blank line. Dan wrote: > I have a paragraph of writing, how do i get the text to be double line > spaced? I am using excel 2000. -- Debra Dalgleish Excel FAQ, Tips & Book List http://www.contextures.com/tiptech.html Does this aply to newer versions of excel? "Debra Dalgleish" wrote: > Excel doesn't have a...

Excel 95 to Excel 2003
Anyone know if it's possible to convert Excel 95 files to Excel 2003? If so, how. Thanks in advance Open the file in 2003 and SAVE as you would a file created in 2003. If not absolutely positive, save as a different name. -- Don Guillett SalesAid Software donaldb@281.com "Cooper" <kcooper2@austin.rr.com> wrote in message news:uqLpEuG4EHA.1292@TK2MSFTNGP10.phx.gbl... > Anyone know if it's possible to convert Excel 95 files to Excel 2003? If > so, how. > > Thanks in advance > > Open in XL 2003, when you close it, it should, ask you if you w...

Conversion Excel 97 to Excel 2003
Hi There, I would like to convert Excel 97 files to excel 2003 I m using Save AS, but little bit confuse what type I have to choose "Microsoft Office Excel workbood (*.xls)" or "Microsoft Excel 97 - Excel 2003 & 5.0/95 workbook (*.xls)". We will not use Office 97 anymore next few weeks. Thank for your prompt response. Wayne You shouldn't have to do anything. If you feel the need, just save as microsoft excel workbook.xls -- Don Guillett SalesAid Software donaldb@281.com "Wayne" <anonymous@discussions.microsoft.com> wrote in message news:8286...

Inner Borders in Excel 2007
Before in pre-Excel 2007 world, I used to be able to select a range of cells, click on a button to get vertical or horizontal boarders for the range selected. In Excel 2007 I can only apply outer boarders. Was this feature removed? I now have go to "format cells", then select the "borders" tab to do the same. Where did the buttons for inner vertical/horizontal boarders go? Thanks. louishong - Home ribbon > (Font group) drop down between Underline and Fill Color - Mike http://www.MikeMiddleton.com <louishong@lycos.com> wrote in message...

Convert excel to java
Hi does anybody know a way to convert an XL spreadhseet (with charts) to a fully interactive Java object?? I have tried exceleverywhere and xlsius, but no luck! Thanks in advance -- shnim1 ...

Find values of properties of checkbox
How do you find the values of a property (such as whether or not they are checked) when the check boxes have been created on the fly? Any help would be much appreciated! *** Sent via Developersdex http://www.developersdex.com *** "Mike P" <mike.parr@gmail.com> wrote in message news:OBhh2ds4KHA.1924@TK2MSFTNGP06.phx.gbl... > How do you find the values of a property (such as whether or not they > are checked) when the check boxes have been created on the fly? Is it a Web or WinForms application? If you did not keep a Reference to the checkboxe...