简体   繁体   English

宏混淆区分ActiveWorkbook和ThisWorkbook

[英]Macro getting confused differentiating ActiveWorkbook and ThisWorkbook

1st post, but not 1st time visitor - this forum has been an invaluable help in developping my VBA coding skills over the last 3 years - so a big thank you. 第一个帖子,但不是第一次访问者-在过去三年中,该论坛对发展我的VBA编码技能提供了宝贵的帮助-非常感谢。

Here's a weird issue I've encountered. 这是我遇到的一个奇怪的问题。

I've created a month end template for my team to use. 我已经创建了一个月末模板供我的团队使用。 This template stores a number of Macros, including some activated by keyboard short cuts. 该模板存储了许多宏,包括一些通过键盘快捷键激活的宏。 Those short cuts activated macros are all run on other reports in CSV format. 这些快捷方式激活的宏都以CSV格式在其他报表上运行。

I've ensured that the macro would stop running if the active workbook was the main workbook, as it does a lot of formatting and would seriously damage the main workbook. 我确保如果活动工作簿是主要工作簿,则宏将停止运行,因为它会进行大量格式化,并且会严重破坏主要工作簿。

So this fail safe short code will be run hundreds of time a day..... and occasionally, once or twice a month, excel gets confused. 因此,此故障安全短代码每天将运行数百次。.....偶尔,每月一次或两次,excel会感到困惑。 Even though the ActiveWorkbook isn't ThisWorkbook, it acts like it is and aborts the macro. 即使ActiveWorkbook不是ThisWorkbook,它的行为也一样,并中止了宏。

'Prevents from running the macro on the Month End Allocation Template '防止在月末分配模板上运行宏

Dim w As Workbook

Set w = ActiveWorkbook

If w Is ThisWorkbook Then
Exit Sub

End If

I can't really get my head around it, the code is pretty simple, and has 100% prevented unwanted damages to the main workbook (the main thing really), but occasionally prevents the formatting macro to be run other workbooks. 我真的无法解决这个问题,代码非常简单,并且100%防止了对主工作簿(实际上是主要事情)的有害破坏,但是偶尔会阻止格式化宏在其他工作簿上运行。

In those rare instances it happens, the user has to close all excel workbooks, and once reopened, it will work again just fine. 在极少数情况下,用户不得不关闭所有excel工作簿,并且重新打开后,它将再次正常运行。 :| :|

Is it just an excel bug? 只是一个Excel错误? Is there something here I didn't consider and should this code be improved? 这里有我没有考虑的东西,应该改进此代码吗? I love its simplicity, and it has 100% avoided damages to the main workbook, I'm just annoyed that excel seemingly gets confused by what ThisWorkbook is/should be, and that i cannot explain it. 我喜欢它的简单性,并且它可以100%避免损坏主工作簿,我很生气,Excel似乎对ThisWorkbook是/应该是什么感到困惑,我无法解释。

Any main workbook that you don't want users to mess up should have protections that are turned off and on by your macros. 您不希望用户搞砸的任何主工作簿都应具有由宏关闭和打开的保护。 Separate your data from your views. 将数据与视图分开。 Lock the views and modify only the data. 锁定视图并仅修改数据。 For you this means the CSV would import in to columns on a hidden sheet and then the data on your protected sheet with the linked data table would just magically update. 对您来说,这意味着CSV将导入到隐藏工作表中的列中,然后带有链接的数据表的受保护工作表上的数据将神奇地更新。 There should be no formatting macro. 应该没有格式化宏。 The format is static. 格式是静态的。 The linked data is what changes. 链接的数据发生了什么变化。

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

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