Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 我需要数组、类、字典还是集合?_Excel_Vba - Fatal编程技术网

Excel 我需要数组、类、字典还是集合?

Excel 我需要数组、类、字典还是集合?,excel,vba,Excel,Vba,我不确定我要做的事情的最佳选择是什么。目前,我正在使用一个3D数组来保存这些值,但我刚刚在学习VBA中的字典、类和集合,无法确定其中是否有更好或更有用的内容 我每个月都会收到一份新的数据电子表格,我需要在单元格中循环查找一个数字,然后根据该数字替换另一个单元格的数据。即(全部在A列) 在B列中,我需要输入相应的国家。如果前两位数字是43,则右边的单元格应为“Germany”,然后在C列中为“DEU”。如果这两个数字是41,那么B列单元格应该是“USA”,C列单元格应该是“USA”…等等。等等 目

我不确定我要做的事情的最佳选择是什么。目前,我正在使用一个3D数组来保存这些值,但我刚刚在学习VBA中的字典、类和集合,无法确定其中是否有更好或更有用的内容

我每个月都会收到一份新的数据电子表格,我需要在单元格中循环查找一个数字,然后根据该数字替换另一个单元格的数据。即(全部在A列)

在B列中,我需要输入相应的国家。如果前两位数字是43,则右边的单元格应为“Germany”,然后在C列中为“DEU”。如果这两个数字是41,那么B列单元格应该是“USA”,C列单元格应该是“USA”…等等。等等

目前,我正在设置3D阵列(psuedo代码):

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

上课会更好吗?然后我可以做一些事情,比如创建一个cntry。代码,cntry.Country,cntry.CountryAbbrev,并将其用于表示“43”、“德国”和“德国”

(同样,psuedo代码):

。。。 至于字典,我认为这是行不通的,因为每个条目只能有一个键。所以我可以输入国家编号(“43”),但只能设置国家名称或国家缩写,但不能同时设置两者……对吗

这个问题有意义吗?在这样的事情上使用类/字典是不是太过分了?收藏会是最好的吗


谢谢你的建议/指导

您可以有一个对象字典或字典

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

  • 字典
  • 收藏
  • 数组(矩阵)变量
  • ActiveX组合框
  • ActiveX列表框
  • 用户窗体控件组合框
  • 用户窗体控件列表框
  • 分类表
  • 法官
我建议您阅读以下文章:


您可以拥有对象词典或词典

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

  • 字典
  • 收藏
  • 数组(矩阵)变量
  • ActiveX组合框
  • ActiveX列表框
  • 用户窗体控件组合框
  • 用户窗体控件列表框
  • 分类表
  • 法官
我建议您阅读以下文章:


您可以拥有对象词典或词典

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

  • 字典
  • 收藏
  • 数组(矩阵)变量
  • ActiveX组合框
  • ActiveX列表框
  • 用户窗体控件组合框
  • 用户窗体控件列表框
  • 分类表
  • 法官
我建议您阅读以下文章:


您可以拥有对象词典或词典

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

  • 字典
  • 收藏
  • 数组(矩阵)变量
  • ActiveX组合框
  • ActiveX列表框
  • 用户窗体控件组合框
  • 用户窗体控件列表框
  • 分类表
  • 法官
我建议您阅读以下文章:


课堂模块就是答案。答案总是这样。代码就是代码,在类模块中几乎没有什么事情是在标准模块中做不到的。类只是以不同方式组织代码的一种方法

但是下一个问题是如何在类模块中存储数据。我使用集合是出于习惯,但集合或脚本编写。字典是最好的选择

我会制作一个名为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只是一个集合。有关NewEnum属性的更多信息,请访问

然后我把我所有的国家资料放在一张桌子上,然后把那张桌子读给我的同学听。在中国

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
我需要一种方法来找到一个国家的一个属性

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

除了定义我循环使用的代码的位置,我真的不需要任何注释。您可以通过对象的名称以及属性或方法来判断发生了什么。这就是设置类模块的额外工作的回报——IMO。

类模块就是答案。答案总是这样。代码就是代码,在类模块中几乎没有什么事情是在标准模块中做不到的。类只是以不同方式组织代码的一种方法

但是下一个问题是如何在类模块中存储数据。我使用集合是出于习惯,但集合或脚本编写。字典是最好的选择

我会制作一个名为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只是一个集合。有关NewEnum属性的更多信息,请访问

然后我把我所有的国家资料放在一张桌子上,然后把那张桌子读给我的同学听。在中国

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
我需要一种方法来找到一个国家的一个属性

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

除了定义我循环使用的代码的位置,我真的不需要任何注释。您可以通过对象的名称以及属性或方法来判断发生了什么。这就是设置类模块的额外工作的回报——IMO。

类模块就是答案。答案总是这样。代码就是代码,在类模块中几乎没有什么事情是在标准模块中做不到的。类只是以不同方式组织代码的一种方法

但下一个问题是如何存储yo