+ Reply to Thread
Results 1 to 5 of 5

Macro to replace cells value automatically based on another cell change event

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2011
    Location
    London
    MS-Off Ver
    Excel 210
    Posts
    5

    Macro to replace cells value automatically based on another cell change event

    I know there are several threads but I wasn't able to find something which will fit my needs.
    I have a sheet in which I want to Find & Replace cell values based on the cell $B1 which will be a drop down list. Also I need to mention that the cells which I want to replace the string have formulas. Here's an example of what a cell looks:
    =IF('http://sharepoint.www.com/Industrial Engineering/2011.06 June/[Workbook.xls]June'!I218=0,"",'http://sharepoint.www.com/Industrial Engineering/2011.06 June/[Workbook.xls]June'!I218)

    By changing the value from $B1 from Workbook to Workbook1 I want to automatically replace in a specific range the values from the cell: Workbook.xls with Workbook1.xls

    I have a code which will work but I need to narrow down the replace function to a specific range like $A2:$BA250

    My macro looks like this:
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
      Dim txtW As String
      txtW = Range("$H$13").Value
      Dim txtR As String
      txtR = Range("$B$1").Value
    If Target.Address = "$B$1" Then
    
    Cells.Replace What:=txtW, Replacement:=txtR, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    
        Else
    Range("$b$1").Select
        End If
    End Sub
    Last edited by phsilverhp; 06-09-2011 at 09:44 AM. Reason: SOLVED

  2. #2
    Registered User
    Join Date
    10-15-2010
    Location
    Waregem, Belgium
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Macro to replace cells value automatically based on another cell change event

    Hi,

    you can include :

    For each c in Range("A2:BA250")
        Cells.Replace What:=txtW, Replacement:=txtR, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Next c
    I didn't test this, but i think this is the way to go.

    Alain

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,155

    Re: Macro to replace cells value automatically based on another cell change event

    Please add code tags to your OP, as in the following code example.

    I think you might be better with this, also untested

        Range("A2:BA250").Replace What:=txtW, Replacement:=txtR, LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    06-08-2011
    Location
    London
    MS-Off Ver
    Excel 210
    Posts
    5

    Re: Macro to replace cells value automatically based on another cell change event

    10x both for such a quick reply, I've tried both codes but TMShucks's works like a charm.
    Thanks a lot.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,155

    Re: Macro to replace cells value automatically based on another cell change event

    You're welcome. Thanks for the feedback and rep.

    Please go back to your original thread and add Code Tags. According to the forum rules, we should not really have provided an answer unless you comply with the rules. The Moderators are quite strict about applying the forum rules and we could all be reprimanded for not following them. It's a small price to pay to make this a useful repository of knowledge.

    Please also mark your thread as Solved. See my signature for details or the FAQ.

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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