使用Dim时无效的外部程序

时间:2015-01-09 15:33:30

标签: excel vba excel-vba

我得到"无效的外部程序"以下代码出错。有人可以告诉我我在哪里出错。

Dim Asset As String, AssetURL As String

Asset = Range("B1").Value

If Asset = "1" Then
    AssetURL = "X:\Docs\excel0001.xls"
Elseif Asset = "2" Then
    AssetURL = "X:\Docs\excel0002.xls"
End If

Range("C1").Value = AssetURL

1 个答案:

答案 0 :(得分:5)

确保您可以在多个Subs中运行代码的最佳方法是将其设为Public Sub

Public Sub qwerty()
    Dim Asset As String, AssetURL As String

    Asset = Range("B1").Value

    If Asset = "1" Then
        AssetURL = "X:\Docs\excel0001.xls"
    ElseIf Asset = "2" Then
        AssetURL = "X:\Docs\excel0002.xls"
    End If

    Range("C1").Value = AssetURL
End Sub

如果要在其他地方执行此代码,则应使用:

Sub OtherSub()
    'OtherSub Code
    Call qwerty() 'or just qwerty
    'Rest of OtherSub Code
End Sub

Related question on using Call to utilize a Sub within another Sub