[VBA,VB.NET,C#,PHP]プログラムTips集

[VBA,VB.NET,C#,PHP]プログラムのちょっとしたテクニック

エクセルVBAの参照設定を動的に変更する方法

‘ 参照設定追加
Sub AddReferences()
On Error Resume Next
    Dim Ref
    With ActiveWorkbook.VBProject
                Call .References.AddFromFile("C:\work\common1.xlam")
                Call .References.AddFromFile("C:\work\common2.xlam")
    End With
End Sub

‘ 参照設定削除
Sub RemoveReferences()
On Error Resume Next
    Dim Ref
    With ActiveWorkbook.VBProject
        For Each Ref In ActiveWorkbook.VBProject.References
	‘ common1.xlamを参照していたら削除(チェックを外す)
            If ((InStr(Ref.FullPath, "common1.xlam") > 0 )) Then
                Call .References.Remove(Ref)
                GoTo C_FOR
            End If
	‘ common2.xlamを参照していたら削除(チェックを外す)
            If ((InStr(Ref.FullPath, "common2.xlam") > 0 )) Then
                Call .References.Remove(Ref)
                GoTo C_FOR
            End If
C_FOR:
        Next Ref
    End With
End Sub