+ Reply to Thread
Results 1 to 9 of 9

Find/Replace VBA Risk

  1. #1
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Find/Replace VBA Risk

    Greetings,
    There is a big risk for Find/Replace macro.
    Let me explain how;

    1- Open an excel file.
    2- Be sure that you have two sheets in this file, first one is Sheet1 and second one is Sheet2.
    3- Enter Apple into Sheet1 A1 cell.
    4- Enter Apple into Sheet2 A1 cell.
    5- Press Ctrl + F in oder to open find and replace dialog box.
    6- Enter Apple into find and replace dialog box, select ThisWorkbook instead of ThisSheet, and click Find All button. (You did this because you want to see all Apple values in the workbook.) (Everything is okey until now) (Close find and replace dialog box now.)
    7- Now, you want to replace Apple value to Orange by using following code only in the Sheet1.
    Please Login or Register  to view this content.
    But code above replaces Apple values to Orange values in all sheets!!! This is the very dangerous thing!
    Any idea???

    Reason;
    Find and replace dialog box DO NOT reset itself!!!
    Last edited by HerryMarkowitz; 10-18-2014 at 02:28 PM.
    Sub DontForgetThese()
         If Your thread includes any code Then Please use code tags...
         If Your thread has been solved Then Please mark as solved...
         If Anybody has helped to you Then Please add reputation...
    End Sub

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Find/Replace VBA Risk

    Herry;
    Yes this is a known bug. You will need to clear the dialogue box before running new searches. Look at this link and it will give you some additional code to add.

    http://stackoverflow.com/questions/2...box-parameters
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Find/Replace VBA Risk

    It's a bug of long standing. Do a dummy Find first:

    Please Login or Register  to view this content.
    You can also use that dummy Find to set the states of the persistent arguments LookAt, SearchOrder, and MatchByte.
    Last edited by shg; 10-18-2014 at 03:08 PM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find/Replace VBA Risk

    Hello HerryMarkowitz,

    If you read the VBA help files, you will find this information. It will tell you exactly which arguments you always need to set in both find and replace to avoid this problem.

    The reason is both the Find and Replace functions in VBA use the Windows System Find and Replace dialog. This dialog does not reset to make it easier for users to modify the search or replace.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Find/Replace VBA Risk

    Hi Alan,
    Thanks for the answer.
    But link you adviced does not mention how to change ThisWorkbook selection to ThisSheet selection in the find and replace dialog box.
    No mention because there is no way.
    http://stackoverflow.com/questions/2...box-parameters
    FYI.
    Last edited by HerryMarkowitz; 10-18-2014 at 02:48 PM.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Find/Replace VBA Risk

    Harry, did you read post#3?

  7. #7
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Find/Replace VBA Risk

    Quote Originally Posted by shg View Post
    Harry, did you read post#3?
    Yes I am.
    And I made simplier code thanks to your post;
    Please Login or Register  to view this content.
    But, I am waiting if any other opinions exist or not
    Last edited by HerryMarkowitz; 10-18-2014 at 03:23 PM.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find/Replace VBA Risk

    For those who are interested, here are the links to the online VBA Help for both the Find and Replace methods...

  9. #9
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,007

    Re: Find/Replace VBA Risk

    Thanks everybody.
    Last edited by HerryMarkowitz; 10-19-2014 at 09:23 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] find and replace to not replace characters found as wildcards
    By sabutler4 in forum Excel General
    Replies: 4
    Last Post: 07-03-2013, 06:48 PM
  2. find in excel replace in word: find/replace text in text boxes and headers
    By dean.rogers in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-10-2012, 12:40 PM
  3. Multiple Find and Replace to replace a list of strings
    By WalterP34 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-11-2011, 07:41 PM
  4. Find, Replace with and then replace adjacent cell
    By Craig2097 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-18-2009, 12:42 PM
  5. [SOLVED] find and replace - replace data in rows to separated by commas
    By msdker in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-14-2006, 08:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1