Excel 2016:活动工作表不可见

时间:2016-08-06 14:09:47

标签: excel-vba excel-2016 vba excel

我有一个Excel宏,可以创建另一个供用户修改的工作簿。但是,当我尝试在新工作簿中的单元格中输入数据时,我收到错误"您尝试更改的单元格或图表位于受保护的工作表中。"事实上,宏工作簿中的工作表受到保护,但在我收到错误时该工作表不可见。当我尝试关闭可见的,新创建的工作簿时,它是关闭的宏工作簿。我的宏做的最后一件事是激活新工作簿中的工作表。我该怎么做才能做到这一点?当然,让宏关闭自己的工作簿可以解决问题,但这不是我想做的事情,因为用户需要同一工作簿中的另一个宏来处理新工作表上的更改。

该程序有超过6000行代码(到目前为止),但这是导致问题的一个例程。

Private Sub z3_BudgetPrepUpd()
    'Build a new workbook initialized to let the user modify data
    'for an existing fiscal-quarter budget.

    'When this routine executes,
    '     UserForm1 is still active.

    Dim strTracer As String             'error-handling tracer for this subroutine
    Dim strFyrQtr As String

    On Error GoTo abend
    If blnAbort Then Exit Sub
    If blnAbortGlobal Then Exit Sub

    'Find out which ListBox item the user selected:
    If UserForm1.ListBox1.ListCount < 1 Then GoTo aa9000     'ListBox is empty
    If UserForm1.ListBox1.ListIndex < 0 Then                             'No item selected
        strMsgTitle = udtPrm.msgTtl
        strMsgPrompt = "Please select a fiscal quarter to update."
        Call z0_MsgBox
        GoTo aa9000
    End If
    strFyrQtr = UserForm1.ListBox1.Value                                     'Selected item in ListBox

    'Close UserForm1:
    UserForm1.Hide
    ThisWorkbook.Sheets(c_WKS_WELCOME).Activate

    'Build the udtBgt() array with data for the specified quarter:
    lngBgtHiNdx = -1
    Call zz_GetBudgetForQtr(strFyrQtr)
    If blnAbort Then GoTo aa9000

    'Build a new workbook for the user to update budget amounts:
    Workbooks.Add
    Set wkbNewBook = ActiveWorkbook

    'Save the names of the default worksheets
    'so we can delete them later:
    strDfltSheets() = z0_SheetNames(wkbNewBook)

    'Build a worksheet with data from the udtBgt() array:
    Call z3_BuildBudgetUpdSheet
    If blnAbort Then GoTo aa9000

    'Delete the default worksheets:
    Call z0_DeleteSheets(wkbNewBook, strDfltSheets())
    If blnAbort Then GoTo aa9000

    wkbNewBook.Sheets(c_WKS_IPT_BUDGET).Activate

    'Excel 2016 Bug:
    'We need to close ThisWorkbook to allow the user
    'to work with the book we just created:
    Application.DisplayAlerts = False
    ThisWorkbook.Close

aa9000:
    Exit Sub

abend:
    lngErr = Err.Number
    strErr = Err.Description
    blnAbort = True
    Application.Cursor = xlDefault    'no more hourglass
    strMsgTitle = "Program Error"
    strMsgPrompt = "The following error occurred:" & Chr(10) & Chr(10) & _
                                 "Error No. " & CStr(lngErr) & Chr(10) & _
                                 "Error Description: " & strErr & Chr(10) & _
                                 "Subroutine: z3_BudgetPrepUpd" & Chr(10) & _
                                 "Tracer: " & strTracer
    Call z0_MsgBox
    Resume aa9000
End Sub

2 个答案:

答案 0 :(得分:1)

您使用我同意的ThisWorkbook。你使用我几乎没用过的ActiveWorkbook。

我建议使用变量来存储对除了代码之外的工作簿的引用。所以使用

Dim wb As Excel.Workbook
Set wb = Application.Workbooks.Open("c:\test.xlsm") 'for opening

'* or
Set wb = Application.Workbooks.Add 'for creating a new one

'* or
Set wb = Application.Workbooks.Item("AlreadyOpen.xlsm") 'for referencing one already open

'* from hereon use wb instead of ActiveWorkbook
wb.Worksheets.Item(1).Visible = True

答案 1 :(得分:0)

谢谢大家,感谢您的关注和建议。我通过重新设计没有UserForms或外部工作簿的应用程序解决了这个问题。 Office 2016有很多问题,也许这就是其中之一。无论如何,我没有时间研究它了。