Workbook reference, search and replace not working?

Razzy

Board Regular
Joined
Jul 24, 2020
Messages
106
Office Version
  1. 365
Platform
  1. Windows
I got this as a string: "='C:\Users\Jones\Desktop\[test.xlsx]"

And want to replace "REPLACE_" in a workbook.

Like: "REPLACE_Sheet1'!$D$15

This will output a workbook reference: "='C:\Users\Jones\Desktop\[test.xlsx]Sheet1'!$D$15"

Questian: I cant get the search and replace function to work.

VBA Code:
Dim HERE as string

HERE = "='C:\Users\Jones\Desktop\[test.xlsx]"

Workbooks("template.xlsm").Activate
Sheets("Sheet1").Select
    Cells.Select
    Selection.Replace what:="'REPLACE_'", Replacement:=HERE, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2


Why is it not working you think?

The code works with just text replacement:

VBA Code:
Dim HERE as string

HERE = "='C:\Users\Jones\Desktop\[test.xlsx]"

Workbooks("template.xlsm").Activate
Sheets("Sheet1").Select
    Cells.Select
    Selection.Replace what:="'REPLACE_'", Replacement:="JUST TEXT", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2

Later on I will build the replace part in a loop.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Razzy

Board Regular
Joined
Jul 24, 2020
Messages
106
Office Version
  1. 365
Platform
  1. Windows
It dont understand the replacement text: HERE = "='C:\Users\Jones\Desktop\[test.xlsx]" :unsure:
 

Razzy

Board Regular
Joined
Jul 24, 2020
Messages
106
Office Version
  1. 365
Platform
  1. Windows
It works now fine, the fault was: It has to find a match in the sheet, and find the worksheet at the given path, otherweis it will just stop.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,491
Messages
5,636,633
Members
416,932
Latest member
mm07

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
Top