为什么删除工作表导致我的代码停止

时间:2018-01-04 10:02:26

标签: excel vba excel-vba

我在这里有一些代码(下面)我需要清除工作表“数据输入”中的数据,可能还有其他更快的方法,但是我选择了删除和重制。请随时说出来。

Private Sub CommandButton1_Click()

Dim DataEntryWs As Worksheet

For i = Worksheets.Count To 1 Step -1

If Worksheets(i).Name = "Data Entry" Then

    Application.DisplayAlerts = False
    Worksheets("Data Entry").Delete
    MsgBox ("Sheet Deleted")
    Set DataEntryWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    DataEntryWs.Name = "Data Entry"
    Call Data_Entry_Calcs

Else

    If i = Worksheets.Count Then

        MsgBox ("Adding new sheets now")
        Set DataEntryWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        DataEntryWs.Name = "Data Entry"
        Call Data_Entry_Calcs

    Else

    End If

End If

Next i

Call Data_Entry_Calcs

End Sub

每当我运行代码时,如果名为“数据条目”的工作表存在,那么当代码到达此行Worksheets("Data Entry").Delete时,代码会中断并且不会继续。为什么会这样?现在一直在讨厌我。

我已尝试向前和向后运行For循环,看看这是否有所改变,但是没有成功。

4 个答案:

答案 0 :(得分:2)

你能试试吗?如果我了解您正在尝试正确执行的操作,则不需要循环来执行此操作

Private Sub CommandButton1_Click()
    Dim DataEntryWs As Worksheet

    ' Set Sheet want to test to variable
    ' We use error handling in case it doesn't exist. If it doesn't exists DataEntryWs = nothing
    On Error Resume Next
    Set DataEntryWs = ThisWorkbook.Worksheets("Data Entry")
    On Error GoTo 0

    ' Test if sheet exists. If does Delete
    If Not DataEntryWs Is Nothing Then
        Application.DisplayAlerts = False
        DataEntryWs.Delete
        Application.DisplayAlerts = True
        MsgBox "Sheet Deleted"
    End If

    ' Add new sheet
    MsgBox "Adding new sheets now"
    With ThisWorkbook
        Set DataEntryWs = .Sheets.Add(after:=.Sheets(.Sheets.Count))
    End With
    DataEntryWs.Name = "Data Entry"

    Call Data_Entry_Calcs
End Sub

答案 1 :(得分:2)

如何清除该工作表的内容而不是删除它,例如:

#include<stdio.h>
#include<string.h>

int to_integer(char *);

int main(int argc, char **argv){

    int sum = 0;
    for(int i=0; i<argc; i++){
        sum += to_integer(argv[i]);
    }
    printf("%d\n", sum);


    return 0;
}

int to_integer(char *str){
    int num=0;
    for(int i=0; i<strlen(str); i++){
        num = num*10 + ( str[i] - '0' );
    }
    return num;
}

答案 2 :(得分:1)

你能试试这段代码,让我知道会发生什么,是的,当我运行它时,我确实得到了消息框......

        Sub Test2()
        Dim i As Integer

            On Error GoTo err_handler

            For i = Worksheets.Count To 1 Step -1

                If Worksheets(i).Name = "Data Entry" Then

                    Application.DisplayAlerts = False
                    Worksheets("Data Entry").Delete
                    Application.DisplayAlerts = True

                    MsgBox ("Sheet Deleted")

                End If

            Next i

            Exit Sub

err_handler:
        MsgBox Err.Description

        End Sub

答案 3 :(得分:1)

为什么不尝试这个?

Private Sub CommandButton1_Click()
Dim DataEntryWs As Worksheet
    On Error Resume Next
    Set DataEntryWs = Sheets("Data Entry")
    On Error GoTo 0

    If Not DataEntryWs Is Nothing Then
        DataEntryWs.Cells.Clear
        MsgBox "Sheet Data Entry cleared.", vbInformation
    Else
        MsgBox "Adding new sheet now.", vbInformation
        With ThisWorkbook
            Set DataEntryWs = .Sheets.Add(after:=.Sheets(.Sheets.Count))
            DataEntryWs.Name = "Data Entry"
        End With
    End If
    DataEntryWs.Activate
    Call Data_Entry_Calcs
End Sub