View Full Version : VBA to Change Connection String of all query's in a workbook
richardSmith
05-22-2013, 07:39 AM
Just like the title states what would be VBA to change a connection string of all the query's in a workbook?
That depends on whether the strings are in code or cells.
richardSmith
05-22-2013, 11:12 AM
I am going to go with cells. They are access query's that are imported into the spreadsheet.
Select a Cell with a connection string to be replaced. Press Ctrl+H, then click the Advanced button.
Jan Karel Pieterse
05-23-2013, 01:28 AM
SOmething like this:
Sub ChangeAllConnections(sNewConn As String)
Dim oLo As ListObject
Dim oSh As Worksheet
Dim oQt As QueryTable
For Each oSh In Worksheets
'connections can be in a listobject (table)
For Each oLo In oSh.ListObjects
oLo.QueryTable.Connection = sNewConn
Next
'but also outside a listobject (querues created in Excel 2003 or before; web queries)
For Each oQt In oSh.QueryTables
oQt.Connection = sNewConn
Next
Next
End Sub
or ?
Sub M_snb()
For Each cn In ThisWorkbook.Connections
cn.OLEDBConnection.Connection = Replace(cn.OLEDBConnection.Connection, "G:\OF\", "G:\")
Next
End Sub
Aflatoon
05-23-2013, 03:42 AM
That assumes they are all OLEDB and not ODBC, of course. ;)
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.