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

时间:2019-02-16 05:20:48

标签: 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 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")

现在的问题是它没有创建一个新的工作表,即带有结果的报告

2 个答案:

答案 0 :(得分:1)

删除页英尺错误时转到0

  • 如果此代码位于ActiveWorkbook中,则应使用 代替ThisWorkbook,或使用其名称来引用它,例如Workbooks(CreateReport.xlsm)
  • 对对象使用With语句以使代码更易读和 避免不必要的参考错误:

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

      

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

    为什么它仍然可以正常工作?这是因为省略ActiveWorkbook时,实际上会使用ActiveWorkbook(“了解”,“默认”)。您可能已经省略了所有使用的ActiveWorkbook参考,所有Sheets仍(正确)参考了ActiveWorkbook的图纸。

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

    最后一部分为我们带来了使用With语句的另一个好处,即,如果您想更改工作簿的引用,则只需在With语句中进行更改(< strong>一次),例如:

      

    With Workbooks("CreateReport.xlsm")

  • VBA 不支持On Error Goto -1Visual Basic 做。如果您会使用

      

    On Error Goto 0

    该代码将产生Run-time error '424': Object required并突出显示Set wsReport = ...行,您将立即知道这是必须更改的行。

  • 您可以使用相同的变量UserAmount(与Variant)代替 UserAmount1。为了防止输入Run-time error '6': Overflow 超过Integer限制的值,例如32768,您应该使用 Long而非Integer

      

    UserAmount = CLng(UserAmount)

    '或:

    Dim UserAmount1 as Long

    ...

    UserAmount1 = Clng(UserAmount)

    如果您坚持使用变量UserAmount1

  • 您不能一次性添加新工作表并重命名(在同一时间) 线)。您必须使用两行:

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

代码

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

答案 1 :(得分:0)

您没有声明或设置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")