我试图在类模块中使用布尔值但是代码在“MsgBox LOADPROPS.DUPLICATES”中失败,并且“公共属性获取DUPLICATES()作为范围”请有人协助吗?
Sub INITIALIZE_CLASS()
Dim LOADPROPS As cLoadData
Set LOADPROPS = New cLoadData
LOADPROPS.DUPLICATES = PasteLoadingForm.Columns("K")
MsgBox LOADPROPS.DUPLICATES
End Sub
并在课程模块中
Public Property Get DUPLICATES() As Range
DUPLICATES = pDUPLICATES
End Property
Public Property Let DUPLICATES(Value As Range)
Dim lcount As Long
lcount = Application.WorksheetFunction.CountIf(Value, "DUPLICATE")
'
pDUPLICATES = lcount
Select Case pDUPLICATES
Case Is = 0
pDUPLICATES = False
Case Is >= 0
pDUPLICATES = True
Case Else
MsgBox "Error"
End Select
End Property
答案 0 :(得分:1)
据我所知,你混淆了DUPLICATES的数据类型。你在get函数中将它声明为一个范围,但是pDUPLICATES可以采用boolean和long的值(最初是count,然后在select语句中转换为true或false)。
快速解决方法是将它们更改为变体,因此您的类模块变为:
Private pDUPLICATES As Variant
Public Property Get DUPLICATES() As Variant
DUPLICATES = pDUPLICATES
End Property
Public Property Let DUPLICATES(Value As Variant)
Dim lcount As Long
lcount = Application.WorksheetFunction.CountIf(Value, "DUPLICATE")
'
pDUPLICATES = lcount
Select Case pDUPLICATES
Case Is = 0
pDUPLICATES = False
Case Is >= 0
pDUPLICATES = True
Case Else
MsgBox "Error"
End Select
End Property
修改强>
为避免变体,更好的方法可能是重构您的类模块,因此您实际上并未使用get / set属性(您在此处并未真正这样做)。您可以使用以下函数执行此操作:
类cLoadDataV2
Option Explicit
Public pDUPLICATES As Boolean
Public Function fDuplicates(rngInputRange As Range)
Dim lcount As Long
lcount = Application.WorksheetFunction.CountIf(rngInputRange, "DUPLICATE")
Select Case lcount
Case Is = 0
pDUPLICATES = False
Case Is >= 0
pDUPLICATES = True
Case Else
MsgBox "Error"
End Select
End Function
常规模块
Sub Initialise_v2()
Dim LOADPROPS As cLoadDataV2
Set LOADPROPS = New cLoadDataV2
LOADPROPS.fDuplicates (Sheet1.Columns("K"))
MsgBox LOADPROPS.pDUPLICATES
End Sub