创建新工作表并在其中创建表

时间:2014-12-02 20:10:16

标签: excel vba excel-vba

我想创建新工作表,命名新工作表,在工作表中添加列,然后在每个新电子表格中创建一个表,所有这些都在一个工作簿中,该工作簿中有一个工作表开头。我的代码不起作用,但到目前为止看看我在哪里:

Sub Create_Sheets()

Sheets.Add.Name = "VA_NAME"
Sheets.Add.Name = "VA_VALUE"
Sheets.Add.Name = "CE_NAME"
Sheets.Add.Name = "CE_VALUE"

Dim Table As ListObject
Set Table = Sheet1.ListObjects("VA_NAME")

Table.ListColumns.Add 1
Table.HeaderRowRange(1) = "SOURCE_SEQ_NBR"
Table.ListColumns.Add 2
Table.HeaderRowRange(2) = "L1_PARCEL_NBR"
Table.ListColumns.Add 3
Table.HeaderRowRange(3) = "L1_ATTR_TEMP_NAME"
Table.ListColumns.Add 4
Table.HeaderRowRange(4) = "L1_ATTR_NAME"
Table.ListColumns.Add 5
Table.HeaderRowRange(5) = "L1_ATTR_VALUE"

Set Table = Sheet1.ListObjects("VA_VALUE")
Table.ListColumns.Add 1
Table.HeaderRowRange(1) = "SOURCE_SEQ_NBR"
Table.ListColumns.Add 2
Table.HeaderRowRange(2) = "L1_PARCEL_NBR"
Table.ListColumns.Add 3
Table.HeaderRowRange(3) = "L1_ATTR_TEMP_NAME"
Table.ListColumns.Add 4
Table.HeaderRowRange(4) = "L1_ATTR_NAME"
Table.ListColumns.Add 5
Table.HeaderRowRange(5) = "L1_ATTR_VALUE"

Set Table = Sheet1.ListObjects("CE_NAME")
Table.ListColumns.Add 1
Table.HeaderRowRange(1) = "SOURCE_SEQ_NBR"
Table.ListColumns.Add 2
Table.HeaderRowRange(2) = "L1_PARCEL_NBR"
Table.ListColumns.Add 3
Table.HeaderRowRange(3) = "L1_ATTR_TEMP_NAME"
Table.ListColumns.Add 4
Table.HeaderRowRange(4) = "L1_ATTR_NAME"
Table.ListColumns.Add 5
Table.HeaderRowRange(5) = "L1_ATTR_VALUE"

Set Table = Sheet1.ListObjects("CE_VALUE")
Table.ListColumns.Add 1
Table.HeaderRowRange(1) = "SOURCE_SEQ_NBR"
Table.ListColumns.Add 2
Table.HeaderRowRange(2) = "L1_PARCEL_NBR"
Table.ListColumns.Add 3
Table.HeaderRowRange(3) = "L1_ATTR_TEMP_NAME"
Table.ListColumns.Add 4
Table.HeaderRowRange(4) = "L1_ATTR_NAME"
Table.ListColumns.Add 5
Table.HeaderRowRange(5) = "L1_ATTR_VALUE"

Columns.AutoFit

我希望代码识别工作表名称(即“VA_NAME”)而不是“Sheet2,Sheet3”等。我知道“Set Table = Sheet1.ListObjects(”VA_NAME“)”很可能是我的问题,因为这依赖于“Sheet(X)”约定,但是做Sheets(“VA_NAME”)。ListObjects不起作用。有没有快速解决这个问题,还是我要求反对VBA的规则?

谢谢,全部!

更新:我发现我发布的代码假设表已经存在,情况并非如此。我添加了新代码来创建实际表,但没有命名列:

Sheet2.ListObjects.Add(xlSrcRange, Range("$A$1"), , xlNo).Name = "VA_NAME"
Sheet3.ListObjects.Add(xlSrcRange, Range("$A$1"), , xlNo).Name = "VA_VALUE"
Sheet4.ListObjects.Add(xlSrcRange, Range("$A$1"), , xlNo).Name = "CE_NAME"
Sheet5.ListObjects.Add(xlSrcRange, Range("$A$1"), , xlNo).Name = "CE_VALUE"

我的操作顺序应该是创建命名表 - >创建命名列 - >从列创建表。

2 个答案:

答案 0 :(得分:0)

虽然我怀疑这是创建具有命名表和列的命名工作表的最直观方式,但这就是我所做的(我仍然对建议非常开放):

Sub Create_Sheets()

Sheets.Add.Name = "VA_NAME"
Sheets.Add.Name = "VA_VALUE"
Sheets.Add.Name = "CE_NAME"
Sheets.Add.Name = "CE_VALUE"

End Sub

Sub Create_PARCEL_ATTR_Tables()

Sheets("VA_NAME").Select
Sheet2.ListObjects.Add(xlSrcRange, Range("$A$1:$E$1"), , xlNo).Name = "VA_NAME"
Sheets("VA_VALUE").Select
Sheet3.ListObjects.Add(xlSrcRange, Range("$A$1:$E$1"), , xlNo).Name = "VA_VALUE"
Sheets("CE_NAME").Select
Sheet4.ListObjects.Add(xlSrcRange, Range("$A$1:$E$1"), , xlNo).Name = "CE_NAME"
Sheets("CE_VALUE").Select
Sheet5.ListObjects.Add(xlSrcRange, Range("$A$1:$E$1"), , xlNo).Name = "CE_VALUE"

End Sub

Sub Create_PARCEL_ATTR_COLUMNS()

Sheets("VA_NAME").Range("A1").Value = "SOURCE_SEQ_NBR"
Sheets("VA_NAME").Range("B1").Value = "L1_PARCEL_NBR"
Sheets("VA_NAME").Range("C1").Value = "L1_ATTRIB_TEMP_NAME"
Sheets("VA_NAME").Range("D1").Value = "L1_ATTRIB_NAME"
Sheets("VA_NAME").Range("E1").Value = "L1_ATTRIB_VALUE"
Sheets("VA_NAME").Columns.AutoFit

Sheets("VA_VALUE").Range("A1").Value = "SOURCE_SEQ_NBR"
Sheets("VA_VALUE").Range("B1").Value = "L1_PARCEL_NBR"
Sheets("VA_VALUE").Range("C1").Value = "L1_ATTRIB_TEMP_NAME"
Sheets("VA_VALUE").Range("D1").Value = "L1_ATTRIB_NAME"
Sheets("VA_VALUE").Range("E1").Value = "L1_ATTRIB_VALUE"
Sheets("VA_VALUE").Columns.AutoFit

Sheets("CE_NAME").Range("A1").Value = "SOURCE_SEQ_NBR"
Sheets("CE_NAME").Range("B1").Value = "L1_PARCEL_NBR"
Sheets("CE_NAME").Range("C1").Value = "L1_ATTRIB_TEMP_NAME"
Sheets("CE_NAME").Range("D1").Value = "L1_ATTRIB_NAME"
Sheets("CE_NAME").Range("E1").Value = "L1_ATTRIB_VALUE"
Sheets("CE_NAME").Columns.AutoFit

Sheets("CE_VALUE").Range("A1").Value = "SOURCE_SEQ_NBR"
Sheets("CE_VALUE").Range("B1").Value = "L1_PARCEL_NBR"
Sheets("CE_VALUE").Range("C1").Value = "L1_ATTRIB_TEMP_NAME"
Sheets("CE_VALUE").Range("D1").Value = "L1_ATTRIB_NAME"
Sheets("CE_VALUE").Range("E1").Value = "L1_ATTRIB_VALUE"
Sheets("CE_VALUE").Columns.AutoFit

谢谢,全部!

答案 1 :(得分:0)

如果您对上述代码进行编码,则会缩短它并减少繁琐。

Sub Create_PARCEL_Stuff()

Sheets.Add.Name = "VA_NAME"
Sheets.Add.Name = "VA_VALUE"
Sheets.Add.Name = "CE_NAME"
Sheets.Add.Name = "CE_VALUE"
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name = "NAME OF YOUR SHEET WITH DATA" Then
            'Do Nothing
        Else
            ws.ListObjects.Add(xlSrcRange, Sheets("Name of source sheet").Range("$A$1:$E$1"), , xlNo).Name = ws.Name
            ws.Range("A1").Value = "SOURCE_SEQ_NBR"
            ws.Range("B1").Value = "L1_PARCEL_NBR"
            ws.Range("C1").Value = "L1_ATTRIB_TEMP_NAME"
            ws.Range("D1").Value = "L1_ATTRIB_NAME"
            ws.Range("E1").Value = "L1_ATTRIB_VALUE"
            ws.Columns.AutoFit
        End If
    Next ws
End Sub