如何使用几个工作表中的值构建摘要表?

发布于 2025-02-05 04:17:55 字数 4207 浏览 3 评论 0原文

我正在尝试将工作簿中其他工作表中的单元格中的数据吸引到摘要表中。用例是,工作簿用户创建并修改了一系列详细信息工作表,这些工作表与几个单元格形式形式,其中包含诸如名称和年龄的值以及更多的详细信息,而摘要则不需要的详细信息。

到目前为止,我已经研究了以下内容:

  • 3D参考
    • 这似乎很有用,但是由于本工作簿的用户会随着时间的推移添加新的细节表,所以我要么必须创建空的警卫表,并告诉他们不要与之混乱,或者硬代码是第一个和最后一个名称细节表,再次告诉他们不要与那些混乱。
    • 无论如何,似乎您可以直接使用3D引用作为公式的值。 = Joe:John!a2给出一个值错误,而不是返回三行。
  • UDF返回范围
    • 我遇到的问题是如何实际返回值。
    • 我不能仅仅迭代纸张并建立一个联合(结果,电流),因为该行抛出了不会在调试器中破坏导致Excel值错误的VB错误。
    • 我尝试将每个值存储在集合中,然后将其转换为一个数组,第一个昏暗的行是行的数量,第二个昏暗为1列。当我尝试设置函数返回范围的.value时,同样,VB错误如上所述。
  • 我尚未尝试使用该函数(在这种情况下为sub?)直接通过跟踪行的数量并通过单元格中的摘要工作表,直接编写值。这感觉真的很刺耳,所以我希望这不是建议的解决方案。

我很想能够通过一组单元格参考并获取表格,但是我很乐意使用摘要中的每一列使用单独的公式来指定整理的值。

我并不是要提供一个工作解决方案,但是我遇到了很多死胡同和令人沮丧的错误,我会对一些可靠的潜在客户感到非常满意。谢谢你!

这是工作簿结构的一些模型。

表1命名为摘要:

ABCD
1名称颜色
2JoeRed
3JaneBlue
4JohnBlack

上面提到的UDF的尝试是试图在摘要表上使用以下公式:= getsummarydata(“ $ d $ 4”)

,这里有三个示例详细信息:

Abcd
1细节
2
3名称年龄性别最爱颜色
442m红色
5
6传记
7lorem ipsum

jane

abcd
1详细信息
2
3 3个名称年龄性别最爱颜色
4Jane18F蓝色
5
6传记
7Lorem Ipsum

John

ABCD
1细节
2
3名称年龄性别最爱的颜色
4Joe99mBlack
5
6传记
7Lorem 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:

ABCD
1NameColor
2JoeRed
3JaneBlue
4JohnBlack

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

ABCD
1Details
2
3NameAgeGenderFavorite Color
4Joe42MRed
5
6Biography
7Lorem Ipsum

Jane

ABCD
1Details
2
3NameAgeGenderFavorite Color
4Jane18FBlue
5
6Biography
7Lorem Ipsum

John

ABCD
1Details
2
3NameAgeGenderFavorite Color
4Joe99MBlack
5
6Biography
7Lorem 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 技术交流群。

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

发布评论

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

评论(1

一向肩并 2025-02-12 04:17:55

如果您有“自动溢流阵列公式”版本的Excel,这对我有用

Function PersonaSheetsData(RangeRef As String)
    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
            col.Add currSheet.Range(RangeRef).Value
        End If
    Next currSheet
    PersonaSheetsData = collectionToArray(col)
End Function

Function collectionToArray(c As Collection) As Variant
    Dim a() As Variant, i As Integer
    ReDim a(1 To c.Count, 1 To 1) '1-based feels more natural here...
    For i = 1 To c.Count
        a(i, 1) = c.Item(i)
    Next
    collectionToArray = a
End Function

This worked for me if you have an "auto-spill array formulas" version of Excel

Function PersonaSheetsData(RangeRef As String)
    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
            col.Add currSheet.Range(RangeRef).Value
        End If
    Next currSheet
    PersonaSheetsData = collectionToArray(col)
End Function

Function collectionToArray(c As Collection) As Variant
    Dim a() As Variant, i As Integer
    ReDim a(1 To c.Count, 1 To 1) '1-based feels more natural here...
    For i = 1 To c.Count
        a(i, 1) = c.Item(i)
    Next
    collectionToArray = a
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文