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

问题描述 投票:1回答:3

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

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

excel vba excel-vba excel-2010
3个回答
1
投票

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

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

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


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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.