vba 将 ws 调暗为工作表集 ws=ActiveSheet 给了我“类型不匹配”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25001164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:43:17  来源:igfitidea点击:

dim ws as worksheet set ws=ActiveSheet gives me "Type Mismatch"

excel-vbavbaexcel

提问by nsg

I would like to have a modelless form that does something to current worksheet. Everything works fine when I use it on the worksheets in the same workbook where the form is defined. (ActiveSheet represents current worksheet).

我想要一个对当前工作表做一些事情的无模型表单。当我在定义表单的同一工作簿中的工作表上使用它时,一切正常。(ActiveSheet 代表当前工作表)。

The problem happens when I try to use form when there is different active workbook, when I click on a form it immediately activates the workbook where it was defined and ActiveSheet no longer works for me. It acts strange:

当我在有不同的活动工作簿时尝试使用表单时会出现问题,当我单击表单时,它会立即激活定义它的工作簿,并且 ActiveSheet 不再适合我。它的行为很奇怪:

dim ws as worksheet
set ws=ActiveSheet

gives me "Type Mismatch"

给我“类型不匹配”

debug.print ActiveSheet.Name ' prints the name of the original worksheet
debug.print ActiveSheet.Parent.Name ' prints the name of the workbook where form is defined

This is Excel 2013. I think I was able to do it in previous versions. Has this ability been lost?

这是 Excel 2013。我想我可以在以前的版本中做到这一点。是不是失去了这个能力?

回答by Gary's Student

This will occur if a Chartor other non-worksheet is Active

如果图表或其他非工作表处于活动状态,则会发生这种情况

回答by cronos2546

Dim ws as worksheet
Set ws= ThisWorkbook.ActiveSheet