热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

动态数组公式,允许目标表扩展-Dynamicarrayformulathatallowsfortargettableexpansion

IhaveaworksheetwhereIcopydatafromTable1toTable2.我有一个工作表,我将数据从Table1复制到Table2。OnceIha

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 个解决方案

#1


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"")"

X-Solution (See What is the XY problem?)

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()将它们转换为数字。

#2


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))

推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
author-avatar
洛特大人_382
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有