运行时错误“1004”:范围类的选择方法失败 VBA 2003
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9916342/
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
Run Time Error '1004': Select method of Range Class failed VBA 2003
提问by Stephan Daudt
I am trying to copy a column from one sheet to another. The code I am using is a recorded macro and it works fine until I connect it to a button. When I do so, it gives a
我正在尝试将一列从一张纸复制到另一张纸上。我使用的代码是一个录制的宏,在我将它连接到按钮之前它可以正常工作。当我这样做时,它给出了一个
Run Time Error '1004': Select method of Range Class failed
运行时错误“1004”:范围类的选择方法失败
Here is the code and I can see nothing wrong with it. When I hit debug it highlights the second line.
这是代码,我看不出有什么问题。当我点击调试时,它会突出显示第二行。
Sheets("Count").Select
Columns("C:C").Select
Selection.Copy
Sheets("Add Invintory").Select
Range("b1").Select
ActiveSheet.Paste
Sheets("Count").Select
Sheets("Count").Columns("A:A").Select
Columns("A:A").Select
Selection.Copy
Sheets("Add Invintory").Select
Range("A1").Select
ActiveSheet.Paste
I have no clue what the problem is. Please help
我不知道问题是什么。请帮忙
回答by Siddharth Rout
You should always avoid using .Select
They are a major cause of errors. You may want to see How to avoid using Select in Excel VBA
您应该始终避免使用.Select
它们是导致错误的主要原因。您可能想查看如何避免在 Excel VBA 中使用 Select
Sub Sample()
Sheets("Count").Columns("C:C").Copy _
Sheets("Add Invintory").Columns("B:B")
Sheets("Count").Columns("A:A").Copy _
Sheets("Add Invintory").Columns("A:A")
End Sub
回答by Ed Bolton
I think the issue is that you have written the code in another sheet's code module. If I'm in Sheet1, and write e.g.
我认为问题在于您已经在另一个工作表的代码模块中编写了代码。如果我在 Sheet1 中,并写下例如
Sheets("Sheet2").Select
Columns("A:A").Select
...then Excel assumes you are referring to the Columns on Sheet1 as it treats the current sheet as a default. Therefore, you've told Excel "select Sheet 2" then "select a column on Sheet 1"...which it can't do so it gives you an error message. The best solution would be not to use 'Select'...but you will still see in Siddharth's code that he has had to refer to sheet addresses explicitly
...然后 Excel 假定您指的是 Sheet1 上的列,因为它将当前工作表视为默认工作表。因此,您已经告诉 Excel“选择工作表 2”然后“选择工作表 1 上的一列”......它不能这样做,因此它会给您一条错误消息。最好的解决方案是不使用“选择”...但您仍会在 Siddharth 的代码中看到他必须明确引用工作表地址
Your original code would have worked if placed in the ThisWorkbook module. Locations for entering code are explained towards the end of this Excel help video
如果放置在 ThisWorkbook 模块中,您的原始代码会起作用。在此Excel 帮助视频的末尾解释了输入代码的位置
回答by tomas
When you are putting vba code into the "view sheet code" .. There definitely helps to use Application.Run ... to run macro..
当您将 vba 代码放入“视图表代码”时.. 使用 Application.Run ... 来运行宏肯定有帮助..
I had problem i directly input macro to sheet code.. for selection in another sheet it claimed runtime error 1004.. So i created macro separately and then i put Application.Run my macro from sheet code.
我有问题,我直接将宏输入到工作表代码中.. 在另一个工作表中选择它声称运行时错误 1004 .. 所以我单独创建了宏,然后我从工作表代码中放置了 Application.Run 我的宏。
Works out perfectly ;)
完美地工作;)
This Application.Run also helps when you have too big macro that excel claim it cant be so big. You can easily divide to several parts and then just run applications one by one.. ;)
当你有太大的宏时,这个 Application.Run 也有帮助,excel 声称它不能这么大。您可以轻松地分成几个部分,然后一个一个地运行应用程序.. ;)