Vba 代码中的nts指的是本工作簿,而不是变量wb。虽然在某些情况下可能会有这样的原因,但通常我会说选择其中一种,但不要混合。快速测试-只需尝试工作表(“TestCase\u模板”)。在之后复制:=工作表(“可跟踪矩阵”),看看这是否有效?当我使用工作表(“Te

Vba 代码中的nts指的是本工作簿,而不是变量wb。虽然在某些情况下可能会有这样的原因,但通常我会说选择其中一种,但不要混合。快速测试-只需尝试工作表(“TestCase\u模板”)。在之后复制:=工作表(“可跟踪矩阵”),看看这是否有效?当我使用工作表(“Te,vba,excel,runtime-error,Vba,Excel,Runtime Error,代码中的nts指的是本工作簿,而不是变量wb。虽然在某些情况下可能会有这样的原因,但通常我会说选择其中一种,但不要混合。快速测试-只需尝试工作表(“TestCase\u模板”)。在之后复制:=工作表(“可跟踪矩阵”),看看这是否有效?当我使用工作表(“TestCase\u模板”)。在之后复制:=工作表(“可跟踪矩阵”)我遇到同样的自动化错误,excel崩溃。如果我只做工作表(“TestCase_模板”)。复制工作表(“可追溯性矩阵”)我会得到“运行时错误”-2147417848(80010108


代码中的nts指的是
本工作簿
,而不是变量
wb
。虽然在某些情况下可能会有这样的原因,但通常我会说选择其中一种,但不要混合。快速测试-只需尝试
工作表(“TestCase\u模板”)。在之后复制:=工作表(“可跟踪矩阵”)
,看看这是否有效?当我使用
工作表(“TestCase\u模板”)。在之后复制:=工作表(“可跟踪矩阵”)
我遇到同样的自动化错误,excel崩溃。如果我只做
工作表(“TestCase_模板”)。复制工作表(“可追溯性矩阵”)
我会得到“运行时错误”-2147417848(80010108):“对象的方法”“复制”“工作表”“失败”。但Excel不会崩溃@brucewayned是否有此帮助?这似乎让我想起了几年前在Excel 2003中复制工作表时的记忆错误。我认为不需要保存/关闭/重新打开该文件,但可能需要定期保存该文件。另外,在过程结束时调用
屏幕更新
后,尝试将
卸载我
移动到。只是一个想法。另外,您已经分配了
Set wb=thiswoolk
,但代码中的许多点都指向
thiswoolk
,而不是变量
wb
。虽然在某些情况下可能会有这样的原因,但我通常会说,选择其中一种,但不要混合。哇!我真的很感谢你的帮助。关于删除
卸载我
的好建议。谢谢你让事情变得复杂一点!如果修复了错误,我会再次发布。所以这段代码几乎解决了我的问题。当我保持userform按钮打开(由您实现)以创建一张又一张的工作表时,不会出现错误,文件也不会崩溃。但是,如果我创建了超过22个工作表,然后手动关闭userfrom,重新打开它,并使用它创建另一个工作表,那么我会再次出现自动错误。有什么想法吗?@Ryan并不特别:(你可以尝试两件事:在程序开始时使用
Application.calculation=xlCalculationManual
在运行时运行并禁用计算。请记住在退出子系统之前将其设置回
xlCalculationAutomatic
。@Ryan你还可以为其显示代码吗“该按钮显示用户窗体"?我不怀疑这是一个问题,但你永远不会知道,可能有不同的方式来处理该表单。兼容性不起作用。按钮的整个代码只是
TestCaseUserForm.Show
。我想如果我能弄清楚为什么我的project explorer显示我不存在的工作簿,我会找到问题的根源没有创建。哇!我真的很感谢你的帮助。关于删除
卸载我
的呼吁很好。感谢你让事情变得更复杂一点!如果它修复了错误,我会再次发布。因此,这段代码几乎解决了我的问题。当我打开userform按钮时(由你实现)创建一张又一张的工作表时,不会出现错误,文件也不会崩溃。但是,如果我创建了22张以上的工作表,然后手动关闭userfrom,重新打开它,并使用它创建另一张工作表,我会再次出现自动错误。有什么想法吗?@Ryan,特别是:(您可以尝试两件事:在程序开始时使用
Application.calculation=xlCalculationManual
在运行时运行并禁用计算。请记住在退出sub之前将其设置回
xlCalculationAutomatic
。@Ryan您是否也可以显示“按钮显示用户表单”的代码?我不怀疑这是一个问题,但你永远不会知道,可能有不同的方式来处理该表单。兼容性不起作用。按钮的整个代码只是
TestCaseUserForm.Show
。我想如果我能弄清楚为什么我的project explorer显示我不存在的工作簿,我会找到问题的根源没有创建。
templateSheet.Copy After:=indexSheet
    wb.Save
    Application.OnTime Now + TimeValue("00:00:01"), Application.Workbooks.Open(filePath)
    wb.Close (True)
Private Sub OkButton_Click()

'Dont update the screen while the macro runs
Application.ScreenUpdating = False

    'Sheet and workbook variables
    Dim wb As Workbook
    Dim indexSheet As Worksheet, templateSheet As Worksheet
    Dim templateCopy As Worksheet, newSheet As Worksheet

    'Table and new row variables
    Dim Tbl As ListObject
    Dim NewRow As ListRow

    'Variables to group shapes based on
    'need to hide or show them
    Dim hideShapes() As Variant, showShapes() As Variant
    Dim hideGroup As Object, showGroup As Object

    'Misc variables
    Dim i As Integer
    Dim exists As Boolean
    Dim filePath As String

    'Variables to assign ranges
    Dim scenarioRng As Range
    Dim traceabilityFocus As Range
    Dim testCaseRng As Range
    Dim statusRng As Range
    Dim newSheetTestCaseRng As Range
    Dim newSheetStatusRng As Range
    Dim newSheetFocus As Range
    Dim newSheetDateRng As Range

    'Create array of shapes based on visibility rules
    hideShapes = Array("TextBox 2", "Rectangle 1")
    showShapes = Array("TextBox 15", "TextBox 14", "TextBox 13", "TextBox 11", "StatsRec", "Button 10")

    'To reference Traceability Matrix sheet
    Set indexSheet = ThisWorkbook.Sheets("Traceability Matrix")
    'To reference Template sheet
    Set templateSheet = ThisWorkbook.Sheets("TestCase_Template")
    'To reference traceability matrix table
    Set Tbl = indexSheet.ListObjects("TMatrix")
    'Set hideShapes to a hide group
    Set hideGroup = indexSheet.Shapes.Range(hideShapes)
    'Set show shapes to a show group
    Set showGroup = indexSheet.Shapes.Range(showShapes)
    'To reference this workbook
    Set wb = ThisWorkbook
    'Get file path of this workbook and set it to string
    filePath = wb.FullName


    'If the userform fields are empty then show error message
    If ScenarioNameBox.Value = "" Or TestCaseNameBox.Text = "" Then
            MsgBox ("Please complete both fields.")
    'If the userform fields are completed and a worksheet with
    'the same name exists, set boolean to true
    Else
        For i = 1 To Worksheets.Count
        If ThisWorkbook.Worksheets(i).Name = TestCaseNameBox.Value Then
            exists = True
    End If
    'Iterate through all worksheets
    Next i

    'If test case name already exists, show error message
    If exists Then
        MsgBox ("This test case name is already in use. Please choose another name.")
    'If test case name is unique, update workbook
    Else
        'Copy template sheet to after traceability matrix sheet
        templateSheet.Copy After:=indexSheet 'LOCATION OF ERROR!!!
        'Ensure template sheet is hidden
        templateSheet.Visible = False

        'To reference copy of template
        Set templateCopy = ThisWorkbook.Sheets("TestCase_Template (2)")

        'Rename template sheet to the test case name
        templateCopy.Name = TestCaseNameBox.Value
        'To reference re-named template sheet
        Set newSheet = ThisWorkbook.Sheets(TestCaseNameBox.Value)
        'Show new sheet
        newSheet.Visible = True

        'Set focus to traceability matrix
        Set traceabilityFocus = indexSheet.Range("A1")

        'Add a new row
        Set NewRow = Tbl.ListRows.Add(AlwaysInsert:=True)

        'Set ranges for cells in traceability table
        Set scenarioRng = indexSheet.Range("B" & NewRow.Range.Row)
        Set testCaseRng = scenarioRng.Offset(0, 1)
        Set statusRng = testCaseRng.Offset(0, 1)

        'Set scenario cell with name and format
        With scenarioRng
            .FormulaR1C1 = ScenarioNameBox.Value
            .HorizontalAlignment = xlGeneral
            .Font.Name = "Arial"
            .Font.Size = 12
        End With

        'Set test case cell with name, hyperlink to sheet, and format
        With testCaseRng
            .FormulaR1C1 = TestCaseNameBox.Value
            .Hyperlinks.Add Anchor:=testCaseRng, Address:="", SubAddress:=newSheet.Name & "!A1", TextToDisplay:=newSheet.Name
            .HorizontalAlignment = xlGeneral
            .Font.Name = "Arial"
            .Font.Size = 12
        End With

        'Set trial status as Incomplete and format
        With statusRng
            'Set new test case to "Incomplete"
            .Value = "Incomplete"
            .Font.Name = "Arial"
            .Font.Size = 12
            .Font.Color = vbBlack
        End With

        'Show or hide objects
        hideGroup.Visible = False
        showGroup.Visible = True

        'Set ranges for cells in test case table
        Set newSheetTestCaseRng = newSheet.Range("C2")
        Set newSheetStatusRng = newSheet.Range("C12")
        Set newSheetDateRng = newSheet.Range("C5")

        'Insert test case name into table
        newSheetTestCaseRng.Value = TestCaseNameBox.Value
        'Add todays date to Date Created
        newSheetDateRng.Value = Date
        'Set status to "Incomplete"
        newSheetStatusRng.Value = "Incomplete"
        'End with cursor at beginning of table
        newSheet.Activate
        Range("C3").Activate


        'wb.Save
        'Application.OnTime Now + TimeValue("00:00:01"), Application.Workbooks.Open(filePath)
        'wb.Close (True)


        'Close the userform
        Unload Me

        End If

    End If

    'Update screen
    Application.ScreenUpdating = True

End Sub
 With templateSheet
        .Visible = xlSheetVisible
        .Copy Before:=indexSheet 'ERRORS HERE!!
        .Visible = xlSheetVeryHidden
Option Explicit
Private Sub OkButton_Click()

'Dont update the screen while the macro runs
Application.ScreenUpdating = False

    'Sheet and workbook variables
    Dim wb As Workbook
    Dim indexSheet As Worksheet, templateSheet As Worksheet
    Dim templateCopy As Worksheet, newSheet As Worksheet

    'Table and new row variables
    Dim Tbl As ListObject
    Dim NewRow As ListRow

    'Variables to group shapes based on
    'need to hide or show them
    Dim hideShapes() As Variant, showShapes() As Variant
    Dim hideGroup As Object, showGroup As Object

    'Misc variables
    Dim i As Integer
    Dim exists As Boolean
    Dim filePath As String

    'Variables to assign ranges
    Dim scenarioRng As Range
    Dim traceabilityFocus As Range
    Dim testCaseRng As Range
    Dim statusRng As Range
    Dim newSheetTestCaseRng As Range
    Dim newSheetStatusRng As Range
    Dim newSheetFocus As Range
    Dim newSheetDateRng As Range

    'Create array of shapes based on visibility rules
    hideShapes = Array("TextBox 2", "Rectangle 1")
    showShapes = Array("TextBox 15", "TextBox 14", "TextBox 13", "TextBox 11", "StatsRec", "Button 10")
    'To reference this workbook
    Set wb = ThisWorkbook
    'To reference Traceability Matrix sheet
    Set indexSheet = wb.Sheets("Traceability Matrix")
    'To reference Template sheet
    Set templateSheet = wb.Sheets("TestCase_Template")
    'To reference traceability matrix table
    Set Tbl = indexSheet.ListObjects("TMatrix")
    'Set hideShapes to a hide group
    Set hideGroup = indexSheet.Shapes.Range(hideShapes)
    'Set show shapes to a show group
    Set showGroup = indexSheet.Shapes.Range(showShapes)
    'Get file path of this workbook and set it to string
    filePath = wb.FullName

    'If the userform fields are empty then show error message
    If ScenarioNameBox.Value = "" Or TestCaseNameBox.Text = "" Then
            MsgBox "Please complete both fields."
            GoTo EarlyExit
    'If the userform fields are completed and a worksheet with
    'the same name exists, set boolean to true
    Else
        On Error Resume Next
        Dim tmpWS As Worksheet
        ' This will error if sheet doesn't exist
        Set tmpWS = wb.Worksheets(TestCaseNameBox.Value)
        exists = Not (tmpWS Is Nothing)
        On Error GoTo 0
    End If

    'If test case name already exists, show error message
    If exists Then
        MsgBox "This test case name is already in use. Please choose another name."
        GoTo EarlyExit
    'If test case name is unique, update workbook
    Else
        'Copy template sheet to after traceability matrix sheet
        With templateSheet
            .Visible = xlSheetVisible
            .Copy Before:=indexSheet
            .Visible = xlSheetVeryHidden
        End With
        Set newSheet = wb.Sheets(indexSheet.Index - 1)
        With newSheet
            newSheet.Move After:=indexSheet
            'Rename template sheet to the test case name
            .Name = TestCaseNameBox.Value
            'To reference re-named template sheet
            .Visible = True
            'Set ranges for cells in test case table
            Set newSheetTestCaseRng = .Range("C2")
            Set newSheetStatusRng = .Range("C12")
            Set newSheetDateRng = .Range("C5")

            'Insert test case name into table
            newSheetTestCaseRng.Value = TestCaseNameBox.Value
            'Add todays date to Date Created
            newSheetDateRng.Value = Date
            'Set status to "Incomplete"
            newSheetStatusRng.Value = "Incomplete"
            'End with cursor at beginning of table
            .Activate
            .Range("C3").Activate
        End With

        'Set focus to traceability matrix
        Set traceabilityFocus = indexSheet.Range("A1")
        'Add a new row
        Set NewRow = Tbl.ListRows.Add(AlwaysInsert:=True)
        'Set ranges for cells in traceability table
        Set scenarioRng = indexSheet.Range("B" & NewRow.Range.Row)
        Set testCaseRng = scenarioRng.Offset(0, 1)
        Set statusRng = testCaseRng.Offset(0, 1)

        'Set scenario cell with name and format
        With scenarioRng
            .FormulaR1C1 = ScenarioNameBox.Value
            .HorizontalAlignment = xlGeneral
            .Font.Name = "Arial"
            .Font.Size = 12
        End With

        'Set test case cell with name, hyperlink to sheet, and format
        With testCaseRng
            .FormulaR1C1 = TestCaseNameBox.Value
            .Hyperlinks.Add Anchor:=testCaseRng, Address:="", SubAddress:=newSheet.Name & "!A1", TextToDisplay:=newSheet.Name
            .HorizontalAlignment = xlGeneral
            .Font.Name = "Arial"
            .Font.Size = 12
        End With

        'Set trial status as Incomplete and format
        With statusRng
            'Set new test case to "Incomplete"
            .Value = "Incomplete"
            .Font.Name = "Arial"
            .Font.Size = 12
            .Font.Color = vbBlack
        End With

        'Show or hide objects
        hideGroup.Visible = False
        showGroup.Visible = True

        wb.Save
    End If

EarlyExit:
    'Update screen
    Application.ScreenUpdating = True

End Sub