I have a worksheet where I copy data from Table1 to Table2.
我有一个工作表,我将数据从Table1复制到Table2。
Once I have copy-pasted the data to Table2, which sits below Table1, I select the first cell in the first column of Table2 and press Ctrl+Shift+Down to select down to the last used cell. Finally, I apply a concatenation array formula that adds a suffix to the values of the related cells in Table1. I have recorded these Table2 steps as a macro.
将数据复制粘贴到位于Table1下面的Table2后,我选择Table2第一列中的第一个单元格,然后按Ctrl + Shift +向下选择向下移动到最后使用的单元格。最后,我应用一个串联数组公式,为Table1中相关单元格的值添加后缀。我已将这些Table2步骤记录为宏。
To demonstrate, Table1 looks like this:
为了演示,Table1看起来像这样:
Table1
| A B C D E
---+-----------------------
1 | Name V1 V3 V3 V4
---+-----------------------
2 | Wood 10 10 10 10
3 | wood 28 28 28 28
4 | tree 30 45 60 68
5 | plastic 50 50 50 50
6 | tree 50 50 50 50
7 | iron 64 75 75 80
This is the formula I use in column A of Table2:
这是我在表2的A列中使用的公式:
{=concatenate(A2:A7," - A")}
This is the result after applying it to Table2:
这是将其应用于Table2后的结果:
Table2
| A B C D E
---+-----------------------------------
25 | Wood - A 25 25 25 25
26 | wood - A 50 50 50 50
27 | tree - A 50 50 100 100
28 | plastic - A 100 100 100 100
29 | tree - A 100 100 100 100
30 | iron - A 100 100 100 100
Now, when I add new entries to Table1, for example in cells A8
& A9
:
现在,当我向Table1添加新条目时,例如在单元格A8和A9中:
Table1a
| A B C D E
---+-----------------------
1 | Name V1 V3 V3 V4
---+-----------------------
2 | Wood 10 10 10 10
3 | wood 28 28 28 28
4 | tree 30 45 60 68
5 | plastic 50 50 50 50
6 | tree 50 50 50 50
7 | iron 64 75 75 80
8 | table 20 25 0 30
9 | plastic 54 35 21 0
after running the recorded macro, instead of using the new range A2:A9
, it's using the previous recorded range (A2:A7
), resulting in the #N/A
errors as can be seen in Table2a:
在运行录制的宏之后,不是使用新的范围A2:A9,而是使用先前记录的范围(A2:A7),导致#N / A错误,如表2a所示:
Table2a
| A B C D E
---+-----------------------------------
25 | Wood - A 25 25 25 25
26 | wood - A 50 50 50 50
27 | tree - A 50 50 100 100
28 | plastic - A 100 100 100 100
29 | tree - A 100 100 100 100
30 | iron - A 100 100 100 100
31 | #N/A #N/A #N/A #N/A #N/A
32 | #N/A #N/A #N/A #N/A #N/A
This is because in the array formula, the range is not dynamic, but fixed.
这是因为在数组公式中,范围不是动态的,而是固定的。
So, I would like a formula which auto-adjusts the range when new entries are added or deleted from Table1. Something similar to what I have in the recorded macro which selects all the cells in column A of Table2, before applying the array formula:
所以,我想要一个公式,在Table1中添加或删除新条目时自动调整范围。在应用数组公式之前,类似于我在录制的宏中选择Table2的A列中的所有单元格的东西:
Application.Goto Reference:="R25C1"
Range("A25", Range("A25").End(xlDown)).Select
I was thinking of something like the following, where I provide the starting cell of Table1 and the range down to the last cell with data is worked out automatically:
我正在考虑以下内容,其中我提供Table1的起始单元格,并且范围向下到最后一个单元格,数据自动计算出来:
Selection.FormulaArray = "=concatenate(Range("A2",Range("A2").End(xldown)).Select,""- A"")"
I would like a formula solution, that I can apply to all the cells. I don't want to define variables, etc, to do this.
我想要一个公式解决方案,我可以应用于所有细胞。我不想定义变量等来做这件事。
2
The answer is as simple as:
答案很简单:
Selection.FormulaArray = "=concatenate(A2:" & Range("A2").End(xlDown).Address & ","" - A"")"
Note that you have to convert every single quote, "
, in the original array formula to double quotes, ""
.
请注意,您必须将每个单引号“,在原始数组公式中转换为双引号”,“”。
The trick with the solution is to calculate the address of the last cell of the range, and replace the A6
with this address. This has to be done outside the string, and added to the string by using the string concatenation operator &
.
该解决方案的技巧是计算该范围的最后一个单元的地址,并用该地址替换A6。这必须在字符串外部完成,并使用字符串连接运算符&添加到字符串中。
However, the Application.Goto
, Select
, and Selection
are unnecessary.
但是,Application.Goto,Select和Selection是不必要的。
Thus you really should use:
因此你真的应该使用:
Range("A25", Range("A25").End(xlDown)).FormulaArray _
= "=concatenate(A2:" & Range("A2").End(xlDown).Address & ","" - A"")"
Another way of coding this, using With
, would be:
另一种使用With编码的方法是:
With Range("A25", Range("A25").End(xlDown))
.FormulaArray = "=concatenate(A2:" & Range("A2").End(xlDown).Address & ","" - A"")"
End With
Finally, a "proper" VBA solution (the one you specifically said you didn't want):
最后,一个“适当的”VBA解决方案(你明确表示你不想要的那个):
With Range("A2").End(xlDown)
Range("A25", .Offset(25 - 2)).FormulaArray = "=concatenate(A2:" & .Address & ","" - A"")"
End With
Note that with this last solution, there is no longer a requirement to manually pre-copy the data from Table1 to Table2.
请注意,使用此最后一个解决方案,不再需要手动将数据从Table1预先复制到Table2。
ADDENDUM:
Just realised that you were asking for a "pure" formula solution. If that's all you're after, then this formula works:
刚刚意识到你要求的是“纯粹的”配方解决方案。如果这就是你所追求的,那么这个公式可行:
{=CONCATENATE(A2:INDEX(A1:A24,MATCH("*",A1:A24,-1))," - A")}
Just remember to double quote all the single quotes when converting it for use in VBA. The resulting VBA would thus look like this:
只需记住在转换它以便在VBA中使用时双引所有单引号。因此得到的VBA看起来像这样:
Selection.FormulaArray = "=CONCATENATE(A2:INDEX(A1:A24,MATCH(""*"",A1:A24,-1)),"" - A"")"
Also just realised that you don't actually need a complicated dynamic formula if you just use a normal formula instead of an array formula.
如果您只使用普通公式而不是数组公式,也只是意识到您实际上并不需要复杂的动态公式。
Just select column A
of Table2 and enter the following formula in cell A25
. Press Ctrl+Enter to enter it as normal formula in all the selected cells.
只需选择表2的A列,然后在单元格A25中输入以下公式。按Ctrl + Enter键将其作为所有选定单元格中的常规公式输入。
=CONCATENATE(A2," - A")
The VBA code for this is:
VBA代码是:
Selection.Formula = "=CONCATENATE(A2,"" - A"")"
This formula will automatically adjust as you add or remove rows from Table1.
在您添加或删除Table1中的行时,此公式将自动调整。
Of course, I would recommend using the "proper" fully automatic VBA code which doesn't require manual pre-copying:
当然,我建议使用“正确”的全自动VBA代码,不需要手动预复制:
Range("A25", Range("A2").End(xlDown).Offset(25 - 2)).Formula = "=CONCATENATE(A2,"" - A"")"
And Finally:
After looking at your previous questions and the supplied data in this one, the solution for columns B
-E
is similarly simple. The same formula is used for all those columns, entered into cell B25
using Ctrl+Enter.
在查看之前的问题和本文提供的数据之后,B-E列的解决方案同样简单。所有这些列都使用相同的公式,使用Ctrl + Enter输入到单元格B25中。
Normal formula:
=IF(B2<25,25,IF(B2<50,50,100))
Equivalent VBA:
Selection.Formula = "=IF(B2<25,25,IF(B2<50,50,100))"
"Proper" fully-auto VBA:
“正确的”全自动VBA:
With Range("A2").End(xlDown)
Range("B25", .Offset(25 - 2, 4)).Formula = "=IF(B2<25,25,IF(B2<50,50,100))"
End With
Note that I have simplified the formula by using numbers directly instead of numbers as strings and then using VALUE()
to convert them to numbers.
请注意,我通过直接使用数字而不是数字作为字符串来简化公式,然后使用VALUE()将它们转换为数字。
0
You need a range in the formula that dynamically expands according to the text values in column A.
您需要公式中的范围,该范围根据A列中的文本值动态扩展。
=concatenate(A2:index(a:a, match("zzz", a:a)), " - A")
In order to homogenize the numeric entries, they should use the same match formula to determine the terminating row. For column B,
为了使数字条目均匀化,它们应使用相同的匹配公式来确定终止行。对于B栏,
b2:index(b:b, match("zzz", a:a))