gpt4 book ai didi

vba - 使用集合作为类中的属性(参数不可选)

转载 作者:行者123 更新时间:2023-12-03 00:44:39 24 4
gpt4 key购买 nike

我正在尝试使用日期集合作为类属性,但遇到了问题。本质上,我想在 emp_vacDates 中存储多个 vacLocation,并将其馈送到 employee.Vaca 中。

我使用 Employee.Name 没有问题

我得到的错误是

Compile error: Argument not optional

Vaca Let 语句

我的代码是:

Sub Vacation()
Dim i As Integer
Dim rowNum As Long: rowNum = 5
Dim colNum As Long: colNum = 4
Dim onePersonLoop As Range
Dim nameLocation As Range
Dim vacLocation As Range
Dim emp_vacDates As Collection
Dim emps As Collection
Dim employee As EmployeeClass

For i = 2 To 2
With ActiveWorkbook.Worksheets(i)

Set onePersonLoop = .Cells(rowNum, colNum).Offset(-1).End(xlToRight).Offset(, -2)
Set emps = New Collection
For rowNum = 5 To 5

Set nameLocation = .Cells(rowNum, colNum).Offset(-1, -1).EntireRow.Cells(1, 2)
Set employee = New EmployeeClass
employee.Name = nameLocation.Value
Set emp_vacDates = New Collection

For colNum = 4 To 6

Set vacLocation = .Cells(rowNum, colNum).Offset(-3)
If .Cells(rowNum, colNum) = "v" Or .Cells(rowNum, colNum) = "V" Then
emp_vacDates.Add vacLocation.Value
End If
Next colNum

employee.Vaca = emp_vacDates
emps.Add employee

Debug.Print employee.Name
Debug.Print employee.Vaca

Next rowNum

End With
Next i
End Sub

我的 EmployeeClass 类模块设置如下

Option Explicit

Dim vName As String
Dim vVaca As Collection

Public Property Get Name() As String
Name = vName
End Property

Public Property Let Name(nme As String)
vName = nme
End Property

Public Property Get Vaca() As Collection
Vaca = vVaca
End Property

Public Property Let Vaca(vcl As Collection)
vVaca = vcl
End Property

最佳答案

集合应该是Set。像这样:

Public Property Get Vaca() As Collection
Set Vaca = vVaca
End Property

Public Property Let Vaca(vcl As Collection)
Set vVaca = vcl
End Property

此外,您不能像这样 debug.print 集合:

调试.打印员工.Vaca

尝试在那里做其他事情,例如:

Debug.Print employee.Vaca.Item(1)

或者编写一个函数,检查集合中是否有项目,如果没有则打印 0 。像这样的事情:

Public Function VacaToString(Optional index = 1) As String

If Vaca.Count = 0 Then
VacaToString = 0
Else
VacaToString = Vaca(index)
End If

End Function

然后你可以像这样调试.打印它:

Debug.Print employee.VacaToString

关于vba - 使用集合作为类中的属性(参数不可选),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48341145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com