VBA:计数时间一对值出现
从中生成:
:
我有此代码:
Sub missing()
Dim ws, wsOut As Worksheet
Set ws = ActiveWorkbook.Sheets("Table1")
Set wsOut = ActiveWorkbook.Sheets("output")
lastRow = ws.Range("G" & Rows.Count).End(xlUp).Row
lastRowOut = wsOut.Range("G" & Rows.Count).End(xlUp).Row + 1
For i = 1 To lastRow
If (ws.Cells(i, 10).Value = "") _
And _
((ws.Cells(i, 7).Value = "Peking") Or _
(ws.Cells(i, 7).Value = "Tokio") Or _
(ws.Cells(i, 7).Value = "London")) _
And _
((ws.Cells(i, 8).Value = "A") Or _
(ws.Cells(i, 8).Value = "B") Or _
(ws.Cells(i, 8).Value = "C")) _
Then
wsOut.Range("B" & lastRowOut & ":C" & lastRowOut).Value = ws.Range("G" & i & ":H" & i).Value
wsOut.Range("A" & lastRowOut).Value = i
lastRowOut = lastRowOut + 1
End If
Next i
End Sub
我尝试实现一个代码以生成一个代码,此外此输出以红色标记:
因此,我正在尝试计算并列出每对发生的一对。我试图将“ Countifs”实现到IF-Stategent中,但失败了。实际的表有超过40个条目在“城市”,“部门”中有10多个条目,总共有6.000多个条目。如果有人可以帮助我这样做,那会很高兴。预先感谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如您在评论中提到的那样,您似乎不知道如何录制宏:您转到“开发人员”功能区(您可能需要先启用它),在第一部分中,您单击“记录宏”,而您只是开始执行要记录的内容(在这种情况下,插入一个枢轴表)。
我刚刚这样做,使用范围“ A1:B8”作为输入并创建您要寻找的枢轴表的类型,并且以下代码会自动创建(当心它是自动添加的代码,它具有很多多余的线,参数,...:可以使用它作为起点,但是尝试通过修改,删除,...部分来学习它非常有用):
顺便说一句:正如您可以从命名“ Pivottable3”,我从第一次尝试中没有成功:-)
As you mention in your comment, you seem not to know how to record a macro: you go to the "Developer" ribbon (you might need to enable it first) and in the first part, you click "Record macro" and you just start doing what you want to record (in this case, insert a pivot table).
I've just done that, using a range "A1:B8" as input and creating the kind of pivot table you are looking for, and the following code gets created automatically (beware that this is automatically added code, which has a lot of superfluous lines, parameters, ...: it's ok to use it as a starting point but it's very useful trying to learn from it by modifying, deleting, ... parts of it):
By the way: as you can see from the name "PivotTable3", I did not succeed from my first attempt :-)
计数列对(字典)
Count Column Pairs (Dictionary)
不需要VBA。
e2
IS= sort(unique($ b $ 2:$ c $ 18),{1,2},{1,1})
公式
g2
IS= countifs($ b $ 2:$ b $ 18,index(e2#,, 1),$ c $ 2:$ c $ 18,index(e2#,2) )
vba解决方案:
结果测试过程将保持您的值,也可以在工作表上使用
= sort(countcities(b2:c18),{1,2},{1,1})
结果是我的第一个解决方案。一个过程中的所有内容:
新代码不使用
unique
函数:No VBA required.
Formula in
E2
is=SORT(UNIQUE($B$2:$C$18),{1,2},{1,1})
Formula in
G2
is=COUNTIFS($B$2:$B$18,INDEX(E2#,,1),$C$2:$C$18,INDEX(E2#,,2))
VBA Solution:
Result in the test procedure will hold your values, or can be used on a worksheet as
=SORT(CountCities(B2:C18),{1,2},{1,1})
to get same result as my first solution.Everything in one procedure:
New code not using
Unique
function: