Excel VBA如何比较工作表名称数组以打开工作表

时间:2014-05-19 14:39:38

标签: excel vba excel-vba

请原谅我,如果这是一个菜鸟问题,但我花了几个小时抓取这个网站寻求答案。

我正在尝试编写一个宏来循环遍历我的所有工作表并删除所有不在预定义数组中的宏。但是,我很难将数组中的工作表名称与工作簿中的实际名称进行比较。这是我的代码:

    Dim DoNotDelete(10) As Worksheet
    Dim sh As Worksheet
    Dim dnd As Worksheet


    Set DoNotDelete(0) = ThisWorkbook.Worksheets("Home")
    Set DoNotDelete(1) = ThisWorkbook.Worksheets("Global Statistics")
    Set DoNotDelete(2) = ThisWorkbook.Worksheets("Summary")
    Set DoNotDelete(3) = ThisWorkbook.Worksheets("Project Dependencies")
    Set DoNotDelete(4) = ThisWorkbook.Worksheets("Completed Projects")
    Set DoNotDelete(5) = ThisWorkbook.Worksheets("Risk Overview- Yellow")
    Set DoNotDelete(6) = ThisWorkbook.Worksheets("Issue Overview- Red")
    Set DoNotDelete(7) = ThisWorkbook.Worksheets("Issue Overview- Red")
    Set DoNotDelete(8) = ThisWorkbook.Worksheets("Dependencies")
    Set DoNotDelete(9) = ThisWorkbook.Worksheets("Completed Data")
    Set DoNotDelete(10) = ThisWorkbook.Worksheets("Data")

    For Each sh In Worksheets
        Delete = False
        For Each dnd In DoNotDelete
            If dnd = sh Then
                Delete = False
                Exit For
            Else
                Delete = True
            End If
        Next dnd

        If Delete = True Then
            ThisWorkbook.Worksheets(sh).Delete
        End If

    Next sh

每次出错都会出错:

If dnd = sh then

我正在使用Excel 2007(不要问......)。任何建议将不胜感激!

3 个答案:

答案 0 :(得分:4)

您必须使用is关键字

检查引用相等性
If dnd is sh Then
    Delete = False
    Exit For
Else
    Delete = True
End If

另一种方法,比较识别或唯一属性:

If dnd.Name = sh.Name Then
    Delete = False
    Exit For
Else
    Delete = True
End If

答案 1 :(得分:1)

尝试使用

If dnd.Name = sh.Name Then
    -- stuff here
End If

答案 2 :(得分:1)

这个怎么样?评论中的解释

Sub DeleteWorksheets()
Dim ws As Worksheet 'Used to loop through all worksheets in workbook
Dim ArrayElement As Variant 'Used to loop through all elements in the array
Dim DoNotDelete(0 To 10) As String 'Used to store NAMES of worksheets rather than objects
Dim Found As Boolean 'Used to test whether or not the worksheet in found in the array

'Store values as strings
DoNotDelete(0) = "Home"
DoNotDelete(1) = "Global Statistics"
DoNotDelete(2) = "Summary"
DoNotDelete(3) = "Project Dependencies"
DoNotDelete(4) = "Completed Projects"
DoNotDelete(5) = "Risk Overview- Yellow"
DoNotDelete(6) = "Issue Overview- Red"
DoNotDelete(7) = "Issue Overview- Red"
DoNotDelete(8) = "Dependencies"
DoNotDelete(9) = "Completed Data"
DoNotDelete(10) = "Data"

For Each ws In Worksheets 'For every worksheet in this workbook
    Found = False 'Reset Found value to false
    For Each ArrayElement In DoNotDelete 'Check if worksheet name is found in array
        If ws.Name = ArrayElement Then 'If it is, set Found to true and exit the loop
            Found = True
            Exit For
        End If
    Next ArrayElement
    If Found = False Then 'If worksheet name isn't in the array, delete it
        ws.Delete
    End If
Next ws

End Sub