Erase values linked to external files

gubertu

Board Regular
Joined
May 24, 2015
Messages
147
Hi all,

I wonder if there is a code for doing the following:

I have a WB called WB1 with Sheet1 with links to another WB, called WB2, Sheet2.

Is there a possibility to delete all the values that came from this external file (WB2)? I mean erase all of them quickly.

Thanks in advance!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
This solution assumes that references to other file are not created using INDIRECT function
- test on a copy of your workbook first

VBA opens WB1 and searches cells in Sheet1 for strings containing [WB2.xlsx]Sheet2
Cells containing that string are added to range Clr (which inititally is set to the last cell in column A to make coding simpler)
Cells in range Clr are cleared in one hit

To test
- place VBA below in a module in a new workbook
- amend the 3 constant values to match your own
- run the macro

For testing purposes (to allow you to see if only the VBA is finding the correct cells)
- line added to colour the cells RED
- cell contents are not cleared
- WB1 is not closed

After successful test
- remove the line colouring cells red and remove the leading apostrophe on the other 2 lines

Option Explicit

Code:
Sub ClearValues()
    Const lookFor = [COLOR=#ff0000][/COLOR]"[COLOR=#ff0000][WB2.xlsx]Sheet2[/COLOR]"                          'string to find that identifies required link
    Const Wb1 = "[COLOR=#006400]C:\folder\subfolder\WB1.xlsm[/COLOR]"                  'full path and name of WB1
    Const Sh1 = "[COLOR=#008080]Sheet1[/COLOR]"                                        'name of sheet in WB1
   
    Dim Srch As Range, Cel As Range, Clr As Range, Addr As String, Wb As Workbook
    Set Wb = Workbooks.Open(Wb1)
    Set Srch = Wb.Sheets(Sh1).Cells
    Set Clr = Srch(Srch.Rows.Count, 1)
    
    Set Cel = Srch.Find(lookFor)
    If Cel Is Nothing Then
        MsgBox "Not found"
        Exit Sub
    End If
    Addr = Cel.Address
    Do
        Set Clr = Union(Clr, Cel)
        Set Cel = Srch.FindNext(Cel)
    Loop While Addr <> Cel.Address
    Clr.Interior.Color = [COLOR=#ff0000]vbRed[/COLOR]                                  [COLOR=#006400]'delete after testing[/COLOR]
    [COLOR=#ff0000]'[/COLOR]Clr.ClearContents                                         [COLOR=#006400] 'remove leading apostrophe after testing[/COLOR]
    [COLOR=#ff0000]'[/COLOR]Wb.Close False                                             [COLOR=#006400]'remove leading apostrophe after testing[/COLOR]
End Sub
 
Upvote 0
Hi and thanks for the reply!

I´m going to try this code, but in the meantime, I would like to know if it is possible to delete all the values that came from any external link.

Thanks!


This solution assumes that references to other file are not created using INDIRECT function
- test on a copy of your workbook first

VBA opens WB1 and searches cells in Sheet1 for strings containing [WB2.xlsx]Sheet2
Cells containing that string are added to range Clr (which inititally is set to the last cell in column A to make coding simpler)
Cells in range Clr are cleared in one hit

To test
- place VBA below in a module in a new workbook
- amend the 3 constant values to match your own
- run the macro

For testing purposes (to allow you to see if only the VBA is finding the correct cells)
- line added to colour the cells RED
- cell contents are not cleared
- WB1 is not closed

After successful test
- remove the line colouring cells red and remove the leading apostrophe on the other 2 lines

Option Explicit

Code:
Sub ClearValues()
    Const lookFor = "[COLOR=#ff0000][WB2.xlsx]Sheet2[/COLOR]"                          'string to find that identifies required link
    Const Wb1 = "[COLOR=#006400]C:\folder\subfolder\WB1.xlsm[/COLOR]"                  'full path and name of WB1
    Const Sh1 = "[COLOR=#008080]Sheet1[/COLOR]"                                        'name of sheet in WB1
   
    Dim Srch As Range, Cel As Range, Clr As Range, Addr As String, Wb As Workbook
    Set Wb = Workbooks.Open(Wb1)
    Set Srch = Wb.Sheets(Sh1).Cells
    Set Clr = Srch(Srch.Rows.Count, 1)
    
    Set Cel = Srch.Find(lookFor)
    If Cel Is Nothing Then
        MsgBox "Not found"
        Exit Sub
    End If
    Addr = Cel.Address
    Do
        Set Clr = Union(Clr, Cel)
        Set Cel = Srch.FindNext(Cel)
    Loop While Addr <> Cel.Address
    Clr.Interior.Color = [COLOR=#ff0000]vbRed[/COLOR]                                  [COLOR=#006400]'delete after testing[/COLOR]
    [COLOR=#ff0000]'[/COLOR]Clr.ClearContents                                         [COLOR=#006400] 'remove leading apostrophe after testing[/COLOR]
    [COLOR=#ff0000]'[/COLOR]Wb.Close False                                             [COLOR=#006400]'remove leading apostrophe after testing[/COLOR]
End Sub
 
Upvote 0
I would like to know if it is possible to delete all the values that came from any external link

Assuming links are to Excel files and are NOT created using INDIRECT function

Try replacing ...
Code:
Const lookFor = "[WB2.xlsx]Sheet2"
with ...
Code:
Const lookFor = "[COLOR=#ff0000][*.xl*][/COLOR]"
 
Upvote 0
Hello,

sorry for the late answering.

Finally with the first code it was enough for me.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,202
Members
448,554
Latest member
Gleisner2

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