Erase values linked to external files

gubertu

Board Regular
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!
 

Yongle

Well-known Member
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
 

gubertu

Board Regular
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
 

Yongle

Well-known Member
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]"
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top