简体   繁体   English

使用VBA将Excel中的两列组合成“虚拟助手列”

[英]Combining two columns in Excel into a “virtual helper column” using VBA

I have two columns that I combine into a third helper column by concatening them. 我有两个列,我通过连接它们组合成第三个辅助列。

One is a 4-digit column, the other is single digit. 一个是4位数的列,另一个是单个数字。

For instance: 例如:

COLUMNA    COLUMNB  COLUMNC
1234       1        12341
1234       1        12341
1234       2        12342
2345       1        23451
2345       2        23452
2345       2        23452

I then run an array formula in a 4th column (using an identifier that is in each row) that indicates how many unique values there are for column C for each identifier. 然后,我在第4列中运行数组公式(使用每行中的标识符),指示每个标识符的列C有多少唯一值。

I would like to circumvent the use of the helper column if possible -- building a range (in numerical format) in VBA that I can reference in code so I can do away with the helper column. 如果可能的话,我想绕过辅助列的使用 - 在VBA中构建一个范围(以数字格式),我可以在代码中引用,这样我就可以取消帮助列了。

Hopefully that makes sense to someone and they have an idea how to do it. 希望这对某人有意义,并且他们知道如何去做。

Thanks in advance. 提前致谢。

EDIT: SORRY -- based on the very useful answer provided by Santosh below (which I can definitely use on another problem I have), I've realised that I haven't fully explained myself. 编辑:抱歉 - 根据以下Santosh提供的非常有用的答案(我可以肯定地使用我的另一个问题),我意识到我还没有完全解释自己。

Each row has an identifier (shown here as COLUMN_IDENT) 每行都有一个标识符(此处显示为COLUMN_IDENT)

COLUMNA    COLUMNB  COLUMNC    COLUMN_IDENT
    1234   1        12341      555
    1234   1        12341      555
    1234   2        12342      555
    2345   1        23451      666
    2345   2        23452      666
    2345   2        23452      666

I then make a table that -- using an array formula (based on the FREQUENCY function) -- shows for each unique identifier (in this case 555 and 666) how many unique values occur for the concatenated COLUMN C. So, here it would 2 for 555 and also 2 for 666). 然后我创建一个表 - 使用数组公式(基于FREQUENCY函数) - 显示每个唯一标识符(在本例中为555和666),为连接的COLUMN C发生了多少个唯一值。所以,这里它会2表示555,表示2表示666)。

I end up with a report like this: 我最终得到了这样一份报告:

IDENT   UNIQUE_COUNT
555     2
666     2

What I'm trying to do with code is to get rid of the need for the helper COLUMNC in the original table and still allow me to achieve the same result in the second table. 我正在尝试使用代码来摆脱原始表中对辅助COLUMNC的需求,并且仍允许我在第二个表中实现相同的结果。

Again, thanks in advance. 再次,在此先感谢。

Try below code. 试试下面的代码。

Sub sample()

    Dim lastRow As Long

    With Sheets("Sheet1")
        lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("A1:B" & lastRow).RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
    End With
End Sub

在此输入图像描述

OK, months later I've realised the answer to my own question. 好吧, 几个月后,我意识到了自己问题的答案。

You can do it a couple of ways (and there's probably more), 你可以通过几种方式做到(而且可能还有更多),

1) You can do it without VBA using an Array Formula. 1)您可以使用数组公式在没有VBA的情况下执行此操作。

So if you have: 所以如果你有:

+---+---------+---------+-----+
|   |    A    |    B    |  C  |
+---+---------+---------+-----+
| 1 | COLUMNA | COLUMNB | ID  |
| 2 | 1234    | 1       | 555 |
| 3 | 1234    | 1       | 555 |
| 4 | 1234    | 2       | 555 |
| 5 | 2345    | 1       | 666 |
| 6 | 2345    | 2       | 666 |
| 7 | 2345    | 2       | 666 |
| 8 |         |         |     |
+---+---------+---------+-----+

... and you want to generate this: ...而你想要生成这个:

+---+-----+--------------+
|   |  D  |      E       |
+---+-----+--------------+
| 1 | ID  | UNIQUE COUNT |
| 2 | 555 | 2            |
| 3 | 666 | 2            |
| 4 |     |              |
+---+-----+--------------+

... then put this as an Array Formula in E2 and E3 (making sure the ranges are correct): ...然后将其作为E2和E3中的数组公式(确保范围正确):

=SUM(IF(FREQUENCY(IF(C2:C7=D2,MATCH(A2:A7&B2:B7,A2:A7&B2:B7,0)),IF(C2:C7=D2,MATCH(A2:A7&B2:B7,A2:A7&B2:B7,0)))>0,1))

2) If you need it in VBA, you can (amongst other things) do the Evaluate "cheat" (here I've used square brackets which is shorthand for Evaluate ): 2)如果你需要在VBA中,你可以(除其他外)做Evaluate “作弊”(这里我使用方括号,这是Evaluate简写):

Range("E2") = [SUM(IF(FREQUENCY(IF(C2:C7=D2,MATCH(A2:A7&B2:B7,A2:A7&B2:B7,0)),IF(C2:C7=D2,MATCH(A2:A7&B2:B7,A2:A7&B2:B7,0)))>0,1))]

So, if anyone was wondering, that's a couple of ways of building "virtual helper columns". 所以,如果有人想知道,那就是构建“虚拟助手列”的几种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM