我可以使用字符串作为条件编译常量

时间:2012-08-03 07:41:40

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

是否可以在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

谢谢!

1 个答案:

答案 0 :(得分:2)

简答:是的

演示:

#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

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

运行子Demo_Error但无效,返回错误13,type mismatch