+ Reply to Thread
Results 1 to 9 of 9

Use Find and Replace in a macro

  1. #1
    Registered User
    Join Date
    02-19-2013
    Location
    Leeds, England
    MS-Off Ver
    Office 365
    Posts
    87

    Use Find and Replace in a macro

    I have 2 cells on a worksheet.

    One contains text I want to search for and the other contains the text I want to replace it with.

    I have a macro which does it all but I need the macro to access the above 2 cells each time as they will always be different. Is there a simple VB command for this please?

  2. #2
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Use Find and Replace in a macro

    Please Login or Register  to view this content.
    sheet 1 is the sheet with the cells you want to perform f&r and A1:B2 is the address of the cells you want to run your find & replace over (use .cells.replace if you want it across all)
    sheet 2 is the sheet with the find and replace strings, C1 is the address for the find string, D1 is the address for the replace string
    CC


    If you feel really indebted please consider a donation to charity. My preferred charity is ActionAid but there are plenty of worthy alternatives.

  3. #3
    Registered User
    Join Date
    02-19-2013
    Location
    Leeds, England
    MS-Off Ver
    Office 365
    Posts
    87

    Re: Use Find and Replace in a macro

    Many thanks. I'll give that a try in the morning and if I can get it to work, donation to Actioaid will follow!

  4. #4
    Registered User
    Join Date
    02-19-2013
    Location
    Leeds, England
    MS-Off Ver
    Office 365
    Posts
    87

    Re: Use Find and Replace in a macro

    Hi Cheeky Charlie. I inserted the code you suggested and I am getting an Error '9' Subscript out of range. Any ideas on what I have done wrong? Copy of Macro is shown below. Hope you can help. Many thanks
    Sheets("Monthly Summary").Select
    ActiveSheet.Unprotect
    Sheets("New Store Skeleton").Select
    ActiveSheet.Unprotect
    Sheets("Monthly Summary").Select
    Rows("4:32").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Sheets("New Store Skeleton").Select
    Range("A4:AQ32").Select
    Selection.Copy
    Sheets("Monthly Summary").Select
    Range("A4").Select
    ActiveSheet.Paste
    ActiveWindow.SmallScroll Down:=-3
    Sheets("Monthly Summary").Select
    Sheets("Monthly Summary").Range("A4:AZ32").Replace Sheets("Macros").Range("B6"), Sheets("Macros").Range("B9"), xlValues
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Sheets("New Store Skeleton").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Sheets("Monthly Summary").Select

  5. #5
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Use Find and Replace in a macro

    Hi there, please put your code in tags, then I'll gladly help

  6. #6
    Registered User
    Join Date
    02-19-2013
    Location
    Leeds, England
    MS-Off Ver
    Office 365
    Posts
    87

    Re: Use Find and Replace in a macro

    Hi again. Hope I've done this properly! And thanks again in advance.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Use Find and Replace in a macro

    My mistake, I used the wrong argument (xlValues)

    I think the below does what you need, but would need an example from you to test it:
    Please Login or Register  to view this content.
    Please do look at the code, and see how the changes I've made work. There are two main things I've done to improve the readability and efficiency of the code - replace "thing.select ... selection.action" with "thing.action" and use of the 'with' construct.

    If this doesn't do what you need, upload an example with your next post.

  8. #8
    Registered User
    Join Date
    02-19-2013
    Location
    Leeds, England
    MS-Off Ver
    Office 365
    Posts
    87

    Re: Use Find and Replace in a macro

    Great Results thanks - Everything works perfectly now.

    If its OK with you, I will make donation to MacMillan Cancer Support as I regularly raise funds to help them.

    And thanks again for your help

  9. #9
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Use Find and Replace in a macro

    Absolutely, I think MacMillan is a very worthy cause, I sincerely hope I never need their services.

    I don't think you would donate if you didn't want to anyway, but I feel I ought to say: The help I provide here is completely free - I wouldn't want to imply that there is any contract (social, implied or otherwise!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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