范围,在Excel中合并:“运行时错误'1004':应用程序定义或对象定义错误”

luaexgnf  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(196)

我有问题,让这个工作的某种原因,在某种程度上,我的头发变白。有一些数据表'测试',我试图巩固工作表'finaldata'。以下是数据截图:

Sub ConsolidateData()
    Dim finalDataSheet As Worksheet
    Dim testSheet As Worksheet
    Dim finalDataRange As Range
    Dim testRange As Range
        
    ' Set references to the worksheets
    Set finalDataSheet = ThisWorkbook.Worksheets("finaldata")
    Set testSheet = ThisWorkbook.Worksheets("Test")
    
    ' Set the range to consolidate in the finaldata sheet
    Set finalDataRange = finalDataSheet.Range("A1")
    
    ' Set the range to consolidate from the Test sheet
    Set testRange = testSheet.Range("A:P")
    
    ' Consolidate the data
    finalDataRange.Consolidate Sources:=Array(testRange.Address), Function:=xlSum, LeftColumn:=True
End Sub

我发现问题出在Sources:=Array(testRange.Address)位,但我真的不知道它是什么。
希望任何人都可以帮助,并有一个我做错了什么端倪。(我试着问妻子,但我得到的只是无穷无尽的不相关问题)。
谢谢你
马克

uubf1zoe

uubf1zoe1#

docs状态:
合并的源代码作为R1C1样式表示法中的文本引用字符串数组。参考必须包括要合并的图纸的完整路径。
试试这个(未测试):

Sub ConsolidateData()
    Dim finalDataSheet As Worksheet
    Dim testSheet As Worksheet
    Dim finalDataRange As Range
    Dim testRange As Range
        
    ' Set references to the worksheets
    Set finalDataSheet = ThisWorkbook.Worksheets("finaldata")
    Set testSheet = ThisWorkbook.Worksheets("Test")
    
    ' Set the range to consolidate in the finaldata sheet
    Set finalDataRange = finalDataSheet.Range("A1")
    
    ' Set the range to consolidate from the Test sheet
    Set testRange = testSheet.Range("A:P")
    
    ' Consolidate the data
    finalDataRange.Consolidate _
            Sources:=Array(testSheet.Name & "!" & testRange.Address(, , xlR1C1), _
            Function:=xlSum, _
            LeftColumn:=True
End Sub
y1aodyip

y1aodyip2#

“合并的源代码作为R1C1样式表示法中的文本引用字符串数组。参考必须包括要合并的图纸的完整路径。”

....
finalDataRange.Consolidate Sources:=Array(testSheet.Name & "!" & testRange.Address(0, 0, xlR1C1, False, testSheet.Range("A1"))), Function:=xlSum, LeftColumn:=True
....

相关问题