Code works in immediate window, not from button click

nyconfidential

New Member
Joined
Jul 22, 2015
Messages
49
Office Version
  1. 365
  2. 2016
Hi all, I have a relative simple sub that I am running to change the name of a reference in a cell(we are replacing a worksheet, I'm looking for references to the old worksheet and replacing the name). It works perfectly when I run it from the immediate window, but if I call it from a button click event on a form, it does not work(no error, it just doesn't replace the old worksheet reference). Can anyone tell me what I am doing wrong? Thanks in advance.


Code:
Public Sub UpdateReferences(wkbCopyPath As String)
On Error GoTo errHandler
Application.StatusBar = True


Dim wkbCopy As String
Dim wrkSht As Worksheet
wkbCopy = Dir(wkbCopyPath)


For Each wrkSht In ActiveWorkbook.Worksheets
    Application.StatusBar = "Updating references on sheet " & wrkSht.Index & " of " & Sheets.Count
                
    'Replace references to workbook you are copying from in other worksheets
     Cells.Replace What:=shtName & "_OLD", Replacement:=shtName, _
        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
        False, ReplaceFormat:=False
            
Next wrkSht




CleanUpNamedRanges wkbCopy, shtName & "_OLD"


Exit_UpdateReferences:
    Application.StatusBar = False
    Exit Sub
    
errHandler:
    Call ErrorHandlerFunction("UpdateReferences")
    Resume Exit_UpdateReferences
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try
Code:
wrkSht.Cells.Replace What:=shtName & "_OLD", Replacement:=shtName, _
        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
        False, ReplaceFormat:=False
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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