将Excel表放入自定义类

时间:2017-01-04 08:29:02

标签: excel vba excel-vba class

我是使用VBA类的新手,我认为我想要做的事情 - 将它们与ListObjects一起使用 - 更像是一个"中间体"而不是"初学者"技术

我们说我有两张与汽车相关的表格。

tblCarDesc
ID   MAKE        MODEL      DOORS     ENGINE
1    Chevrolet   Corvette    2         V8
2    Ford        Escort      4         V6
3    Rolls-Royce SilverCloud 4         V8

tblCarProd
ID      COUNTRY  TYPE
1       US       Sport
2       US       Economy
3       UK       Luxury

(两张桌子上的车辆相同,并以身份证号码显示。)

我希望有一个名为objCars的类,它包含两个表中的字段(列)。这样,当提到汽车#3时,objCars.Make将是"罗尔斯·罗伊斯"和objCars.Type将是" Luxury"。

1)有没有办法将两个表导入objCars?

也许我会创建一个足以容纳所有列的数组,然后将两个表加载到其中。我读过的教程说我会创建一个Collection,循环遍历数组的每一行,创建一个objCars的新实例,并为每一行分配objCars.Make,objCars.Model等。然后,Collection的每个项目都包含一辆汽车。 (或类似的东西。我也不太了解收藏品。)如果那是对的,这是最好的方式吗?

2)一个人如何引用特定的汽车?我读过的例子就像循环收集并处理其中的每个项目,但如果我想提取特定项目怎么办?我知道福特是汽车#2;如何获取该特定ID号的objCars.Make和objCars.Model?

2 个答案:

答案 0 :(得分:2)

我会有两节课。一辆汽车的班级clsCar和汽车收藏班级clsCars

这些类中的每一个都可能具有setter和getter方法,并且如果需要也可以具有自定义方法。特别是clsCars应该有一套getBy... - 方法来按照标准从集合中获取汽车或汽车集合。

示例:

clsCar

Private pID As Long
Private pMAKE As String
Private pMODEL As String
Private pDOORS As Integer
Private pENGINE As String
Private pCOUNTRY As String
Private pTYPE As String

Public Property Get ID() As Long
    ID = pID
End Property
Public Property Let ID(Value As Long)
    pID = Value
End Property

Public Property Get MAKE() As String
    MAKE = pMAKE
End Property
Public Property Let MAKE(Value As String)
    pMAKE = Value
End Property

Public Property Get MODEL() As String
    MODEL = pMODEL
End Property
Public Property Let MODEL(Value As String)
    pMODEL = Value
End Property

Public Property Get DOORS() As Integer
    DOORS = pDOORS
End Property
Public Property Let DOORS(Value As Integer)
    pDOORS = Value
End Property

Public Property Get ENGINE() As String
    ENGINE = pENGINE
End Property
Public Property Let ENGINE(Value As String)
    pENGINE = Value
End Property

Public Property Get COUNTRY() As String
    COUNTRY = pCOUNTRY
End Property
Public Property Let COUNTRY(Value As String)
    pCOUNTRY = Value
End Property

Public Property Get CarTYPE() As String
    CarTYPE = pTYPE
End Property
Public Property Let CarTYPE(Value As String)
    pTYPE = Value
End Property

Public Function toString() As String
 toString = pID & "; " & _
            pMAKE & "; " & _
            pMODEL & "; " & _
            pDOORS & "; " & _
            pENGINE & "; " & _
            pCOUNTRY & "; " & _
            pTYPE
End Function

clsCars

Private pCars As collection

Private Sub Class_Initialize()
 Set pCars = New collection
End Sub

Public Sub add(oCar As clsCar)
 pCars.add oCar
End Sub

Public Function getByIndex(lIndex As Long) As clsCar
 Set getByIndex = pCars.Item(lIndex)
End Function

Public Function getByID(lID As Long) As clsCar
 Dim oCar As clsCar
 For Each oCar In pCars
  If oCar.ID = lID Then
   Set getByID = oCar
  End If
 Next
End Function

Public Function getByEngine(sEngine As String) As collection
 Dim oCar As clsCar
 Set getByEngine = New collection
 For Each oCar In pCars
  If oCar.ENGINE = sEngine Then
   getByEngine.add oCar
  End If
 Next
End Function

default Module

Public oCars As clsCars

Sub initialize()
 Dim oCar As clsCar
 Dim oListObject As ListObject
 Dim oListRow As ListRow
 Dim oCells As Range

 Set oCars = New clsCars

 Set oListObject = Worksheets("Sheet1").ListObjects("tblCarDesc")
 For Each oListRow In oListObject.ListRows
  Set oCells = oListRow.Range.Cells
  Set oCar = New clsCar
  oCar.ID = oCells(, 1).Value
  oCar.MAKE = oCells(, 2).Value
  oCar.MODEL = oCells(, 3).Value
  oCar.DOORS = oCells(, 4).Value
  oCar.ENGINE = oCells(, 5).Value
  oCars.add oCar
 Next

 Set oListObject = Worksheets("Sheet1").ListObjects("tblCarProd")
 Dim lID As Long
 For Each oListRow In oListObject.ListRows
  Set oCells = oListRow.Range.Cells
  lID = oCells(, 1).Value
  Set oCar = oCars.getByID(lID)
  If Not oCar Is Nothing Then
   oCar.COUNTRY = oCells(, 2).Value
   oCar.CarTYPE = oCells(, 3).Value
  End If
 Next

 MsgBox oCars.getByIndex(2).toString

 For Each oCar In oCars.getByEngine("V8")
  MsgBox oCar.toString
 Next

End Sub

答案 1 :(得分:-1)

我会为每个使用一个类,并且每个都使用一个数组,因此arrCars包含clsCars,而arrProd包含clsProduction。然后我会在填充时使用每个数组的索引,所以arrCars(1)= Corvette和arrProd(1)= US Sport然后从每个你可以参考其他,所以如果x = 3,汽车(x)和prod(x)是正确的。或者首先在excel中使用vlookup,并制作一个较大的表,只需要1个ID,如果这是它们相关的方式,但Bentley也可以是3.它不太清楚你的意思是什么桌子,是否有每辆车的入口,或者就像使用某种选择进​​一步捕捉汽车一样。另一个想法是在汽车类中使用ProductionID,然后使用类的静态生产“表”来引用。