Jason Samuels
Jason Samuels

Reputation: 971

Connect to another data source

I have an excel spreadsheet (call it SS-A) connected to a data source which is another excel spreadsheet (call it SS-B). I also have another spreadsheet (call it SS-C) identical to SS-B. If there is an error in the connection to SS-B, I want SS-A then to connect to SS-C.

Thus I would like something like this,

If SS-B Connection Error Then
  connect to SS-C
ELSE
  connect to SS-B
End If

I am not sure how to implement something like this using Excel VBA, because the result, whether the connection is SS-B or SS-C must be placed in the same sheet in SS-A.

Upvotes: 0

Views: 140

Answers (1)

Raj
Raj

Reputation: 674

@Jason I think you can use "On error statement" for your scenario.

Public sub Connection()
On Error GoTo ErrorHandler
' Insert your connection code to SS-B
Exit Sub
ErrorHandler:
' Insert connection code to SS-c
Resume Next 
End Sub

If your connection code to SS-B throws any error, it will try to connect to SS-C.

For generating code to connect to another data source, please check following link VBA Code to connect to external data source. Hope it might help you.

Thanks,
Raj

Upvotes: 1

Related Questions