激活工作簿Excel 2010中的隐藏工作表

时间:2013-08-08 15:53:15

标签: excel vba excel-vba excel-2010

我有一个名为“摘要”的摘要表,上面有一个链接。此链接转到以下子目录:

Sub WTButton()
Call GoToWorksheet("Wilmington")
End Sub

如您所见,这会调用另一个名为GoToWorksheet的子组件。它看起来像这样:

Sub GoToWorksheet(strWorksheet As String)
On Error GoTo Err_GoToWorksheet
With Worksheets("Summary")
   If Worksheets(strWorksheet).Visible = xlSheetHidden Then
      Worksheets(strWorksheet).Visible = xlSheetVisible
   End If

   Worksheets(strWorksheet).Activate

End With

Exit_GoToWorksheet:
   Exit Sub

Err_GoToWorksheet:
   MsgBox Err.Description, vbCritical, "GoToWorksheet Error"
   Resume Exit_GoToWorksheet

End Sub

我希望工作表保持隐藏,直到最终用户准备好向其添加数据。如果工作表已经可见,整个系统工作得很好。如果工作表被隐藏,则不会使其可见,也不会激活它。

我做错了什么?

链接到文件:https://www.dropbox.com/s/wsv9al410m7kwda/Book1Test.xlsm

3 个答案:

答案 0 :(得分:1)

这就是我做的事情:我摆脱了GoToWorksheet子程序。然后我让WTButton子看起来像这样:

Sub WTButton()
Worksheets("Wilmington").Visible = xlSheetVisible
Worksheets("Wilmington").Activate
Range("A3").Select
End Sub

这使得它更直接[注意:我从别人那里接管了这个项目]

答案 1 :(得分:0)

未经测试但应该有效......

Sub GoToWorksheet(strWorksheet As String)
  On Error GoTo Err_GoToWorksheet
  With Worksheets(strWorksheet)
     If .Visible = xlSheetHidden Then
        .Visible = xlSheetVisible
     End If
     .Activate
  End With

Exit_GoToWorksheet:
   Exit Sub

Err_GoToWorksheet:
   MsgBox Err.Description, vbCritical, "GoToWorksheet Error"
   Resume Exit_GoToWorksheet

End Sub

答案 2 :(得分:0)

不需要With,也不会测试xlVeryHidden

我会这样写例程:

Option Explicit
'This makes sure I find spelling errors

Sub GoToWorksheet(strWorksheet As String)
   On Error GoTo Err_GoToWorksheet 'Should only fail if worksheet name is wrong
   Worksheets(strWorksheet).Visible = xlSheetVisible
   'just make it visible - don't care what state it was in previously
   Worksheets(strWorksheet).Activate
   Exit Sub

Err_GoToWorksheet:
   MsgBox Err.Description, vbCritical, "GoToWorksheet Error"

End Sub

并且为了测试,为了发现错误的确切位置,我会注释掉所有错误处理