Consulting

Results 1 to 4 of 4

Thread: Find/Replace in Specific Workcheet

  1. #1
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    5
    Location

    Question Find/Replace in Specific Workcheet

    I am writing a macro to find and replace a certain string within a certain worksheet within a workbook. The macro I have found is below, but searches the entire workbook. Please can someone advise how to amend this to only look in one particular worksheet?


    Sub FindReplaceAll()

    Dim sht As Worksheet
    Dim fnd AsVariant
    Dim rplc AsVariant

    fnd = "April"
    rplc = "May"

    ForEach sht In ActiveWorkbook.Worksheets
    sht.Cells.Replace what:=fnd, Replacement:=rplc, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False
    Next sht

    EndSub

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Try:
    Sub FindReplaceAll()
    Dim fnd
    Dim rplc
    
    fnd = "April"
    rplc = "May"
    ActiveWorkbook.Sheets("TheNameOfYourSheetHere").Cells.Replace what:=fnd, Replacement:=rplc, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    End Sub
    Obviously, adjust TheNameOfYourSheetHere
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    5
    Location
    Thanks p45cal - I will try this when I am back in the office later this week. Will keep you posted!

  4. #4
    VBAX Newbie
    Joined
    Sep 2015
    Posts
    5
    Location
    p45cal - Tried your macro and it worked a treat. Many thanks.

Posting Permissions

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