VBA - 运行时错误1004'应用程序定义或对象定义的错误' [英] VBA - Run Time Error 1004 'Application Defined or Object Defined Error'

查看:1889
本文介绍了VBA - 运行时错误1004'应用程序定义或对象定义的错误'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel文档,可以在第一次运行时将模板复制到新工作表中。此新模板附带的任何更多的工作表将追加到新创建的工作表中。

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.

我在本节代码中的标题中收到错误:

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

基本上如果如果语句为真(单元格= 1),则特定工作表上的范围应该被复制并粘贴到新的在使用 PasteSpecial 给出的范围内的值进行格式化。之后,新创建表格应该将图像复制到模板的左上角单元格中,然后调用一个子例程来格式化新工作表的行。

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.

我在第一个 .Range 之前收到错误,在之后使用工作表(有线卡)声明。我试过没有使用语句,直接复制值而不是粘贴特殊的等等。奇怪的是,这将运行在第一次,当新的表通过以下方式创建:

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:

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

但是第二次,完全失败,使用运行时错误1004'应用程序定义或对象定义的错误'。我很欣赏任何帮助。

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.

推荐答案

您的单元格对象未完全限定。您需要在单元格对象之前添加 DOT 。例如

Your cells object is not fully qualified. You need to add a DOT before the cells object. For example

With Worksheets("Cable Cards")
    .Range(.Cells(RangeStartRow, RangeStartColumn), _
           .Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues

同样,单元格对象。

这篇关于VBA - 运行时错误1004'应用程序定义或对象定义的错误'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆