【发布时间】:2018-04-05 07:03:44
【问题描述】:
我正在尝试创建一个带有 for 循环的模块,以计算 3 个组合列的唯一值,这些列包含付款日、月份和年份的状态。所述日期是分开的,因为它们中的每一个都指的是一个月的一个周期,并且所有内容都按照这个确切的顺序按年、月和日排序。就像下面这个简化的小例子一样。
STATUS: DAY : MONTH : YEAR
PAID: 1 : 7 : 2016
OPEN: 1 : 7 : 2016
PAID: 1 : 7 : 2016
OPEN: 5 : 7 : 2016
PAID: 5 : 7 : 2016
OPEN: 5 : 7 : 2016
PAID: 10 : 7 : 2016
OPEN: 10 : 7 : 2016
PAID: 10 : 7 : 2016
PAID: 15 : 7 : 2016
PAID: 15 : 7 : 2016
OPEN: 15 : 7 : 2016
我试图做的是将所有 3 个单元格与列的下一个单元格进行比较,如果它们在所有 3 种情况下都相等,我将简单地计算它以查看我在该日期有多少唯一值并将其保存在一张单独的纸。如果在任何情况下都不同,它只需将日期添加到第二张表并从那里开始计数。为了方便起见,下面的代码进行了简化,因为我正在处理的宏太大而无法在此处发布。 编辑:如果需要,我可以在某处上传完整的代码,我只是翻译一些变量和 cmets。
j = 3 '' variable referencing the next line after i
k = 1 '' variable referencing the lines of the second sheet.
For i = 2 To lastrow ''variable to count how many rows the first sheet has
j = j + 1 ''variable to check the very next line after i
If w1.Range("A" & i).Value = "PAID" Then
If w1.Range("H" & i).Value = w1.Range("H" & j) And w1.Range("G" & i).Value = w1.Range("G" & j) And w1.Range("F" & i).Value = w1.Range("F" & j) Then ''if statement to check if all 3 cells are equal to the next 3 cells
w2.Range("D" & K).Value = w2.Range("D" & K).Value + 1 '' Sum 1 to the total number of dates with equal parameters on the 3 cells
Else '' writes the new date in the second sheet
K = K + 1
w2.Range("A" & K).Value = w1.Range("H" & i).Value
w2.Range("B" & K).Value = w1.Range("G" & i).Value
w2.Range("C" & K).Value = w1.Range("F" & i).Value
w2.Range("D" & K).Value = 1
End If
End If
Next i
我得到的通常是第一个日期,所有内容都计入新工作表的一行,第二行的最后一行数据。
我也尝试使用字典和/或集合,但我并没有完全理解它们的概念,即使在我在堆栈溢出和互联网上找到的一些示例中也是如此。
我如何使这个循环工作,或者有什么更好的方法来做到这一点?
【问题讨论】: