假设数据库名称没有改变,而只是服务器,下面的内容会为你工作。未测试
Sub ChangeAllConnections()
Dim q As QueryTable, w As Worksheet, oldserverName As String, newServerName As String
Dim con As Variant
oldserverName = "onlsrvr" 'Change here
newServerName = "newsrvr" ' change here
For Each w In ThisWorkbook.Worksheets
For Each q In w.QueryTables
q.Connection = Replace(q.Connection, oldserverName, newServerName)
Next
Next
On Error Resume Next
For Each con In ThisWorkbook.Connections
con.ODBCConnection.Connection = Replace(con.ODBCConnection.Connection, oldserverName, newServerName)
con.OLEDBConnection.Connection = Replace(con.OLEDBConnection.Connection, oldserverName, newServerName)
Next
On Error GoTo 0
End Sub