[英]Excel VBA run-time error 1004 'a table can't overlap another table'
This run-time error, "a table can't overlap another table", occurs every time I open up my xlsm file. 每次打开我的xlsm文件时都会出现此运行时错误“一个表不能与另一个表重叠”。 After I click through the debug window, the following line of VBA code is highlighted.
单击调试窗口后,将突出显示以下VBA代码行。
Set tbl = Sheets("DataSheet").ListObjects.Add(xlSrcRange, rng, , xlYes)
The full version is attached below just for your reference. 以下附有完整版本仅供参考。
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
So what should I do to fix this issue? 那么我该怎么做才能解决这个问题呢? Why is giving an error on that line there?
为什么在那条线上出错?
The following is the solution that I ended up with. 以下是我最终得到的解决方案。 Seems to be solving the issue.
似乎是在解决这个问题。
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
You could call this before adding the table, or modify it to be more specific: 你可以在添加表之前调用它,或者将其修改为更具体:
For Each tbl In Sheets("DataSheet").ListObjects
tbl.Unlist
Next
Note: tbl.Unlist will turn the table into a normal range but leave the data, tbl.Delete will remove a table and will also delete data within the table. 注意:tbl.Unlist会将表转换为正常范围但保留数据,tbl.Delete将删除表,并且还将删除表中的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.