如何使用几个工作表中的值构建摘要表?
我正在尝试将工作簿中其他工作表中的单元格中的数据吸引到摘要表中。用例是,工作簿用户创建并修改了一系列详细信息工作表,这些工作表与几个单元格形式形式,其中包含诸如名称和年龄的值以及更多的详细信息,而摘要则不需要的详细信息。
到目前为止,我已经研究了以下内容:
- 3D参考
- 这似乎很有用,但是由于本工作簿的用户会随着时间的推移添加新的细节表,所以我要么必须创建空的警卫表,并告诉他们不要与之混乱,或者硬代码是第一个和最后一个名称细节表,再次告诉他们不要与那些混乱。
- 无论如何,似乎您可以直接使用3D引用作为公式的值。
= Joe:John!a2
给出一个值错误,而不是返回三行。
- UDF返回范围
- 我遇到的问题是如何实际返回值。
- 我不能仅仅迭代纸张并建立一个联合(结果,电流),因为该行抛出了不会在调试器中破坏导致Excel值错误的VB错误。
- 我尝试将每个值存储在集合中,然后将其转换为一个数组,第一个昏暗的行是行的数量,第二个昏暗为1列。当我尝试设置函数返回范围的.value时,同样,VB错误如上所述。
- 我尚未尝试使用该函数(在这种情况下为sub?)直接通过跟踪行的数量并通过单元格中的摘要工作表,直接编写值。这感觉真的很刺耳,所以我希望这不是建议的解决方案。
我很想能够通过一组单元格参考并获取表格,但是我很乐意使用摘要中的每一列使用单独的公式来指定整理的值。
我并不是要提供一个工作解决方案,但是我遇到了很多死胡同和令人沮丧的错误,我会对一些可靠的潜在客户感到非常满意。谢谢你!
这是工作簿结构的一些模型。
表1命名为摘要:
A | B | C | D | |
---|---|---|---|---|
1 | 名称 | 颜色 | ||
2 | Joe | Red | ||
3 | Jane | Blue | ||
4 | John | Black |
上面提到的UDF的尝试是试图在摘要表上使用以下公式:= getsummarydata(“ $ d $ 4”)
,这里有三个示例详细信息:
乔
A | b | c | d | |
---|---|---|---|---|
1 | 细节 | |||
2 | ||||
3 | 名称 | 年龄 | 性别 | 最爱颜色 |
4 | 乔 | 42 | m | 红色 |
5 | ||||
6 | 传记 | |||
7 | lorem ipsum |
jane
a | b | c | d | |
---|---|---|---|---|
1 | 详细信息 | |||
2 | ||||
3 3个 | 名称 | 年龄 | 性别 | 最爱颜色 |
4 | Jane | 18 | F | 蓝色 |
5 | ||||
6 | 传记 | |||
7 | Lorem Ipsum |
John
A | B | C | D | |
---|---|---|---|---|
1 | 细节 | |||
2 | ||||
3 | 名称 | 年龄 | 性别 | 最爱的颜色 |
4 | Joe | 99 | m | Black |
5 | ||||
6 | 传记 | |||
7 | Lorem Ipsum |
终于,这是我上次的一些非功能性VBA代码试图:
Option Explicit
Function PersonaSheetsData(RangeRef As String) As Range
Dim result As Range, thisRange As Range, currSheet As Worksheet, col As New Collection
For Each currSheet In ActiveWorkbook.Worksheets
' Guard to only pull data from Details worksheets
If currSheet.Cells(1, 1) = "Details" Then
Set thisRange = currSheet.Range(RangeRef)
col.Add thisRange.Value
End If
Next currSheet
Set result = ActiveCell
Set result = result.Resize(col.Count, 1)
Dim arr As Variant
arr = collectionToArray(col)
result.Value = arr
Set PersonaSheetsData = result
End Function
Function collectionToArray(c As Collection) As Variant
Dim a() As Variant: ReDim a(0 To c.Count - 1, 0 To 0)
Dim i As Integer
For i = 1 To c.Count
a(i - 1, 0) = c.Item(i)
Next
collectionToArray = a
End Function
I'm trying to pull data from cells in a set of other worksheets in the workbook into a summary table. The use case is that the workbook user creates and modifies a series of Details worksheets that are form-like with several cells that contain values such as Name and Age as well as more verbose information that is not needed for the summary.
So far, I've researched the following:
- 3D References
- This seems useful, but because the users of this workbook will be adding new detail sheets over time, I either have to create empty guard sheets and tell them not to mess with those, or hard code the names of the first and last detail sheets and again, tell them not to mess with those.
- Regardless, it doesn't appear that you can directly use a 3D reference as the value of a formula.
=Joe:John!A2
gives a value error rather than returning three rows.
- UDF Returning a Range
- The problem I'm running into here is how to actually return the values.
- I can't just iterate through the sheets and build a Union(Result, CurrentRange) because that line throws a VB error that doesn't break in the debugger which results in an Excel Value Error.
- I tried storing each of the values in a Collection then converting that to an Array with the first dim being the number of rows and the second dim being 1 column. When I try to set the .Value of the function's return Range, again, VB error as above.
- I have not yet tried having the Function (or Sub in this case?) directly write the values by keeping track of the number of rows and moving through the Summary worksheet cell by cell. This feels really hacky, so I'm hoping it isn't the suggested solution.
I would love to be able to just pass a set of cell references and get a table, but I'd be happy to work with using a separate formula for each column in the summary to specify the value to collate.
I'm not asking for a working solution to be handed off, but I've hit so many dead ends and frustrating errors I'd be very happy with some solid leads. Thank you!
Here are some mockups of the structure of the workbook.
Sheet 1 is named Summary:
A | B | C | D | |
---|---|---|---|---|
1 | Name | Color | ||
2 | Joe | Red | ||
3 | Jane | Blue | ||
4 | John | Black |
The attempts with UDFs I mentioned above were trying to use the following formula in cell B2 on the summary sheet: =GetSummaryData("$D$4")
And here are three example detail sheets:
Joe
A | B | C | D | |
---|---|---|---|---|
1 | Details | |||
2 | ||||
3 | Name | Age | Gender | Favorite Color |
4 | Joe | 42 | M | Red |
5 | ||||
6 | Biography | |||
7 | Lorem Ipsum |
Jane
A | B | C | D | |
---|---|---|---|---|
1 | Details | |||
2 | ||||
3 | Name | Age | Gender | Favorite Color |
4 | Jane | 18 | F | Blue |
5 | ||||
6 | Biography | |||
7 | Lorem Ipsum |
John
A | B | C | D | |
---|---|---|---|---|
1 | Details | |||
2 | ||||
3 | Name | Age | Gender | Favorite Color |
4 | Joe | 99 | M | Black |
5 | ||||
6 | Biography | |||
7 | Lorem Ipsum |
Finally, here is some non-functional VBA code from my last attempt:
Option Explicit
Function PersonaSheetsData(RangeRef As String) As Range
Dim result As Range, thisRange As Range, currSheet As Worksheet, col As New Collection
For Each currSheet In ActiveWorkbook.Worksheets
' Guard to only pull data from Details worksheets
If currSheet.Cells(1, 1) = "Details" Then
Set thisRange = currSheet.Range(RangeRef)
col.Add thisRange.Value
End If
Next currSheet
Set result = ActiveCell
Set result = result.Resize(col.Count, 1)
Dim arr As Variant
arr = collectionToArray(col)
result.Value = arr
Set PersonaSheetsData = result
End Function
Function collectionToArray(c As Collection) As Variant
Dim a() As Variant: ReDim a(0 To c.Count - 1, 0 To 0)
Dim i As Integer
For i = 1 To c.Count
a(i - 1, 0) = c.Item(i)
Next
collectionToArray = a
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有“自动溢流阵列公式”版本的Excel,这对我有用
This worked for me if you have an "auto-spill array formulas" version of Excel