在VBA中相当于IFDEF

我有需要在Excel 2003和Excel 2007上运行的代码,并且有几个版本中的更改会导致代码停止。 我尝试用If-Else语句分隔这些行,但是代码不能编译,因为它不能识别用于其他的代码。 有没有什么办法可以告诉一个版本忽略VBA中类似于C或C ++风格的#ifdef的一段代码?

这是一个很好的起点,但是它不能与运行的Excel版本一起工作,因为只能在运行时计算出来,而不是编译时间。

如果您需要根据仅在运行时可发现的信息来分支代码,则可以考虑将后期绑定作为解决scheme。 有两种方法可以潜入版本问题。

如果您需要访问仅存在于特定版本中的属性或方法,则可以使用第一种方法,您可以使用CallByName。 按名称调用的优点是,它允许您尽可能地保留对象的早期绑定(和智能感知)。

举个例子,Excel 2007有一个新的TintAndShade属性。 如果您想更改范围的颜色,并且Excel 2007也确保TintAndShade设置为0,则会遇到麻烦,因为您的代码在Excel 2003中无法编译,而Excel 2003中没有将TintAndShade作为范围对象的属性。 如果您使用CallByName访问您知道的并非所有版本的属性,则您的代码将在所有版本中都可以正常编译,但只能在您指定的版本中运行。 见下文:

Sub Test() ColorRange Selection, Excel.Application.version, 6 End Sub Sub ColorRange(rng As Excel.Range, version As Double, ParamArray args() As Variant) With rng.Interior .colorIndex = 6 .Pattern = xlSolid If version >= 12# Then 'Because the property name is stored in a string this will still compile. 'And it will only get called if the correct version is in use. CallByName rng.Interior, "TintAndShade", VbLet, 0 End If End With End Sub 

第二种方法是需要通过“新build”实例化的类,甚至不存在于旧版本中。 你不会遇到Excel的这个问题,但我会给一个quickie演示,所以你可以看到我的意思:

想象一下,你想要做文件IO,出于一些奇怪的原因,并不是所有的计算机上都有Microsoft脚本运行时。 但是,对于一些同样离奇的原因,你想确保它在任何时候都可以使用。 如果设置了引用并在代码中使用了早期绑定,则代码将不会在没有该文件的系统上编译。 所以你使用后期绑定,而不是:

 Public Sub test() Dim strMyString As String Dim strMyPath As String strMyPath = "C:\Test\Junk.txt" strMyString = "Foo" If LenB(Dir("C:\Windows\System32\scrrun.dll")) Then WriteString strMyPath, strMyString Else WriteStringNative strMyPath, strMyString End If End Sub Public Sub WriteString(ByVal path As String, ByVal value As String) Dim fso As Object '<-Use generic object 'This is late binding: Set fso = CreateObject("Scripting.FileSystemObject") fso.CreateTextFile(path, True, False).Write value End Sub Public Sub WriteStringNative(ByVal path As String, ByVal value As String) Dim lngFileNum As Long lngFileNum = FreeFile If LenB(Dir(path)) Then Kill path Open path For Binary Access Write Lock Read Write As #lngFileNum Put #lngFileNum, , value Close #lngFileNum End Sub 

有一个自2003年以来的Excel对象模型的所有添加和更改的完整列表:
http://msdn.microsoft.com/en-us/library/bb149069.aspx对于1997年和2000年之间的变化去这里&#xFF1A;
http://msdn.microsoft.com/en-us/library/aa140068(office.10).aspx

是的,可以在Excel VBA中进行条件编译。 下面是一个简短的资源和一些示例代码: 条件编译

 #If Win32 Then ' Profile String functions: Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long #Else ' Profile String functions: Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Integer Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer #End If 

你能发表违规的代码行吗?

如果它是一个像vbYes或xlFileFormat或其他常量,使用相应的数值。

告诉我你得到了什么,我会看看我是否可以重构它。

法案