VBA Find and Replace in another workbook

Suavis

New Member
Joined
Jun 1, 2014
Messages
17
Hi All,

I have several user who use capture forms to record their daily productivity. The productivity data is recorded in a central workbook on a share drive named OPDB.xlsx. One of the fields automatically records the user name as per their log-in credentials. For some reason the system user name format is not consistent. In some cases it records the user's staff ID number (i.e. A123456), and in other cases their names (i.e. Smith, John J).

Another workbook named Productivity.xlsm, saved on the same share drive, contains pivot tables and graphs reflecting team and individual productivity stats. The Productivity Stats however use the User Name, which makes for very confusing stats.

In an attempt to standardise the User Name Field, I wrote the following Macro and saved it to my Personal Macros, which works perfectly when I run the code.

Code:
Sub Replace_User_Name()

Dim BackupDB as Workbook
Set BackupDB = Workbooks.Open("Z:\Forms\Capture Forms\OPDB.xlsx")
With BackupDB
Worksheets("Outcome Data").Activate

Cells.Replace What:="a096506", Replacement:="John Smith",  _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

.Save
.Close
End With

End Sub

If I however add a Command Button to Productivity.xlsm (using activeX controls), and I run the exact same code in Productivity.xlsm workbook, it does not work.

No errors or debug is produced. It opens OPDB.xlsx, runs the code, saves and closes, but the code does not actually change / replace the data.

If I run the identical code in my personal macros, it works every time.

Can anyone offer some advise or insight?

Thanks
Johan
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Give this a try. This is a guess.

Code:
[color=darkblue]Sub[/color] Replace_User_Name()
    
    [color=darkblue]Dim[/color] BackupDB [color=darkblue]As[/color] Workbook
    [color=darkblue]Set[/color] BackupDB = Workbooks.Open("Z:\Forms\Capture Forms\OPDB.xlsx")
    [color=darkblue]With[/color] BackupDB
        [COLOR="#FF0000"].Worksheets("Outcome Data").[/COLOR]Cells.Replace What:="a096506", Replacement:="John Smith", _
                                                  LookAt:=xlPart, SearchOrder:=xlByRows, _
                                                  MatchCase:=False, SearchFormat:=[color=darkblue]False[/color], _
                                                  ReplaceFormat:=False
        .Save
        .Close
    [color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

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