Consulting

Results 1 to 2 of 2

Thread: change a string in each worksheet

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    change a string in each worksheet

    hello
    i have a workbbok with many sheets. each sheet contains the month name.each month i have to submit a report.
    i am trying to replace at once all previous month with the new month name.i do not want to repeat the action on each sheet.
    [VBA]
    Dim X, Y
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    Y = InputBox("what to replace")
    If Y = vbCancel Then Exit Sub
    X = InputBox("with what")
    If Y = vbCancel Then Exit Sub
    Cells.Replace What:=Y, _
    Replacement:=X, LookAt:=xlPart, MatchCase:=False
    Next
    Exit Sub

    [/VBA]
    thanks
    moshe

  2. #2
    VBAX Regular HaHoBe's Avatar
    Joined
    Aug 2004
    Location
    Hamburg
    Posts
    89
    Location
    Hi, moshe,

    if the names are identical for all the worksheets put both the Inputboxes outside the loop:

    [vba]Dim X, Y
    Dim ws As Worksheet
    Y = InputBox("what to replace")
    If Y = "" Then Exit Sub
    X = InputBox("with what")
    If X = "" Then Exit Sub
    For Each ws In ActiveWorkbook.Worksheets
    ws.Cells.Replace What:=Y, _
    Replacement:=X, LookAt:=xlPart, MatchCase:=False
    Next[/vba]
    If different names are used for the worksheets at least get the information from the previous inputbox:
    [vba]Dim X, Y
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
    Y = InputBox("what to replace", , X)
    If Y = "" Then Exit Sub
    X = InputBox("with what")
    If X = "" Then Exit Sub
    ws.Cells.Replace What:=Y, _
    Replacement:=X, LookAt:=xlPart, MatchCase:=False
    Next[/vba]
    If the names are generated by formatting the date in MMM or MMMM this macro a different way must be use to achieve.

    Ciao,
    Holger

Posting Permissions

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