VBA 类型不匹配(条件编译)

VBA Type Mismatch (Conditional Compiling)

我的 Dim 语句不起作用:有一条错误消息:

编译错误: 类型不匹配

我正在有条件地编译一个 VBA 宏,它使用大量代数、大量数字和大量文件,因此除非我调整数据类型,否则执行时间将很长。最好根据用户计算机的速度和可用时间来允许用户执行此操作。

代码如下:

' Develop fake data to at glance recognize whether program works.
' Source http://www.cpearson.com/excel/optimize.htm
Option Explicit

Sub Function1(ByVal VarType As String)

    Dim mVers As String
    Dim userChoice As Variant

'   Give the user macro options based on how fast or slow the computer is using advanced conditional compliling
    userChoice = MsgBox("This macro by default treats all numbers as doubles for maximum precision. If you are running this macro on an old computer, you may want to relare numbers as singles, to speed up the macro." & vbNewLine & "You can also use integers for a quick estimate of data results.")
    userChoice = VarType

    #If VarType = "Long" Or "long" Then
        Dim RangeNOut As Long, vRangeNIn As Long, Ind6 As Long, Ind4 As Long, Ind5 As Long
        Dim Step2 As Long, MRow As Long, ColIn As Long, Ind3 As Long, Mcol As Long
        Dim MxRNo As Long, BgrSum As Long, RowIn As Long, Ind As Long, M40eff As Long, Step As Long
        Dim ColNo As Long, Startcol As Long, Startrow As Long, MeanComp As Long
        Dim PlateNo As Long, MonoVal As Long, Ind1 As Long, EntryRow2 As Long, EntryRow As Long
        Dim Ind2 As Long, BgrValP As Long, BgrRow As Long, M40eff As Long
        Dim BrgSum As Long, BgrVal As Long, RangeNIn As Long, RangeNOut As Long, TLCorn As Long
        Dim Volcorr As Long, BRCorn As Long, MEeff As Long, MediaVal As Long

    #If VarType = "Double" Or "double" Then
        Dim RangeNOut As Double, vRangeNIn As Double, Ind6 As Double, Ind4 As Double, Ind5 As Double
        Dim Step2 As Double, MRow As Double, ColIn As Double, Ind3 As Double, Mcol As Double
        Dim MxRNo As Double, BgrSum As Double, RowIn As Double, Ind As Double, M40eff As Double, Step As Double
        Dim ColNo As Double, Startcol As Double, Startrow As Double, MeanComp As Double
        Dim PlateNo As Double, MonoVal As Double, Ind1 As Double, EntryRow2 As Double, EntryRow As Double
        Dim Ind2 As Double, BgrValP As Double, BgrRow As Double, M40eff As Double
        Dim BrgSum As Double, BgrVal As Double, RangeNIn As Double, RangeNOut As Double, TLCorn As Double
        Dim Volcorr As Double, BRCorn As Double, MEeff As Double, MediaVal As Double

    #ElseIf VarType = "Single" Or "single" Then
        Dim RangeNOut As Single, vRangeNIn As Single, ecInd6 As Single, Ind4 As Single, Ind5 As Single
        Step2 As Single, MRow As Single, ColIn As Single, Ind3 As Single, Mcol As Single
        Dim MxRNo As Single, BgrSum As Single, RowIn As Single, Ind As Single, M40eff As Single, Step As Single
        Dim ColNo As Single, Startcol As Single, Startrow As Single, MeanComp As Single
        Dim PlateNo As Single, MonoVal As Single, Ind1 As Single, EntryRow2 As Single, EntryRow As Single
        Dim Ind2 As Single, BgrValP As Single, BgrRow As Single, M40eff As Single
        Dim BrgSum As Single, BgrVal As Single, RangeNIn As Single, RangeNOut As Single, TLCorn As Single
        Dim Volcorr As Single, BRCorn As Single, MEeff As Single, MediaVal As Single

    #ElseIf VarType = "Integer" Or "integer" Then
        Dim RangeNOut As Integer, vRangeNIn As Integer, ecInd6 As Integer, Ind4 As Integer, Ind5 As Integer
        Dim Step2 As Integer, MRow As Integer, ColIn As Integer, Ind3 As Integer, Mcol As Integer
        Dim MxRNo As Integer, BgrSum As Integer, RowIn As Integer, Ind As Integer, M40eff As Integer
        Dim Step As Integer, ColNo As Integer, Startcol As Integer, Startrow As Integer, MeanComp As Integer
        Dim PlateNo As Integer, MonoVal As Integer, Ind1 As Integer, EntryRow2 As Integer, EntryRow As Integer
        Dim Ind2 As Integer, BgrValP As Integer, BgrRow As Integer, M40eff As Integer
        Dim BrgSum As Integer, BgrVal As Integer, RangeNIn As Integer, RangeNOut As Integer, TLCorn As Integer
        Dim Volcorr As Integer, BRCorn As Integer, MEeff As Integer, MediaVal As Integer

    #Else
        MsgBox "VarType " & VarType & " is not valid. Check spelling."
    #End If

导致类型不匹配错误消息的行:

#If VarType = "Long" Or "long" Then

类型不匹配是编译器期望 Const - 它与您检查的值无关。变量 VarType 不是常量,因此此代码目前无法按结构运行。根本没有办法 select 如何在运行时声明变量 - 这就是 Variants 的用途。如果你需要做这个预编译,你需要声明一个常量,将常量更改为你想要编译的值,然后重新编译。例如:

Option Explicit

#Const VarType = "Long"

Sub Function1()

    #If VarType = "Long" Then
        Dim RangeNOut As Long, vRangeNIn As Long, Ind6 As Long, Ind4 As Long, Ind5 As Long
        Dim Step2 As Long, MRow As Long, ColIn As Long, Ind3 As Long, Mcol As Long
        Dim MxRNo As Long, BgrSum As Long, RowIn As Long, Ind As Long, M40eff As Long, Step As Long
        Dim ColNo As Long, Startcol As Long, Startrow As Long, MeanComp As Long
        Dim PlateNo As Long, MonoVal As Long, Ind1 As Long, EntryRow2 As Long, EntryRow As Long
        Dim Ind2 As Long, BgrValP As Long, BgrRow As Long, M40eff As Long
        Dim BrgSum As Long, BgrVal As Long, RangeNIn As Long, RangeNOut As Long, TLCorn As Long
        Dim Volcorr As Long, BRCorn As Long, MEeff As Long, MediaVal As Long
    #ElseIf VarType = "Double" Or "double" Then
        '....
        '....
        '....
        '....
    #End If

如果您需要能够在运行时执行此操作,则必须为每种类型编写一个不同的函数,并且 select 在它们之间使用标准 IfSelect语法,而不是预编译器切换。