Macro to delete all exernal data connections

hyperdreamz

New Member
Joined
Sep 30, 2004
Messages
11
Hi All

I am trying to write a macro to delete all connections from a very large excel file

I created this file by using a Macro to extract data from a external data source (url's listed on a sheet)

The issue is the 1000+ connections on this file make it next to impossible to operate

I cannot recreate the file

Need some help with a macro to delete all connections in all sheets in a given work book.

Sample :http://www.4shared.com/file/95922031/b7611f8f/Book1.html


Macro used :


Sub Dump()

Dim myURL

Sheets("Sheet1").Select
ActiveCell.Offset(1, 0).Select

myURL = Worksheets("Sheet1").Range(ActiveCell.Address).Text

Sheets("Sheet2").Select
Range("A65536").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select

With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & myURL, _
Destination:=Range(ActiveCell.Address))
.BackgroundQuery = False
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.PreserveFormatting = False
.SaveData = True

.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
'.WebTables = "9"
.WebPreFormattedTextToColumns = False
.WebConsecutiveDelimitersAsOne = False
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
'.Refresh BackgroundQuery:=False

End With

Dim nName As Name
For Each nName In Names
If InStr(1, nName.RefersTo, "#REF!") > 0 Then
nName.Delete
End If
Next nName

End Sub


URL's on sheet1:

http://archive.corewebprogramming.com/Chapter2/Simple-Table.html
http://archive.corewebprogramming.com/Chapter2/Simple-Table.html
http://archive.corewebprogramming.com/Chapter2/Simple-Table.html
http://archive.corewebprogramming.com/Chapter2/Simple-Table.html
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

smiths87

Board Regular
Joined
Aug 7, 2006
Messages
106
Maybe this?

Sub deletedconns()

For i = 1 To ActiveWorkbook.Connections.Count
If ActiveWorkbook.Connections.Count = 0 Then Exit Sub
ActiveWorkbook.Connections.Item(i).Delete
i = i - 1
Next i

End Sub
 

hyperdreamz

New Member
Joined
Sep 30, 2004
Messages
11
Hey smiths87

Thanks for the input. Your solution worked. But a friend reccomended this (PFB) approach that seemed to run faster.


Dim Sh As Worksheet, xNazwa As Object
Dim xConect As Object


For Each xConect In ActiveWorkbook.Connections
If UCase(xConect.Name) Like "*" Then xConect.Delete
Next xConect

For Each Sh In ActiveWorkbook.Worksheets
For Each xNazwa In Sh.Names
xNazwa.Delete
Next xNazwa
Next Sh
 

gad79

New Member
Joined
Dec 16, 2012
Messages
3
I use this one.

sub delete_Connections()
do while activeworkbook.connections.count > 0
activeworkbook.connections.item(activeworkbook.connections.count).delete
loop
end sub
 

Mr Boris

New Member
Joined
Feb 25, 2015
Messages
10
I use this one.

sub delete_Connections()
do while activeworkbook.connections.count > 0
activeworkbook.connections.item(activeworkbook.connections.count).delete
loop
end sub

A long time ago I know, but just what I was looking for! :biggrin:
Cheers
B
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,954
Messages
5,834,567
Members
430,297
Latest member
xa_gta

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