我需要一个数组,类,字典还是集合?

我不知道什么是我想要做的最好的select。 目前,我正在使用一个3D数组来保存这些值,但是我现在正在学习VBA中的字典,类和集合,并且无法确定这些值是否会对我所尝试的更好或更有用去做。

我每个月都会得到一个新的电子表格数据,我需要循环查找一个数字的单元格,并根据这个数字来replace另一个单元格的数据。 IE(全部在A栏)

4323 4233 4123 4343 4356 3213 

在B栏中,我需要放一个相应的国家。 如果前两位数字是43,则右边的单元格应该是“德国”,然后是“col”。 C,“DEU”。 如果这两个数字是41,那么col。 B细胞应该是“美国”,C细胞应该是“美国”…等等。 等等

目前,我正在build立一个3D数组(伪码):

 myArray(0,0) = 43 myArray(0,1) = "Germany" myArray(0,2) = "DEU" myArray(1,0) = 41 myArray(1,1) = "United States" myArray(1,2) = "USA" etc. etc. 

然后,我有一个循环遍历所有单元格并replace信息。

一堂课可能会更好吗? 然后我可以做一些事情,比如创build一个cntry。 Code,cntry.Country,cntry.CountryAbbrev并用它们来指代“43”,“Germany”和“DEU”

(再次,伪代码):

 num = left("A1",2) 'then here, somehow find the num in cntry.Code list - will need to work out how Cells("B1").Value = cntry.Country Cells("C1").Value = cntry.CountryAbbrev 

至于字典,我认为这是行不通的,因为(AFAIK)每个条目只能有一个关键字。 所以我可以做国家号码(“43”),但只设置国家名称或国家缩写 – 但不是两个都是正确的?

这个问题有意义吗? 在类似的东西上使用类/字典矫枉过正? 一个集合是最好的?

感谢您的任何build议/指导!

class级模块是答案。 这总是答案。 代码是代码,在类模块中几乎没有任何东西可以在标准模块中完成。 类只是一种组织代码的方式。

但接下来的问题就是如何将数据存储在类模块中。 我习惯使用集合,但Collection或Scripting.Dictionary是您的最佳select。

我会做一个叫CCountry的类,看起来像这样

 Private mlCountryID As Long Private msCode As String Private msFullname As String Private msAbbreviation As String Public Property Let CountryID(ByVal lCountryID As Long): mlCountryID = lCountryID: End Property Public Property Get CountryID() As Long: CountryID = mlCountryID: End Property Public Property Let Code(ByVal sCode As String): msCode = sCode: End Property Public Property Get Code() As String: Code = msCode: End Property Public Property Let Fullname(ByVal sFullname As String): msFullname = sFullname: End Property Public Property Get Fullname() As String: Fullname = msFullname: End Property Public Property Let Abbreviation(ByVal sAbbreviation As String): msAbbreviation = sAbbreviation: End Property Public Property Get Abbreviation() As String: Abbreviation = msAbbreviation: End Property 

然后我会做一个名为CCountries的类来保存我所有的CCountry实例

 Private mcolCountries As Collection Private Sub Class_Initialize() Set mcolCountries = New Collection End Sub Private Sub Class_Terminate() Set mcolCountries = Nothing End Sub Public Property Get NewEnum() As IUnknown Set NewEnum = mcolCountries.[_NewEnum] End Property Public Sub Add(clsCountry As CCountry) If clsCountry.CountryID = 0 Then clsCountry.CountryID = Me.Count + 1 End If mcolCountries.Add clsCountry, CStr(clsCountry.CountryID) End Sub Public Property Get Country(vItem As Variant) As CCountry Set Country = mcolCountries.Item(vItem) End Property Public Property Get Count() As Long Count = mcolCountries.Count End Property 

您看到CCountries在这一点上仅仅是一个集合。 您可以在http://dailydoseofexcel.com/archives/2010/07/09/creating-a-parent-class/上阅读有关NewEnum属性的更多信息

然后,我把所有的国家的东西放在一张桌子,并把我的class级读到桌子上。 在CC国家

 Public Sub FillFromRange(rRng As Range) Dim vaValues As Variant Dim i As Long Dim clsCountry As CCountry vaValues = rRng.Value For i = LBound(vaValues, 1) To UBound(vaValues, 1) Set clsCountry = New CCountry With clsCountry .Code = vaValues(i, 1) .Fullname = vaValues(i, 2) .Abbreviation = vaValues(i, 3) End With Me.Add clsCountry Next i End Sub 

我需要一种方法来find一个国家的一个属性

 Public Property Get CountryBy(ByVal sProperty As String, ByVal vValue As Variant) As CCountry Dim clsReturn As CCountry Dim clsCountry As CCountry For Each clsCountry In Me If CallByName(clsCountry, sProperty, VbGet) = vValue Then Set clsReturn = clsCountry Exit For End If Next clsCountry Set CountryBy = clsReturn End Property 

然后我把我的数字清单放在旁边

 Sub FillCodes() Dim clsCountries As CCountries Dim rCell As Range Dim clsCountry As CCountry Set clsCountries = New CCountries clsCountries.FillFromRange Sheet1.ListObjects("tblCountries").DataBodyRange For Each rCell In Sheet2.Range("A3:A5").Cells Set clsCountry = Nothing Set clsCountry = clsCountries.CountryBy("Code", CStr(rCell.Value)) If Not clsCountry Is Nothing Then rCell.Offset(0, 1).Value = clsCountry.Fullname rCell.Offset(0, 2).Value = clsCountry.Abbreviation End If Next rCell End Sub 

除了定义我正在循环的代码的位置之外,我并不需要任何评论。 你可以告诉我的对象名称和属性或方法是怎么回事。 这是build立类模块 – 国际海事组织的额外工作的回报。

你可以有一个对象或词典的字典。

VBA有几种方法来存储数据:

  • 一本字典
  • 一个集合
  • 一个数组(matrix)variables
  • 一个ActiveXcombobox
  • 一个ActiveX列表框
  • 一个用户窗体控件combobox
  • 一个用户窗体控件列表框
  • 一个sorting列表
  • 一个arrays列表

我build议你阅读下面的文章:

http://www.snb-vba.eu/VBA_Dictionary_en.html