更改工作表中的所有数据表名称

时间:2018-07-13 10:09:21

标签: excel vba excel-vba

我有一个品牌的7个工作表。我有10个品牌 所有工作表都包含多个具有精确名称的数据表 所有表格均以“ brand_”开头

我做了一个宏来复制所有品牌的所有工作表(产生70个工作表),效果很好。 但是,当我复制工作表时,所有表都被命名为“ brand _” +表名+ 3位数字,其中“ brand_”与原始表具有相同的品牌。 在我的宏中,我遍历所有品牌,因此我始终知道我正在处理哪个品牌。 复制工作表时,是否有办法更改所有表名,而不必手动更改所有表名?

Sub copy()
Dim all_brands As Variant
Dim all_tabs As Variant
Dim brand As Variant
Dim sheettocopy As Variant
Dim OLDsheet As String
Dim NEWsheet As String
all_brands = Array("xxx", "yyy", "jjj", "zzz", "ppp", "qqq", "vvv", "bbb")
all_tabs = Array("tab1", "tab2", "tab3", "tab4", "tab5", "tab6", "tab7")

For Each brand In all_brands
   For Each sheettocopy In all_tabs
       OLDsheet = "MMM" & sheettocopy
       Sheets(OLDsheet).copy after:=Sheets(OLDsheet)
       NEWsheet = brand & sheettocopy
       ActiveSheet.Name = NEWsheet
   Next sheettocopy
Next brand

End Sub

1 个答案:

答案 0 :(得分:1)

问题解决了,这是我的解决方案:

For Each brand In all_brands
    For Each sheettocopy In all_tabs
    OLDsheet = "MMM" & sheettocopy
    Sheets(OLDsheet).copy after:=Sheets(OLDsheet)
    NEWsheet = brand & sheettocopy
    ActiveSheet.name = NEWsheet

        For Each tbl In Worksheets(NEWsheet).ListObjects
        name = tbl.name
        length = Len(name) - 6
        newname = brand & Mid(name, 4, length)
        tbl.name = newname
        Next tbl

Next sheettocopy
Next brand