【发布时间】:2019-12-28 10:30:03
【问题描述】:
我正在尝试在七个工作表中的每一个上创建一个表格。每张纸都是一个领土;表格的第一列是该地区的一家公司,随后的每一列都是出售给该公司的产品。
虽然我想要的所有数据都是在创建表之前选择的,但“创建表”代码只包括前两列。
我尝试添加以下内容来解决此问题,但出现错误:
ActiveSheet.ListObjects.Add(xlSrcRange, Range([#All]), , xlYes).Name = _
"Table1"
为什么只包含两列?
Sub Find_Em()
'
' Find_Em Macro
'
Dim Salesman(7) As String
Salesman(0) = "Sally"
Salesman(1) = "Sandra"
Salesman(2) = "Susan"
Salesman(3) = "Suki"
Salesman(4) = "Samantha"
Salesman(5) = "Sutra"
Salesman(6) = "Sherly"
i = 4
Tbl_Name = Salesman(i) & "_QB_Table"
'a clumsy way to find the data I actually want to put into the new table
Cells(3, 150).Select
Selection.End(xlToLeft).Select
Last_Column = ActiveCell.Column
Range(Cells(3, 12), Cells(3, Last_Column)).Select
Range(Selection, Selection.End(xlDown)).Select
' the following actually makes the table. Although many columns are "selected" on
' the left-most 2 columns become part of the new table.
ActiveSheet.ListObjects.Add(SourceType:=xlSrcRange, _
Source:=Selection.CurrentRegion, _
xlListObjectHasHeaders:=xlYes _
).Name = Tbl_Name
End Sub
【问题讨论】:
-
你应该尽量避免使用select。这是不推荐的情况之一。如果不了解数据的结构,就很难提出替代方案。您可以edit您的问题并发布一些示例数据的屏幕截图。
-
谢谢里卡多。我正在尝试将数据放入表中,因此将来不必使用 select;我可以参考表格。但我想我找到了答案。我使用了“CurrentRegion”,并且有一个完整的空白列。我很快就会删除这个问题 - 只是想让你看到这个并说声谢谢!
-
很高兴你能解决它!这里是some reference 如何避免选择