gpt4 book ai didi

vba - Excel VBA 运行时错误 1004 'a table can' t 与另一个表重叠'

转载 作者:行者123 更新时间:2023-12-03 07:46:36 25 4
gpt4 key购买 nike

每次我打开我的 xlsm 文件时都会发生此运行时错误“一个表不能与另一个表重叠”。在我单击调试窗口后,突出显示以下 VBA 代码行。

Set tbl = Sheets("DataSheet").ListObjects.Add(xlSrcRange, rng, , xlYes)

下面附上完整版,仅供引用。
Private Sub Workbook_Open()
Dim tbl As ListObject
Dim rng As Range

'Ungroup worksheets
ThisWorkbook.Sheets("DataSheet").Select

Set rng = Range(Range("A1"), Range("A1").SpecialCells(xlLastCell))
Set tbl = Sheets("DataSheet").ListObjects.Add(xlSrcRange, rng, , xlYes)
tbl.Name = "ReportTable"
tbl.TableStyle = "TableStyleMedium7"
End Sub

那么我应该怎么做才能解决这个问题呢?为什么在那条线上给出错误?

最佳答案

以下是我最终得到的解决方案。似乎正在解决问题。

    Dim tblExists As Boolean

'Check the same already exists
tblExists = False
For Each o In Sheets("DataSheet").ListObjects
If o.Name = "ReportTable" Then tblExists = True
Next o

'If exists, delete the table first
If (tblExists) Then
Sheets("DataSheet").ListObjects("ReportTable").Unlist
End If

关于vba - Excel VBA 运行时错误 1004 'a table can' t 与另一个表重叠',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39584684/

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