External references...convert to values

theta

Well-known Member
Joined
Jun 9, 2009
Messages
960
Hi. I have macro that copies a sheet ("Section 1") to a new workbook.

Only problem is that it contains vlookups etc to other sheets in the original workbook.

So I need a macro that will go through and change each external reference (linking back to the old sheet) as values

But still have other formulas on the same sheet (=sum etc) that can remain as formulas...just need to external ones to be made values

?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Something like this...

Code:
Sub convert_to_values()
Application.ScreenUpdating = False

Dim r As Range

    For Each r In Cells
        If InStr(1, r.Formula, ".xls", 1) > 0 Then r.Formula = r.Value
    Next r
Application.ScreenUpdating = True
End Sub

If you know how many rows/columns are affected, this could be amended to run a lot faster
 
Upvote 0
Could I not put UsedRange in there to speed it up?

Also, using excel 2007, so .xls might not work?

Checking r.Formula

What about checking if Left( is "=" and the cell contains "[" as this would indicate an external ref ?
 
Upvote 0
You think this would work easier ?.

Code:
    For Each r In ws.UsedRange
        cFormula = r.Formula
        If Len(cFormula) > 0 Then
            If Left$(cFormula, 1) = "=" Then
                If InStr(cFormula, "[") > 1 Then
 
Upvote 0
Yes - UsedRange will be more efficient than Cells

I don't see the value in testing the length of the cell or the first character. You can test whether a cell contains a formula using
Code:
If r.HasFormula = True Then

If you're using Excel 2007 or 2010 then yes, test for "[" rather than "'.xls"
 
Upvote 0
Ah....small problem. Some of my formulas do not show that they link to another sheet as I used named ranges, so you do not get the workbook reference, only the named range

(Although the workbook ref =[....] is contained within the named range definition)

Thoughts?
 
Upvote 0
Ah....small problem. Some of my formulas do not show that they link to another sheet as I used named ranges, so you do not get the workbook reference, only the named range

(Although the workbook ref =[....] is contained within the named range definition)

Thoughts?

Code:
For Each r In Cells
        If InStr(1, r.Formula, "[", 1) > 0 Then r.Formula = r.Value
    Next r
 
Upvote 0
So will this cover both standard formula =[...] etc, and those containing named ranges

Or should there be a combination of both with and OR statement?

For Each r In ws.UsedRange
If r.HasFormula = True Then
sFormula = r.Formula
If Len(sFormula) > 0 Then
If Left$(sFormula, 1) = "=" Then
If InStr(sFormula, "[") > 1 OR If InStr(1, sFormula, "[",1) >0 Then
 
Upvote 0
Why over-complicate things?
Did you try the code in my last post?
I thought you wanted to leave formulae intact and just remove references to other files.
I also don't understand why you would test the length of the formula, or use a separate variable (sFormula), and surely if the length of the formula is >0, then the first character MUST be "="
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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