VBA - 运行时错误 1004 '应用程序定义或对象定义错误'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20601805/
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
VBA - Run Time Error 1004 'Application Defined or Object Defined Error'
提问by user3105671
I have an Excel document that copies a template sheet into a new sheet on the first time it runs. Any more sheets that follow this template are appended to the newly created sheet.
我有一个 Excel 文档,它在第一次运行时将模板工作表复制到新工作表中。遵循此模板的任何更多工作表都会附加到新创建的工作表中。
I'm getting the error in the title in this section of code:
我在这部分代码的标题中收到错误:
If Worksheets("User Configuration").Cells(9, 15).Value = 1 Then
Worksheets("Cable Cards Template").Range("A1:J33").Copy
With Worksheets("Cable Cards")
**.Range(Cells(RangeStartRow, RangeStartColumn), Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues**
.Range(Cells(RangeStartRow, RangeStartColumn), Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlFormats
End With
Worksheets("Cable Cards Template").Shapes("Picture 1").Copy
Worksheets("Cable Cards").Paste Cells(RangeStartRow, RangeStartColumn)
Call Sheets.FormatCableCardRows
End If
Basically if the If
statement is true (the cell = 1), then a range on a particular sheet should be copied and pasted into the new sheet at the range given using PasteSpecial
for values and formatting. Following that, the "newly created" sheet should have an image copied into the top left cell of the template and then a subroutine is called to format the rows of the new sheet.
基本上,如果该If
语句为真(单元格 = 1),则应将特定工作表上的范围复制并粘贴到新工作表中使用PasteSpecial
值和格式给定的范围内。之后,“新创建的”工作表应将图像复制到模板的左上角单元格中,然后调用子例程来格式化新工作表的行。
I'm getting the error at the first .Range
call after the With Worksheets("Cable Cards")
statement. I've tried not using the With
statement, copying values directly instead of paste-special etc. The weird thing is that this will run on the first go, when the new sheet is created via:
我.Range
在With Worksheets("Cable Cards")
语句后的第一次调用时收到错误。我试过不使用该With
语句,直接复制值而不是特殊粘贴等。奇怪的是,当通过以下方式创建新工作表时,这将在第一次运行时运行:
If (RangeStartRow = 1) Then
Worksheets.Add().Name = "Cable Cards" ' Create new sheet with given name only on first cable card
Columns(1).ColumnWidth = 9.43
Columns(6).ColumnWidth = 11
Columns(10).ColumnWidth = 9
Call FormatForA5Printing("Cable Cards", 71)
End If
but on the 2nd go, it fails entirely, with the Run Time Error 1004 'Application Defined or Object Defined Error'
. I'd appreciate any help.
但是在第二次运行时,它完全失败了,Run Time Error 1004 'Application Defined or Object Defined Error'
. 我很感激任何帮助。
回答by Siddharth Rout
Your cells object is not fully qualified. You need to add a DOT
before the cells object. For example
您的单元格对象不完全合格。您需要DOT
在单元格对象之前添加一个。例如
With Worksheets("Cable Cards")
.Range(.Cells(RangeStartRow, RangeStartColumn), _
.Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues
Similarly, fully qualify all your Cells object.
同样,完全限定所有 Cells 对象。
回答by sancho.s ReinstateMonicaCellio
Solution #1: Your statement
解决方案#1:你的陈述
.Range(Cells(RangeStartRow, RangeStartColumn), Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues
does not refer to a proper Range
to act upon. Instead,
不是指Range
行为的正当性。反而,
.Range(.Cells(RangeStartRow, RangeStartColumn), .Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues
does (and similarly in some other cases).
确实(在其他一些情况下也是如此)。
Solution #2:
Activate Worksheets("Cable Cards")
prior to using its cells.
解决方案#2:Worksheets("Cable Cards")
在使用其细胞之前激活。
Explanation:
Cells(RangeStartRow, RangeStartColumn)
(e.g.) gives you a Range
, that wouldbe ok, and that is why you often see Cells
used in this way. But since it is not applied to a specific object, it applies to the ActiveSheet
. Thus, your code attempts using .Range(rng1, rng2)
, where .Range
is a method of one Worksheet
object and rng1
and rng2
are in a different Worksheet
.
解释:
Cells(RangeStartRow, RangeStartColumn)
(eg) 给你一个Range
,那就可以了,这就是为什么你经常看到Cells
以这种方式使用。但由于它不适用于特定对象,因此它适用于ActiveSheet
. 因此,您的代码尝试使用.Range(rng1, rng2)
, where.Range
是一个Worksheet
对象的方法,而rng1
和rng2
在不同的Worksheet
.
There are two checks that you can do to make this quite evident:
您可以执行两项检查来使这一点非常明显:
Activate your
Worksheets("Cable Cards")
prior to executing yourSub
and it will start working (now you have well-formed references toRange
s). For the code you posted, adding.Activate
right afterWith...
would indeed be a solution, although you might have a similar problem somewhere else in your code when referring to aRange
in anotherWorksheet
.With a sheet other than
Worksheets("Cable Cards")
active, set a breakpoint at the line throwing the error, start yourSub
, and when execution breaks, write at the immediate windowDebug.Print Cells(RangeStartRow, RangeStartColumn).Address(external:=True)
Debug.Print .Cells(RangeStartRow, RangeStartColumn).Address(external:=True)
and see the different outcomes.
Worksheets("Cable Cards")
在执行之前激活你的Sub
,它将开始工作(现在你有对Range
s 的格式良好的引用)。对于您发布的代码,.Activate
在之后添加With...
确实是一个解决方案,尽管在引用Range
another 中的a 时,您的代码中的其他地方可能会遇到类似的问题Worksheet
。使用非
Worksheets("Cable Cards")
活动表,在抛出错误的行设置断点,启动您的Sub
,当执行中断时,在立即窗口写入Debug.Print Cells(RangeStartRow, RangeStartColumn).Address(external:=True)
Debug.Print .Cells(RangeStartRow, RangeStartColumn).Address(external:=True)
并看到不同的结果。
Conclusion:
Using Cells
or Range
without a specified object (e.g., Worksheet
, or Range
) might be dangerous, especially when working with more than one Sheet
, unless one is quite sure about what Sheet
is active.
结论:使用Cells
或Range
不使用指定的对象(例如,Worksheet
, 或Range
)可能是危险的,尤其是在使用多个 时Sheet
,除非非常确定什么Sheet
是活动的。
回答by Mosca Pt
Assgining a value that starts with a "=" will kick in formula evaluation and gave in my case the above mentioned error #1004. Prepending it with a space was the ticket for me.
分配以“=”开头的值将启动公式评估,并在我的情况下给出上述错误 #1004。在它前面加上一个空格对我来说是一张票。