使用VBA for Excel中的常量避免重复函数原型

时间:2016-10-23 00:14:08

标签: excel vba excel-vba

我试图在VBA中定义函数原型时消除代码重复,而且这种方法似乎没有用。基本上对于除32位窗口之外的大多数平台,我们都有相同的原型,但只有DLL / dylib名称会发生​​变化。所以我试图找到一种方法让VBA做我想做的事情,但它一直抱怨库名不是字符串常量(除非我硬编码)。有没有办法让VBA了解我想要做什么?

我尝试过的其他事情:每个块中的编译器常量#Const libName = "CoolProp_xls_x64.dll"而不是const libName ...,但后来它说libName是多重定义的(除非它听不到它是否为&t;预处理器标志

#If Win64 Then
    Const libName As String = "CoolProp_xls_x64.dll"
#ElseIf Win32 Then
    Const libName As String = ""
#ElseIf Mac Then
    #If MAC_OFFICE_VERSION >= 15 Then
        #If VBA7 Then ' 64-bit Excel 2016 for Mac
            Const libName As String = "libCoolProp.dylib"
        #Else ' 32-bit Excel 2016 for Mac
            Const libName As String = "libCoolProp_32bit.dll"
        #End If
    #Else ' 32-bit Excel 2011 for Mac
        Const libName As String = "libCoolProp_32bit.dll"
    #End If
#End If

#Const theLibName = libName

#If Mac Or Win64 Then
Private Declare PtrSafe Function get_global_param_string_private Lib theLibName Alias "get_global_param_string" (ByVal param As String, ByVal Output As String, ByVal n As Integer) As Long
Private Declare PtrSafe Function get_fluid_param_string_private Lib theLibName Alias "get_fluid_param_string" (ByVal fluid As String, ByVal param As String, ByVal Output As String, ByVal n As Integer) As Long
Private Declare PtrSafe Function PropsSI_private Lib theLibName Alias "PropsSI" (ByVal Output As String, ByVal Name1 As String, ByVal Value1 As Double, ByVal Name2 As String, ByVal Value2 As Double, ByVal Ref As String) As Double
Private Declare PtrSafe Function PhaseSI_private Lib theLibName Alias "PhaseSI" (ByVal Name1 As String, ByVal Value1 As Double, ByVal Name2 As String, ByVal Value2 As Double, ByVal Ref As String, ByVal Output As String, ByVal n As Integer) As Long
Private Declare PtrSafe Function Props1SI_private Lib theLibName Alias "Props1SI" (ByVal Output As String, ByVal Ref As String) As Double
Private Declare PtrSafe Function HAPropsSI_private Lib theLibName Alias "HAPropsSI" (ByVal Output As String, ByVal Input1Name As String, ByVal Value1 As Double, ByVal Input2Name As String, ByVal Value2 As Double, ByVal Input3name As String, ByVal Value3 As Double) As Double
'DEPRECATED
Private Declare PtrSafe Function Props_private Lib theLibName Alias "PropsS" (ByVal Output As String, ByVal Name1 As Long, ByVal Value1 As Double, ByVal Name2 As Long, ByVal Value2 As Double, ByVal Ref As String) As Double
#Else 'Win32
Private Declare PtrSafe Function get_global_param_string_private Lib "CoolProp_stdcall.dll" Alias "_get_global_param_string@12" (ByVal param As String, ByVal Output As String, ByVal n As Integer) As Long
Private Declare PtrSafe Function get_fluid_param_string_private Lib "CoolProp_stdcall.dll" Alias "_get_fluid_param_string@16" (ByVal param As String, ByVal param As String, ByVal Output As String, ByVal n As Integer) As Long
Private Declare PtrSafe Function PropsSI_private Lib "CoolProp_stdcall.dll" Alias "_PropsSI@32" (ByVal Output As String, ByVal Name1 As String, ByVal Value1 As Double, ByVal Name2 As String, ByVal Value2 As Double, ByVal Ref As String) As Double
Private Declare PtrSafe Function PhaseSI_private Lib "CoolProp_stdcall.dll" Alias "_PhaseSI@36" (ByVal Name1 As String, ByVal Value1 As Double, ByVal Name2 As String, ByVal Value2 As Double, ByVal Ref As String, ByVal Output As String, ByVal n As Integer) As Long
Private Declare PtrSafe Function Props1SI_private Lib "CoolProp_stdcall.dll" Alias "_Props1SI@8" (ByVal Output As String, ByVal Ref As String) As Double
Private Declare PtrSafe Function HAPropsSI_private Lib "CoolProp_stdcall.dll" Alias "_HAPropsSI@40" (ByVal Output As String, ByVal Input1Name As String, ByVal Value1 As Double, ByVal Input2Name As String, ByVal Value2 As Double, ByVal Input3name As String, ByVal Value3 As Double) As Double
'DEPRECATED
Private Declare PtrSafe Function Props_private Lib "CoolProp_stdcall.dll" Alias "_PropsS@32" (ByVal Output As String, ByVal Name1 As String, ByVal Value1 As Double, ByVal Name2 As String, ByVal Value2 As Double, ByVal Ref As String) As Double
#End If

1 个答案:

答案 0 :(得分:1)

这是不可能的。尽管有外观,但VBA没有传统意义上的预处理器。 #Const声明只能在#If语句中使用,不能替换为实际代码,而Lib语句中的Declare必须是文字(不仅仅是常量,一个文字)。此外,正如您所注意到的,#Const声明本身不受条件编译的约束,因此您甚至无法对条件进行分组以使事情变得更容易。脚本编写不好玩吗?

据我所知,你需要四个街区。写入块的条件而不是相反:

#If Win64 Then
    Private Declare PtrSafe Function get_global_param_string_private Lib "CoolProp_xls_x64.dll" ...
#ElseIf MAC_OFFICE_VERSION >= 15 And VBA7 Then  
    '64-bit Excel 2016 for Mac
    Private Declare PtrSafe Function get_global_param_string_private Lib "libCoolProp.dylib" Alias "get_global_param_string" ...
#ElseIf Mac Then
    '32-Bit Excel for Mac
    Private Declare PtrSafe Function get_global_param_string_private Lib "libCoolProp_32bit.dll" Alias "get_global_param_string" ...
#ElseIf Win32 Then
    Private Declare PtrSafe Function get_global_param_string_private Lib "CoolProp_stdcall.dll" Alias "_get_global_param_string@12" ...
#EndIf