简体   繁体   English

Excel vba 2013不支持thisworkbook.sheets语法

[英]Excel vba 2013 not supporting thisworkbook.sheets syntax

Background 背景

I am working on a inventory management and staff time tracker tool in which I have created several tabs ie Login Panel, Interface, Reference tab, ... etc. In the VBA editor in thisworkbook module of this tool file I have written a code within the workbook_beforeClose event which will hide all the sheets and make the Login Panel sheet visible before getting closed whenever user tried to close it. 我正在使用一个库存管理和人员时间跟踪工具,在其中创建了几个选项卡,即“登录面板”,“界面”,“参考”选项卡等。在此工具文件的thisworkbook模块的VBA编辑器中,我在其中编写了代码workbook_beforeClose事件,该事件将隐藏所有工作表并使登录面板工作表可见,然后在用户每次关闭时将其关闭。 Here is the code:- 这是代码:-

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.ScreenUpdating = False
sht_LoginPanel.Visible = xlSheetVisible
For Each sht In ThisWorkbook.Sheets
If sht.Name <> "Login Panel" Then
sht.Visible = xlSheetHidden
End If
Next sht
Application.ScreenUpdating = True
End Sub

Problem 问题

This above piece of code is written on a computer with MS Excel 2010 and everything works fine. 以上这段代码是在装有MS Excel 2010的计算机上编写的,一切正常。 However when I use this tool on a computer with MS Excel 2013 the same above code gives me error on line 5 (If sht.Name <>...) . 但是,当我在具有MS Excel 2013的计算机上使用此工具时,上述相同的代码在第5行上显示错误(如果sht.Name <> ...) Another amazing thing which I caught is error does not occurs with the first iteration of the For each loop... it only occurs when it has reached till Next sht command line and then comes back to line 5. 我发现的另一个令人惊奇的事情是,对于For Each循环的第一次迭代,不会发生错误...仅在到达Next sht命令行然后返回到第5行时才会发生。

Can anybody please help me with this error and explain what is wrong here.... also is there any compatibility issue between Excel VBA 2010 and 2013. Also one last thing is I also see the ListView control is not working in 2013 wheareas it was working perfectly fine in excel 2010. 有人可以帮我解决这个错误,并解释这里的问题吗。...Excel VBA 2010和2013之间也存在任何兼容性问题。最后一件事是我还看到ListView控件在2013年无法正常运行,原因是在Excel 2010中工作正常。

Please help.... 请帮忙....

Regards, Premanshu 问候,Premanshu

Could you switch to this and see if it helps? 您可以切换到该功能,看看是否有帮助?

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Application.ScreenUpdating = False
  sht_LoginPanel.Visible = xlSheetVisible
  For Each sht In ThisWorkbook.Sheets
    If sht <> sht_LoginPanel Then
      sht.Visible = xlSheetHidden
    End If
  Next sht
  Application.ScreenUpdating = True
End Sub

Regarding compatibility issues, the only forward-compatibility problem I've come across so far is that in the 64 bit version of excel 2013 (and apparently 2016) activeX objects break any code that refers to them. 关于兼容性问题,到目前为止,我遇到的唯一前向兼容性问题是在64位版本的excel 2013(显然是2016)中,activeX对象破坏了引用它们的代码。 Apparently they are not going to be supported going forward or something. 显然,将来他们将不会得到支持。

It works for me in all Excel versions 2010, 2013, and 2016. BUT: If there is no sheet named "Login Panel" you would try to hide ALL sheets which is not possible, at least one sheet mus remain unhidden. 它适用于所有Excel版本2010、2013和2016。但是:如果没有名为“登录面板”的工作表,您将尝试隐藏所有不可能的工作表,至少要隐藏一个工作表。 Also I strongly recommend to initialize variables and use indentations - makes life and maintenance easier. 另外,我强烈建议初始化变量并使用缩进-使寿命和维护更加容易。 This codes works on all Excel platforms - I have tried it out: 该代码可在所有Excel平台上使用-我已经尝试了:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
  ' create variables with data type
  Dim sht As Worksheet
  Dim sht_LoginPanel As Worksheet

  Application.ScreenUpdating = False
  Set sht_LoginPanel = ThisWorkbook.Sheets("Login Panel")
  sht_LoginPanel.Visible = xlSheetVisible
  For Each sht In ThisWorkbook.Sheets
    If sht.Name <> "Login Panel" Then
      ' prevents an error if you try to hide the last worksheet
      On Error Resume Next
      sht.Visible = xlSheetHidden
      ' go back to normal error handling
      On Error GoTo 0
    End If
  Next sht
  Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM