如何从单个工作簿中多个工作表的同一列收集数据?

发布于 2024-08-23 05:06:27 字数 365 浏览 3 评论 0原文

好的,这就是我想要完成的任务。对于此示例,我有一个工作簿,由 4 个数据表和一个总计表组成。 4 个数据表均具有相似的名称,遵循相同的模式,唯一的区别是日期。 (例如 9854978_1009_US.txt,其中 1009 是更改的日期,而文件名的其余部分相同)。在每个文档中,F 列都包含一系列数字,我想求其总和,但我不知道 F 中实际上有多少个单元格包含数字。 (但是,数字下面永远不会有其他信息,因此理论上我可以将整个 F 列添加在一起)。随着时间的推移,我还将向工作簿添加新文件,并且不想重写从 F 列收集数据的代码。本质上,我想要完成的是让“总计”文档获取每一列 F来自工作簿中名称为“9854978_??????_US.txt”的文档,其中问号根据文件名而变化。我该如何用纯 Excel 代码执行此操作?

Okay so here is what I want to accomplish. For this example I have a single workbook composed of 4 data sheets plus a totals sheet. Each of the 4 data sheets has a similar name following the same pattern where the only difference is the date. (Ex. 9854978_1009_US.txt, where 1009 is the date that changes while the rest of the file name is the same). In each of those documents column F contains a series of number that I would like to find the sum of but I will have no idea how many cells in F actually contain numbers. (However there will never be additional information below it the numbers so I could in theory just add the entire F column together). I will also add new files to the workbook over time and do not want to have to rewrite the code of which I gather my data from column F. Essentially what I would like to accomplish is for the 'totals' document to take every column F from documents in the workbook with the name of '9854978_????_US.txt', where the question marks change based on the file name. How would I go about doing this in pure Excel code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2024-08-30 05:06:27

在这种情况下,我建议使用 VBA,但我找到了一个(诚然低效)仅包含 Excel 公式的解决方案。

在新工作表的单元格 A1 中写入 1,然后在下面的单元格中写入 2。
选择两个单元格并向下拖动。自动完成功能将生成从 1 到拖动结束的数字。继续下去,直到达到数字 9999

在单元格 B1 中输入公式 =TEXT(A1,"000#");向下拖动公式以获取从 00019999 的文本。

在单元格 C1 中输入公式 ="9854978_"&B1&"_US.txt";向下拖动公式以获取从 9854978_0001_US.txt 到 9854978_9999_US.txt 的文本

在单元格 D1 中输入公式:
=if(ISERROR(SUM(INDIRECT(C1&"!F:F"))),0,SUM(INDIRECT(C1&"!F:F"))) ;向下拖动公式即可得出总和。

通过减少可能的数字集,该解决方案的效率可能会大大降低。缺少年份,可能的日期只有 365。

PS:公式可能略有错误,因为我是从本地化版本的 Excel 翻译它们的。

In cases like this I would recommend to use VBA, but I've found a (admittedly inefficient) solution with only excel formulas.

In the cell A1 of a new sheet write 1, then 2 in the cell below.
Select the two cells and drag down. The autocomplete function will produce the number from 1 until the end of the drag. Carry on until you reach the number 9999

In the cell B1 put the formula =TEXT(A1,"000#"); drag the formula down to get the text from 0001 to 9999.

In the cell C1 put the formula ="9854978_"&B1&"_US.txt"; drag the formula down to get the text from 9854978_0001_US.txt to 9854978_9999_US.txt

In the cell D1 put the formula:
=if(ISERROR(SUM(INDIRECT(C1&"!F:F"))),0,SUM(INDIRECT(C1&"!F:F"))) ; drag the formula down to get the sums.

The solution could become much less inefficient reducing the set of possible numbers. Missing the year the possible dates are only 365.

PS: the formulas could be slightly wrong because I translated them from my localized version of excel.

执手闯天涯 2024-08-30 05:06:27

这是一个将执行此计算的函数:

Function TotalColF() As Double

    Dim wks As Worksheet

    For Each wks In ThisWorkbook.Worksheets
        If left(wks.name,7) = "9854978" then
            TotalColF = TotalColF + Application.WorksheetFunction.Sum(wks.Range("F:F"))
        End If
    Next wks

End Function

这​​不是一个易失函数,因此如果您添加另一个工作表或更改 F 列中的值,则修改后的总计将不会显示,直到您通过执行 Ctrl + Alt + F9 强制重新计算。

如果您需要一个始终显示正确总数的易失性函数,请告诉我。易失性函数可能会使重新计算时间变慢,但这对您来说可能不是问题。

如果您通过 VBA 添加新工作表,则可以使用 VBA 指令强制重新计算:Application.CalculateFull

Here's a function that will do this calculation:

Function TotalColF() As Double

    Dim wks As Worksheet

    For Each wks In ThisWorkbook.Worksheets
        If left(wks.name,7) = "9854978" then
            TotalColF = TotalColF + Application.WorksheetFunction.Sum(wks.Range("F:F"))
        End If
    Next wks

End Function

This is not a volatile function, so if you add another sheet, or change a value in column F, the revised total will not show up until you force a recalculation by doing Ctrl + Alt + F9.

If you need a volatile function which will always the display the correct total, let me know. Volatile functions can make recalculation times slow, but that may not be an issue for you.

If you are adding the new sheets via VBA, there is a VBA instruction you can use to force the recalculation: Application.CalculateFull

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文