在excel vba中重置variables

我正在试图用VBA为我做一些工作。 假设我有4个variables – 单位,租约开始date,租约条款和替代租约条款还有更多,但这对我的问题无关紧要。 VBA循环遍历每一行,并获得单元i在各列中的值。

因此,从逻辑上讲,我可以将单位声明为string,将开始date作为date,将租约作为单个租约,将租约作为单个租约。 我有的问题是,我需要区分空的条目和0.默认的数值将是0. 0和空白之间的区别是至关重要的。 我发现解决这个问题的唯一方法是声明一切为Variant,然后检查相应的范围是否为空。 如果它是空的,则租用Variant默认值(空),否则分配值。

我有一种感觉,这将严重影响我的代码性能。 最终,会有很多variables,我想在代码中引用这些variables。 就像, if isempty(AltLease) = true then做一件事,否则别的东西。

我也发现,我不能清空单个或datevariables(date实际上不是一个问题,因为它下降到1900)。 任何人都可以提出build议

这里是代码:

 Dim tUnitName As Variant Dim tNumberOfUnits As Variant Dim tLeaseCurLeaseLengthDef as Variant Dim tLeaseCurLeaseLengthAlt as Variant Sub tenancyScheduleNew() Dim lRow As Long Dim i As Long lRow = Sheet2.Cells(Rows.Count, 2).End(xlUp).Row For i = 3 To lRow reAssignVariables i Next i End Sub Sub reAssignVariables(i As Long) tAssetName = checkIfEmpty(i, getColumn("Sheet4", "tAssetName", 3)) tNumberOfUnits = checkIfEmpty(i, getColumn("Sheet4", "tNumberOfUnits", 3)) tLeaseCurLeaseLengthDef = checkIfEmpty(i, getColumn("Sheet4", "tLeaseCurLeaseLengthDef", 3)) tLeaseCurLeaseLengthDef = checkIfEmpty(i, getColumn("Sheet4", "tLeaseCurLeaseLengthAlt", 3)) End Sub Function getColumn(sh As String, wh As String, colNo As Long) As Long Dim refSheet As Worksheet Dim rFound As Range Set refSheet = Sheets(sh) With refSheet Set rFound = .Columns(1).Find(What:=wh, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:=xlWhole) On Error GoTo 0 If Not rFound Is Nothing Then getColumn = rFound.Offset(0, colNo - 1).Value Else End If End With End Function 

现在我正在这样做,我认为这会降低性能。 这只是我所做的一小部分变数 – 会有更多的变数。 我只需要了解如何正确构build它。 更具体地说,如果tLeaseCurLeaseLengthAlt有一个值,那么代码应该使用该值,或者使用默认值。

您不能清空整型variables,因为空不是整数。 如果你有一个当前是整数子types的variablesvariables,你可以将它重置为空:

 Sub test() Dim v As Variant Debug.Print TypeName(v) v = 1 Debug.Print TypeName(v) v = Empty Debug.Print TypeName(v) End Sub 

输出:

 Empty Integer Empty 

此外,使用变体的性能可能不如您担心的那么好。 非正式testing:

 Sub InformalTest(n As Long) Dim i As Long, sum1 As Double Dim j As Variant, sum2 As Variant Dim start As Double, elapsed1 As Double, elapsed2 As Double start = Timer For i = 1 To n sum1 = sum1 + 1# Next i elapsed1 = Timer - start start = Timer For j = 1 To n sum2 = sum2 + 1# Next j elapsed2 = Timer - start Debug.Print "Nonvariant time: " & elapsed1 & ", Nonvariant sum: " & sum1 Debug.Print "Variant time: " & elapsed2 & ", Variant sum: " & sum2 End Sub 

示例输出:

 InformalTest 1000000 Nonvariant time: 0.060546875, Nonvariant sum: 1000000 Variant time: 0.099609375, Variant sum: 1000000 InformalTest 10000000 Nonvariant time: 0.521484375, Nonvariant sum: 10000000 Variant time: 0.599609375, Variant sum: 10000000 

也许你可以创build自己的类? 示例为Single

class级Single2

 Private m_value As Single Private m_hasValue As Boolean Public Property Let Initialize(ByVal source As Range) m_hasValue = False m_value = 0 If source Is Nothing Then _ Exit Property ' add any checks you need to recognize the source cell as non-empty ' ... to distinguish between empty entry and 0 If Trim(source.Value) = "" Then _ Exit Property If Not IsNumeric(source.Value) Then _ Exit Property m_value = CSng(source.Value) m_hasValue = True End Property Public Property Get Value() As Single Value = m_value End Property Public Property Get HasValue() As Boolean HasValue = m_hasValue End Property 

并使用这样的类:

模块:

 Dim lease As Single2 Set lease = New Single2 lease.Initialize = Range("a1") If lease.HasValue Then Debug.Print "lease has value ... " & lease.Value Else Debug.Print "lease hasn't value ... " End If