Arrays 使用VBA for Excel在数组或字典中创建、填充和删除条目

Arrays 使用VBA for Excel在数组或字典中创建、填充和删除条目,arrays,excel,vba,dictionary,populate,Arrays,Excel,Vba,Dictionary,Populate,我是VBA新手,但我花了大量时间搜索一些代码来帮助解决我的问题,如下所示: 为了简单起见,我将用一个例子来说明我试图实现的目标: 假设我有一堆具有唯一编号的大理石,我想将它们放入编号类别中 例如: At "Time '1.0'" "Marble '1'" is "Categorized" into "Group '1'" At "Time '2.0'" "Marble '2'" is "Categorized" into "Group '2'" etc. At "Time '3.0'"

我是VBA新手,但我花了大量时间搜索一些代码来帮助解决我的问题,如下所示:

为了简单起见,我将用一个例子来说明我试图实现的目标:

假设我有一堆具有唯一编号的大理石,我想将它们放入编号类别中

例如:

At "Time '1.0'" "Marble '1'" is "Categorized" into "Group '1'" At "Time '2.0'" "Marble '2'" is "Categorized" into "Group '2'" etc. At "Time '3.0'" "Marble '1'" is "Categorized" into "Group '3'" 在“时间'1.0'”中,“大理石'1'”被“分类”为“组'1'” 在“时间'2.0'”中,“大理石'2'”被“分类”为“组'2'”等。 在“时间'3.0'”中,“大理石'1'”被“分类”为“组'3'” 因此,在时间3.0时,大理石1被归类为第1组和第2组

我创建了一个按钮,允许我输入“大理石”和“时间”,并报告在给定时间大理石是否被分类,如果是,是哪个组,如果不是,最后一个组是什么

当大理石仅被分为一组时,这种方法效果良好,但当出现大理石被分为多组,然后从其中一组中移除的情况时,即

……在“时间'4.0'”时,“大理石'1'”与“组'1'”是“未分类的”

我的有限代码将删除任何分类记录,并简单地将最近的分类报告为“它所在的最后一个类别”,即

Marble#=1,Time=5.0 Result=“Uncategorized”Last Category=“Group'1'”,而实际上它仍然被“分类”为“Group'2'”

总之,我需要一个代码,允许我输入“大理石”和“时间”,并告诉我在那个特定时间“大理石”是否被“分类”,如果是,它被“分类”到哪个“组”,如果不是,那么它被“分类”到的最后一个“组”是什么

我已经把我的代码贴在下面了。请记住,这是我的第一个VBA代码,它已经通过谷歌搜索和试验和错误一起被弗兰肯斯坦*(注意单元格(15,8)和单元格(18,8)分别为“输入大理石”和“输入时间)*(第1、2、3、4列分别为大理石、分类、组和时间)**

私有子命令按钮2_单击() Dim分类为字符串,i分类为整数,组分类为整数 Count=Application.CountA(范围(“A:A”)) 对于i=1进行计数
如果单元格(i,1).Value=Cells(15,8)和Cells(i,4).Value,我想你应该看看字典中的'object'。Patrick Matthews的一篇很好的参考资料,我不想使用Count和Group作为变量名——它们都存在于VBA对象库的其他地方。我使用intCount或lngCount(用于变量类型Integer或Long)来保持清楚。您还需要将
Count=Application.CountA(范围(“A:A”)
修改为
Count=Application.WorksheetFunction.CountA(范围(“A:A”)
。代码是否在某个特定的位置中断?谢谢您的字典链接。我会调查一下,看看我是否能取得更多进展。我还将接受您的建议MattCrum并更改变量名以清理它。为了回答你的问题,我没有故意破译密码。我计划只输入两个条目(“大理石和时间”),然后按下命令按钮,让代码运行并显示结果。 Private Sub CommandButton2_Click() Dim Categorized As String, i As Integer, Group As Integer Count = Application.CountA(Range("A:A")) For i = 1 To Count If Cells (i, 1).Value = Cells(15, 8) And Cells (i, 4).Value <= Cells (18, 8) And Cells (i, 2) = "CATEGORIZE" Then Categorized = "CATEGORIZED" If Cells (i, 1).Value = Cells(15, 8) And Cells (i, 4).Value <= Cells (18, 8) And Cells (i, 2) = "UNCATEGORIZE" Then Categorized = "NOT CATEGORIZED" If Cells (i, 1).Value = Cells(15, 8) And Cells (i, 4).Value <= Cells (18, 8) And Cells (i, 2) = "CATEGORIZE" Then Group = Cells(i, 3) Next i If Categorized = "CATEGORIZED" Then MsgBox Categorized & " Categorized to " & Group If Categorized = "NOT CATEGORIZED" Then Msgbox Categorized & " Was Last Categorized to " & Group If Categorized = "" Then Msgbox "Marble Does Not Exist Prior to This Time" End Sub At "Time '1.0'" "Marble '1'" is "Categorized" into "Group '1'" At "Time '1.1'" "Marble '1'" is "Categorized" into "Group '1'" At "Time '1.4'" "Marble '1'" is "Categorized" into "Group '1'"