除Sheet1和Sheet2之外,在每张工作表上运行宏

时间:2016-08-02 12:10:19

标签: excel-vba vba excel

以下是我要做的宏:

在除sheet1和sheet2之外的每张工作表上运行宏

dim rs as worksheet

表格中的每个rs     如果rs.name =" Sheet1"或" Sheet2"然后     下一个rs 否则rs.name = rs.range("

我是VBA的新手,而且我一直没有获得#'错误。不知道如何下一个for循环和if语句。提前谢谢

            Dim rs As Worksheet

            For Each rs In Sheets
               If rs.Name = "sheet1" Or "sheet2" Then
            Next ws
                Else: rs.Name = rs.Range("N2") 'rename sheet base on vendor name field

               End If
            Next rs

1 个答案:

答案 0 :(得分:5)

小混淆,试试:

Dim rs As Worksheet

For Each rs In ThisWorkbook.Worksheets
   If rs.Name <> "Sheet1" And rs.Name <> "Sheet2" Then
        rs.Name = rs.Range("N2").Value 'rename sheet base on vendor name field
   End If
Next rs

End Sub