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

我试图消除在VBA中定义函数原型的代码重复,这种方法似乎并不奏效。 基本上除了32位窗口以外的大多数平台,我们有相同的确切原型,但只有DLL / dylib名称更改。 所以我试图find一种方法来让VBA做我想做的事情,但它一直抱怨库名称不是一个string常量(除非我硬编码)。 有什么办法让VBA了解我想要做什么?

我试过的其他东西:编译常量#Const libName = "CoolProp_xls_x64.dll"而不是const libName ...在每个块中,但它说libName被多次定义(除非它不是如果它侦听预处理器旗

 #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 

这是不可能的。 VBA没有传统意义上的预处理器,尽pipe出现了。 #Const声明只能在#If语句中使用,不能被replace成实际的代码,而Declare语句中的Lib必须是一个文字(不仅仅是一个常量,一个文字 )。 此外,正如你已经注意到的那样,# #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