Excel VBAmacros根据date统计特定单词出现的次数(将被转换为星期编号)

所以我有列A(包含单词)和列C(包含date)如下所示。 这些列偶尔会被新的标题分隔,例如“Word”和“Date”以及一个空格。

Word Date BV 12/06/2017 BV 12/06/2017 BV 13/06/2017 BV 13/06/2017 BR 17/07/2017 BR 17/07/2017 BR 24/07/2017 Word Date BT 30/07/2017 BT 30/07/2017 Word Date BY 05/08/2017 

首先将按星期数转换为新的D列,如12/06/2017至第24周。

使用类似于:

 Sub TimeConverter() Dim I as Long, MaxRow as Long MaxRow = Range("A" & Rows.count).End(xlUp).Row For I = 2 to MaxRow Cells(I, "D").Value = DatePart("ww", Cells(I, "C"), 7) Next End Sub 

然后,我希望VBAmacros代码查看A列,找出一个词出现的次数,并与同一周的数字中的date匹配到一个新的列B.

使用类似于:

 =COUNTIF(A:A, "BV") =COUNTIF(A:A, "BR") Output # 4 # 3 

现在将它们组合在一起,以便唯一字(A列)计数(B列)可以被分成相应的星期编号(D列)。

期望的输出:

 BV 4 24 BR 2 29 BR 1 30 BT 2 30 BY 1 31 

任何build议将是伟大的! 谢谢。

假设用你的VBA代码,你已经设法得到这样的input:

在这里输入图像说明

然后,正如评论中所提到的, 你需要实现一个字典来得到像这样的东西:

在这里输入图像说明

正如你所看到的,字典的关键字是+周数字。 因此BR29BR30不同。

复制示例input,运行下面的代码,您将得到所需的输出:

 Option Explicit Public Sub TestMe() Dim myDict As Object Dim lngCounter As Long Dim strKey As String Dim objKey As Object Set myDict = CreateObject("Scripting.Dictionary") For lngCounter = 1 To 14 strKey = Cells(lngCounter, 1) & Cells(lngCounter, 3) If myDict.exists(strKey) Then myDict(strKey) = myDict(strKey) + 1 Else myDict(strKey) = 1 End If Next lngCounter For lngCounter = 0 To myDict.Count - 1 Cells(lngCounter + 1, 6) = myDict.Items()(lngCounter) Cells(lngCounter + 1, 7) = myDict.keys()(lngCounter) Next lngCounter End Sub 

然后,你必须更多的工作,find一种方法来分割BV24到BV和24的密钥。你需要find一种方法来消除结果中的零。