vba 错误:无法移动焦点,因为它不可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19373798/
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-11 23:49:25  来源:igfitidea点击:

Error: Can't move focus because it is invisible

excelvba

提问by user2832896

I have the following code that works in all other circumstances except in a single where it returns the error "Can't move focus because it is invisible, not enable, or type that does not accept focus". The data in sheet consists only basic numbers and words. My objective is to select a range from one work book and paste it to another. It appear that excel does not recognise anything to be in the cells, although there in fact is. Does anyone know why this may be happening? Thanks in advance.

我有以下代码可以在所有其他情况下工作,但在单个代码中它返回错误“无法移动焦点,因为它不可见、未启用或类型不接受焦点”。表中的数据仅包含基本数字和单词。我的目标是从一本工作簿中选择一个范围并将其粘贴到另一本工作簿中。excel似乎无法识别单元格中的任何内容,尽管实际上确实存在。有谁知道为什么会发生这种情况?提前致谢。

    Set Users = Application.Workbooks.Open(PathA)
        With Prices
        .Sheets("Sheet").Range("A:AJ").Select
        Selection.Copy
        End With
    'Set Risk = Application.Workbooks.Open(PathX)
        With Risk
        .Sheets("Sheet").Range("A1:AJ1048576").PasteSpecial Paste:=xlPasteAll
        .Save
       ' .Close
        End With
        Users.Close

回答by chiliNUT

it looks like someone else had the same issue and was able to resolve it on an MSDN forum

看起来其他人有同样的问题,并且能够在 MSDN 论坛上解决它

http://social.msdn.microsoft.com/Forums/office/en-US/3263b079-7e4f-452c-8dcc-92c682b8370b/excel-form-cant-move-focus-to-the-control-because-it-is-invisible-not-enabled-or-of-a-type-that?forum=exceldev

http://social.msdn.microsoft.com/Forums/office/en-US/3263b079-7e4f-452c-8dcc-92c682b8370b/excel-form-cant-move-focus-to-the-control-because-it- is-invisible-not-enabled-or-of-a-type-that?forum=exceldev

maybe this fix will apply to your situation too.

也许此修复程序也适用于您的情况。

... OK so on that page, one guy has a kludge that seemed to work for him:

......好吧,在那个页面上,一个人有一个似乎对他有用的kludge:

Error was:

错误是:

Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.

无法将焦点移到控件上,因为它不可见、未启用或属于不接受焦点的类型。

How to Fix

怎么修

1. Select the object with the problem, e.g. the Form or Control.

1. 选择有问题的对象,例如表单或控件。

2. In the properties windows, select the name of the object and rename adding an "x" to the end, e.g. Rename CPanel to CPanelx.

2. 在属性窗口中,选择对象的名称并在末尾添加“x”重命名,例如将 CPanel 重命名为 CPanelx。

3. Save the Form.

3. 保存表格。

4.Now rename the CPanelx back to CPanel.

4.现在将 CPanelx 重命名回 CPanel。

5. Run your form.

5. 运行您的表单。

6. Problem solved.

6. 问题解决。

Something screwy with Excel VBA, not sure what !, but this will get you working again. Steve D.

Excel VBA 有点问题,不确定是什么!,但这会让你重新工作。史蒂夫 D。

and then someone else described the underlying problem, but his solution involved using design mode, which seems like it is maybe defeating the purpose of automating in vba, but the moderator seemed to like his answer better, so here that is as well

然后其他人描述了潜在的问题,但他的解决方案涉及使用设计模式,这似乎违背了 vba 中自动化的目的,但版主似乎更喜欢他的答案,所以这里也是

In normal use activeX controls are intended only to be activated, not selected, there's a difference. If you partifcularly want to Select the control (manually or with code), try putting Excel into Design mode first.

在正常使用中,activeX 控件旨在仅被激活,而不是被选中,这是有区别的。如果您特别想要选择控件(手动或使用代码),请先尝试将 Excel 置于设计模式。

See also the topic "Why can't I select form and ActiveX controls?" in Excel's help (not VBA's help)

另请参阅主题“为什么我不能选择窗体和 ActiveX 控件?” 在 Excel 的帮助中(不是 VBA 的帮助)

Peter Thornton

彼得·桑顿

回答by user2832896

"Run-time error '-2147352565 (8002000b)': Can't move focus to the control because it is invisible, not enabled or of a type that does not accept the focus" Can be overcome but sequentially defining the area you wish to select. Obviously some may do this in any case, but it this specific instance it is needed to overcome the issue. This is an example for copying from one workbook to another and pasting where U is the sheet excel finds to be "invisible".

“运行时错误‘-2147352565 (8002000b)’:无法将焦点移到控件上,因为它不可见、未启用或属于不接受焦点的类型” 可以克服但按顺序定义您希望的区域选择。显然有些人在任何情况下都可能会这样做,但在这个特定的情况下,需要克服这个问题。这是一个示例,用于从一个工作簿复制到另一个工作簿并粘贴 U 是 Excel 发现“不可见”的工作表。

        Dim U As Workbook
        Dim Us As Worksheet
    Set U = Application.Workbooks.Open(Path)
    Set Us = U.Worksheets("sheet")
    With Us
        .Range("A:AJ").Select
        Selection.Copy
    End With
        U.Close SaveChanges = True
    With DestinationWorkbook
        .Sheets("sheet").Range("A:AJ").PasteSpecial Paste:=xlPasteAll
        .Save
    End With

回答by Lucien

Just to help in case you didn't find the answer till now : In my case this message came out when I redimensioned the application window while it was maximized :

只是为了帮助您直到现在还没有找到答案:在我的情况下,当我在最大化应用程序窗口时重新调整它的大小时会出现此消息:

e.g. Application.width = 100 (on Excel 2003 or 2007)

例如 Application.width = 100 (on Excel 2003 or 2007)

The solution in this case, is to first bring the application window to NORMAL.

在这种情况下,解决方案是首先将应用程序窗口设置为 NORMAL。

e.g. ActiveWindow.WindowState = xlNormal

例如 ActiveWindow.WindowState = xlNormal