Forum Discussion

messengineer's avatar
messengineer
Copper Contributor
Nov 08, 2024

Dynamically changing ODC Connection

I have connected Excel to a Power BI dataset using ODC (Office Data Connection). I am pulling data into 19 different Excel tables, so I have 19 different ODC files all configured for the different c...
  • messengineer's avatar
    messengineer
    Nov 09, 2024

    This is where I ended up .... with two named cells having the two different Power BI Dataset IDs

    Option Explicit
    
    Public Sub SwapProdTest()
    
        Call SwapConnections("PROD", "TEST")
    
    End Sub
    
    Public Sub SwapTestProd()
    
        Call SwapConnections("TEST", "PROD")
    
    End Sub
    
    
    Private Sub SwapConnections(str_From As String, str_To As String)
    
        Dim str_Old, str_New, str_Connection, str_Response As String
        Dim cn As WorkbookConnection
        Dim oledbCn As OLEDBConnection
      
        For Each cn In ActiveWorkbook.Connections
        
          Set oledbCn = cn.OLEDBConnection
          str_Old = oledbCn.Connection
          str_New = Replace(str_Old, shtSettings.Range("ptr_ID_" & str_From), shtSettings.Range("ptr_ID_" & str_To))
          str_Connection = str_Connection & Chr(13) & cn.Name
            Debug.Print cn.Name
            Debug.Print str_Old
            Debug.Print str_New
        
          oledbCn.Connection = str_New
          
        Next cn
    
        str_Response = MsgBox("For Connections :" & str_Connection & Chr(13) & Chr(13) & "Activate 'Refresh All' when ready.", vbOKOnly, "Swapped " & str_From & " to " & str_To)
    
    End Sub
    

     

Resources