VBA将班级成员打印到单元格

时间:2017-12-26 21:53:23

标签: excel vba excel-vba class

我有一个班级" empl"包含员工数据的名称,部门等,以及另一个类" empls"包含所有" empl"。我想打印" empls"的每个成员。在工作的不同细胞中。我创建了一个函数toString,用于排列我想要的每个员工的元素。在屏幕上打印时效果很好。

我正在尝试循环来完成工作,但似乎在每次互动中,整个列表都在" empls"是印刷而不是逐个元素。我还可以尝试其他什么?

我对编码很陌生,所以可能会犯明显的错误!

 Sub Emplman()
 Static intEmplNum As Integer
 Dim lrow As Integer
 Dim ws1 As Worksheet
 Set ws1 = Worksheets("Sheet1")
 Dim ws As Worksheet
 Set ws = Worksheets("Employee Status")
 ws.Range("A2").End(xlDown).Select
 lrow = ActiveCell.Row
 Dim empl As clsEmpl
Set empls = New clsEmpls
Dim i As Integer
For i = 1 To lrow
        Set empl = New clsEmpl
                empl.Number = ws.Range("A" & i + 1).Value
                empl.Firstname = ws.Range("B" & i + 1).Value
                empl.Surname = ws.Range("C" & i + 1).Value
                empl.Department = ws.Range("D" & i + 1).Value
                     With empl
                        intEmplNum = intEmplNum + 1
                        .ID = "E" & Format$(intEmplNum, "00000")
                    End With
        empls.Add empl
Next i

    Dim j As Integer
     For Each empl In empls.getAll("%&")
      For j = 1 To n
       ws1.Cells(1, j).Value = empl.toString
       Next
     Next 

" GETALL"是一个从集合中获取每个员工的功能。我创建它是为了按部门过滤员工,但在这种情况下它会让所有人。

On Cls Modules

Public Function getAll(sAll) As Collection
Dim empl As clsEmpl
  Set getAll = New Collection
    For Each empl In emplcoll
        If empl.Department <> sAll Then
            getAll.Add empl
        End If
    Next
End Function


Public Function toString() As Variant
toString = m_Firstname & " " & _
           m_Surname 
End Function

0 个答案:

没有答案