gpt4 book ai didi

excel - 使用带有 Ranges 数组的 Consolidate 函数

转载 作者:行者123 更新时间:2023-12-04 20:23:38 25 4
gpt4 key购买 nike

我目前正在尝试创建一个系统,用户可以在其中选择一些引用表格的复选框并获取所选表格的合并表格。到目前为止,我的系统生成复选框,检查哪些复选框被勾选,将该列表传递给另一个函数,该函数应该读取已选择表的范围并将该范围传递给合并函数以创建最终表。
我无法让合并功能正常工作。从我收集到的 .Consolidation 函数需要一系列字符串形式的范围才能工作,但无论我如何尝试传递范围,我似乎都无法让该函数为我工作
下面是生成数组的代码,同时还在另一个工作表上创建了一个组合表,因此我可以确保它实际上正在运行。组合表制作起来没有任何麻烦。

Function rangesfromtables(workinglist() As Variant) As Variant
Dim tbl As ListObject
Dim sht As Worksheet
Dim workingrange As Range
Dim workingarray() As Variant
Dim item As Variant
Dim loopcount As Integer
Dim destinationsheet As Worksheet
Dim endrow As Long
Dim numrows As Long
Set destinationsheet = ThisWorkbook.Worksheets("WorkingSheet")
destinationsheet.Cells.Clear
loopcount = 0
endrow = 1
For Each item In workinglist
'Loop through each sheet and table in the workbook
For Each sht In ThisWorkbook.Worksheets
For Each tbl In sht.ListObjects
If StrComp(item, tbl.name, vbTextCompare) = 0 Then
If loopcount = 0 Then
Set workingrange = tbl.Range
ReDim workingarray(0)
workingarray(UBound(workingarray)) = sht.name & tbl.Range.Address(ReferenceStyle:=xlR1C1)
loopcount = loopcount + 1
Else
Set workingrange = tbl.DataBodyRange
ReDim Preserve workingarray(UBound(workingarray) + 1)
workingarray(UBound(workingarray)) = sht.name & tbl.Range.Address(ReferenceStyle:=xlR1C1)
End If
numrows = workingrange.Rows.Count 'Below code copies table data to separate worksheet for checking
workingrange.Copy
destinationsheet.Range("A" & endrow).PasteSpecial Paste:=xlPasteValues

endrow = endrow + numrows
End If
Next tbl
Next sht
Next item

rangesfromtables = workingarray

End Function
这是应该合并表格的功能
Sub consolidatetable(workingrange() As Variant)
Dim destinationsheet As Worksheet


Set destinationsheet = ThisWorkbook.Worksheets("Main Sheet")

destinationsheet.Cells.Clear

destinationsheet.Range("A6").Consolidate _
Sources:=workingrange, _
Function:=x1Sum, _
TopRow:=True, _
LeftColumn:=True, _
CreateLinks:=False

End Sub
每当我运行代码时,我都会收到错误 1004 范围类的合并方法失败
我有一种感觉,我的问题是将表格的范围错误地放入数组中,但是我尝试了许多不同的方法,但似乎无法做到。我尝试过使用字符串数组而不是变体,尝试在不修改它们的情况下传递范围,目前我正在尝试将范围转换为字符串,但我不知道我是否做得正确。
任何帮助,将不胜感激。
一个小更新,即使我手动输入一个范围,我仍然会收到错误,但我觉得我根据文档正确使用了该功能
Sub consolidatetable(workingrange() As Variant)
Dim destinationsheet As Worksheet


Set destinationsheet = ThisWorkbook.Worksheets("Main Sheet")

destinationsheet.Cells.Clear

destinationsheet.Range("A6").Consolidate _
Sources:="WorkingSheet!A1:J23", _
Function:=x1Sum, TopRow:=True, LeftColumn:=True, CreateLinks:=False

End Sub

最佳答案

这是一个使用 Consolidate 的简单示例。适合我的方法。希望这对您有用,并帮助您了解什么不起作用,但如果没有,我们就会知道不是您的代码有问题。
首先将以下内容添加到 Sheet1:
enter image description here
Sheet2中的以下内容:
enter image description here
然后确保 Sheet3 为空并运行以下命令(来自同一工作簿中的模块):

Sub ConsolidateTest()

Dim rng As Range
Set rng = ThisWorkbook.Sheets("Sheet3").Cells(1, 1)

rng.Consolidate _
Sources:=Array("Sheet1!R1C1:R3C3", "Sheet2!R1C1:R3C3"), _
Function:=-4157, _
TopRow:=True, _
LeftColumn:=True, _
CreateLinks:=False

End Sub
然后,您应该得到以下结果:
enter image description here

关于excel - 使用带有 Ranges 数组的 Consolidate 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66202337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com