VBA Replace needs Sheet to be active, why?

sirplus

New Member
Joined
Sep 9, 2007
Messages
13
HI
My issue is it only replaces all instances on the ActiveSheet
oSht.Cells should allow it to work on all cells on oSht

Short Version
Change:
VBA Code:
For Each oSht In ThisWorkbook.Worksheets
            oSht.Activate
            oSht.Cells.Replace What....

into

VBA Code:
For Each oSht In ThisWorkbook.Worksheets
            oSht.Cells.Replace What....
Yet still work
It would be a lot neater if I could make replace work without the sheet being "made over" being active.
Without the Activate it only replaces all instances on the ActiveSheet
I'm sure I've done this many times in the past

The Project version
I have 3 columns
1 contains "Old Values"
2 contains "New Values"
3 Boolean TRUE Old = New, FALSE if different.

If they are different (Col3 = FALSE) I want to replace all occurrences in all cells on every worksheet with the new values.

Seems simple enough.
Cells refers to Cells on Activesheet so we need to loop through the sheets and apply replace
This should work fine only doesn't unless we make the sheet active.
Why the need OR how to make it work without activate?...

Quick & Dirty code follows

VBA Code:
Option Explicit

Sub CQSPartDesc_Adjust()
'   Select all cells in the column flagging if the Part Desc Changed 
'    Looking for values FALSE  (use a formula like =B4=J4) 
'    Then Run this code
   
    Dim oCell As Range
    Dim oShtOrig As Worksheet
    Dim oSht As Worksheet
    Dim strDescOld As String
    Dim strDescNew As String
   
    'Adjust these for correct Offset (old to the RHS in the instance used), Offsets relate to the Col with Flags!
    Const cintColOffsetReplaceThis As Integer = 4 'Number of Columns offset Data to Replace
    Const cintColOffsetReplaceWith As Integer = -4 'Number of Columns offset Data to Replace With
   
    Application.ScreenUpdating = False
    Set oShtOrig = ActiveSheet
    For Each oCell In Selection.Cells
    If oCell Then
        GoTo getnext
    Else
        strDescOld = oCell.Offset(0, cintColOffsetReplaceThis).value
        strDescNew = oCell.Offset(0, cintColOffsetReplaceWith).value
       
        For Each oSht In ThisWorkbook.Worksheets
            oSht.Activate
            oSht.Cells.Replace What:=strDescOld, _
                Replacement:=strDescNew, LookAt:=xlPart, _
                SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
        Next oSht
    End If
getnext:
    oShtOrig.Activate
    Next oCell
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Well here is a example of how I would do it.
Modify to your needs.
VBA Code:
Sub Replace_All_Sheets()
'Modified 12/21/2019 12:52:44 AM  EDT
Application.ScreenUpdating = False
Dim i As Long
Dim r As Range
For i = 1 To Sheets.Count
    With Sheets(i)
    
        For Each r In .UsedRange
            If r.Value = "Alpha" Then r.Value = "Bravo"
        Next
    End With
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I ran your code without "osht.activate" and it worked .

not sure what "If oCell Then" means
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top