【问题标题】:Can I use a string as conditional compiling Constant我可以使用字符串作为条件编译常量吗
【发布时间】:2012-08-03 07:41:40
【问题描述】:

是否可以在 VBA 的条件编译中使用字符串常量?

例如:

#Const This_File_Concept="Chancleta"
'
#If This_File_Concept="Chancleta" then
     ''...Something happens

#End If
'    
#If This_File_Concept="Auto" then
     ''...Something different happens

#End If
'    
#If This_File_Concept="Freesbee" then
     ''...Another thing happens

#End If

谢谢!

【问题讨论】:

    标签: excel vba ms-access outlook ms-word


    【解决方案1】:

    简短回答:是的

    演示:

    #Const This_File_Concept = "Chancleta"
    
    #If This_File_Concept = "Chancleta" Then
         Dim zx As Long
    #End If
    '
    #If This_File_Concept = "Auto" Then
         Dim zx As String
    #End If
    '
    
    Sub Demo_OK()
        #If This_File_Concept = "Chancleta" Then
             zx = 1
        #End If
        '
        #If This_File_Concept = "Auto" Then
            zx = "Hello"
        #End If
    End Sub
    
    Sub Demo_Error()
        #If This_File_Concept = "Chancleta" Then
             zx = "Hello"
        #End If
        '
        #If This_File_Concept = "Auto" Then
            zx = 1
        #End If
    End Sub
    

    运行 Sub Demo_OK 工作正常,没有错误。

    运行 Sub Demo_Error 不工作,返回错误 13,type mismatch

    【讨论】:

    • 哈哈哈哈,我会咬耳朵!! :-) 我发誓我在发布之前尝试了自己的示例,但没有成功。我一定是打错了什么。谢谢克里斯
    猜你喜欢
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多