Excel 尝试删除工作表并创建新工作表

Excel 尝试删除工作表并创建新工作表,excel,vba,Excel,Vba,我试着运行这个程序,这样它就可以删除已经存在的工作表,创建一个新的工作表,这样我就可以用结果填充它。我希望每次运行程序时都能这样做,这样我就可以得到一张新的工作表,而不需要以前的结果 Dim CustomerID As Integer Dim SameID As Integer Dim TotalSpent As Currency Dim HighSpenders As Integer Dim CustomerOrder As Integer Dim DataCell As Range Dim

我试着运行这个程序,这样它就可以删除已经存在的工作表,创建一个新的工作表,这样我就可以用结果填充它。我希望每次运行程序时都能这样做,这样我就可以得到一张新的工作表,而不需要以前的结果

Dim CustomerID As Integer
Dim SameID As Integer
Dim TotalSpent As Currency
Dim HighSpenders As Integer
Dim CustomerOrder As Integer
Dim DataCell As Range
Dim ReportCell As Range
Dim UserAmount As Variant
Dim UserAmount1 As Integer
Dim wsData As Worksheet
Dim wsReport As Worksheet

Set wsData = ActiveWorkbook.Sheets("Data")

Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Sheets("Report").Delete
On Error GoTo -1
Application.DisplayAlerts = True

Do

    UserAmount = InputBox("Enter an amount")

    If Not IsNumeric(UserAmount) Then

    MsgBox "Enter a numeric value"

    Else
        UserAmount1 = CInt(UserAmount)
    End If
Loop While Not IsNumeric(UserAmount)

Set wsReport = ActiveWorkbook.Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Report"

Set DataCell = wsData.Range("A3")
Set ReportCell = wsReport.Range("A3")

现在的问题是,它并没有创建一个名为Report的新工作表,而您并没有声明或设置wsData或wsReport。这至少会将wsReport设置为新创建的工作表

Dim CustomerID As Integer, SameCustomerID As Integer
Dim TotalSpent As Currency
Dim HighSpenders As Integer, CustomerOrder As Integer,  UserAmount1 As Integer
Dim DataCell As Range, ReportCell As Range
Dim UserAmount As Variant
dim wsData as worksheet, wsReport as worksheet

application.displayalerts = false    'do NOT ask for confirmation
on error resume next                 'if Reports doesn't exist, keep going
ActiveWorkbook.Sheets("Report").Delete
on error goto -1                     'reset the error handler
application.displayalerts = true     'turn alerts back on

Do    
    UserAmount = InputBox("Enter an amount")

    If Not IsNumeric(UserAmount) Then    
        MsgBox "Enter a numeric value"    
    Else
        UserAmount1 = CInt(UserAmount)
    End If
Loop While Not IsNumeric(UserAmount)

set wsReport = ActiveWorkbook.workSheets.Add(After:=Sheets(Sheets.Count))
with wsReport
    .Name = "Report"
end with

Set ReportCell = wsReport.Range("A3")
'wsData is still not set to any worksheet
Set DataCell = wsData.Range("A3")
错误转到0时删除工作表英尺
  • 如果此代码位于
    活动工作簿中
    ,则应使用
    改为使用此工作簿
    ,或以其名称引用,例如
    工作簿(CreateReport.xlsm)
  • 对对象使用
    With
    语句,使代码更具可读性和可读性 避免不必要的参考错误:

    After
    参数
    After:=Sheets(Sheets.Count)
    的参数部分是不正确的,应该是:

    After:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)

    为什么它仍然可以正常工作?这是因为当省略
    ActiveWorkbook
    时,实际使用的是
    ActiveWorkbook
    (“已理解”、“默认”)。您可以省略所有使用的
    ActiveWorkbook
    引用,并且所有
    工作表
    仍然(正确)引用了
    ActiveWorkbook
    的工作表

    为什么不正确?您已决定将所有
    ActiveWorkbook
    实例更改为
    工作簿(“CreateReport.xlsm”)
    。您可能不会在
    After
    参数中添加引用,因为它引用的是
    ActiveWorkbook
    ,可能是另一个工作簿(不是
    CreateReport.xlsm

    最后一部分介绍了使用
    With
    语句的另一个好处,即如果您想更改工作簿的引用,您只需在
    With
    语句(一次)中更改它,例如:

    与工作簿(“CreateReport.xlsm”)

  • 支持错误转到-1时的
    ,
    做如果你用

    错误转到0时

    代码会产生运行时错误“424”:需要对象,并且会突出显示行
    Set wsReport=…
    ,您会立即知道这是必须更改的行

  • 您可以使用相同的变量
    UserAmount
    (作为
    Variant
    )而不是
    UserAmount1
    。要防止运行时错误“6”:输入时溢出 超过
    整数
    限制的值,例如32768,您应该使用
    Long
    而不是
    Integer

    UserAmount=CLng(UserAmount)

    “或:

    Dim UserAmount1尽可能长

    UserAmount1=Clng(UserAmount)

    如果您坚持使用变量
    UserAmount1

  • 您不能一次添加新工作表并重命名它(以相同的方式) 行)。您必须使用两行:

    With ActiveWorkbook
        Set wsReport = .Sheets.Add(After:=.Sheets(.Sheets.Count))
    End With
    wsReport.Name = "Report"
    
  • 创建标题或简短地描述各种内容是一种很好的做法 代码的各个部分。我可能加的太多了

代码
为什么不干脆清除或清除内容呢?如果你坚持删除报告工作表并在其位置添加新的工作表,您应该在删除前使用application.displayalerts=false,然后在删除后立即使用application.displayalerts=true。该任务要求我删除以前的现有工作表。您没有设置wsData或wsReport。那么问题出在哪里?乍一看,你似乎就是这么做的。
Sub AddSheet()

    Dim CustomerID As Integer
    Dim SameID As Integer
    Dim TotalSpent As Currency
    Dim HighSpenders As Integer
    Dim CustomerOrder As Integer
    Dim DataCell As Range
    Dim ReportCell As Range
    Dim UserAmount As Variant
    'Dim UserAmount1 As Long
    Dim wsData As Worksheet
    Dim wsReport As Worksheet

    ' If this code is in the ActiveWorkbook, use ThisWorkbook instead.
    With ThisWorkbook

        ' Create a reference to Data Sheet.
        Set wsData = .Sheets("Data")

        ' Delete (old) Report Sheet.
        On Error Resume Next
            Application.DisplayAlerts = False
                .Sheets("Report").Delete
            Application.DisplayAlerts = True
        On Error GoTo 0 ' VBA doesn't support On Error Goto -1

        ' Input UserAmount.
        Do
            UserAmount = InputBox("Enter an amount")
            If Not IsNumeric(UserAmount) Then
                MsgBox "Enter a numeric value"
              Else
                ' You can use the same variable.
                ' To prevent "Run-time error '6': Overflow" when entering a
                ' value that exceeds the integer limit e.g. 32768, you have
                ' to use Long.
                UserAmount = CLng(UserAmount)
                'UserAmount1 = CLng(UserAmount)
            End If
        Loop While Not IsNumeric(UserAmount)

        ' Create a reference to a newly added sheet.
        Set wsReport = .Sheets.Add(After:=.Sheets(.Sheets.Count))

    End With

    ' Rename the newly added sheet.
    wsReport.Name = "Report"

    ' Create references to cells "A3" of both worksheets.
    Set DataCell = wsData.Range("A3")
    Set ReportCell = wsReport.Range("A3")

End Sub