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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
It dont understand the replacement text: HERE = "='C:\Users\Jones\Desktop\[test.xlsx]" :unsure:
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,884
Messages
6,122,082
Members
449,064
Latest member
MattDRT

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