vba to remove connections

tlindeman

Active Member
Joined
Jun 29, 2005
Messages
313
Could you please supply me with the code to remove connections to a web site? Here is my code, however it creates multiple connections and I would like to delete them after they get created. Thank You

Sub Income_statement()
Sheets("Income Statement").Select
For i = 1 To 11551 Step 231
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.google.com/finance?q=" & Range("A" & i).Value & "&fstype=ii", Destination:=Range("B" & i))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery = False
End With
Next i
End Sub
 
Right after the for statement at the top, try this line:

If Range("A"& i).Value = "" Then Exit For

Hope that helps.
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I call this as required
Code:
Sub connections()

    'TimeOff local timer control
Application.EnableEvents = False
Application.Calculation = xlManual
    On Error Resume Next
Application.ScreenUpdating = True
    Dim i
    For i = 1 To 10000
        ActiveWorkbook.connections("Connection" & i).Delete
        'Sheets("combination").Range("m11") = i so i can see whats happening
   Application.ScreenUpdating = True
   DoEvents
    Next i

    MsgBox "done"

Application.EnableEvents = True
Application.Calculation = xlAutomatic
    ActiveWorkbook.Save

    TimeOn local timer control

End Sub

for those really large collections of connections, but easily minimsed and adapted
 
Upvote 0
Code:
Sub CopyTExtFIle()

    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\Users\Tyger\Desktop\test2.txt", Destination:=Range("$A$1"))  'Change Connection to your file location and Desitnation to where you want to paste
        .Name = "test2"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

For Each cn In ThisWorkbook.Connections
cn.Delete
Next cn

End Sub


Try that , that should delete the connection afterward
 
Upvote 0
Is there a way to exclude recently refreshed connections from the delete all connections script. I have a sheet which has built up about 65000 connections and i need to keep about 100 of them.

Thanks.
Jason
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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