【问题标题】:Application defined or object defined error 1004应用程序定义或对象定义错误 1004
【发布时间】:2015-12-08 22:52:37
【问题描述】:

我正在尝试定义两个范围(myADR 和 myOCC)以在 Linest 公式中使用。但是我不断收到这个 1004 错误。我尝试了两种方法,第二种方法被注释掉了。有谁知道如何解决这个问题?

Sub LinestFormula()

Dim nCols As Integer
Dim myOCC As Range
Dim myADR As Range
Dim nRows3 As Integer

Range("A1").CurrentRegion.Select
nCols = Selection.Columns.Count

ActiveCell.Offset(5, 1).Resize(1, nCols - 2).Select
Selection.Copy
Range("A1").Select
Selection.End(xlToRight).Offset(0, 2).Select
ActiveCell = "OCC"
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial xlPasteValues, Transpose:=True
nRows3 = Selection.Rows.Count
'Selection = myOCC


Cells(5, 2).Select
Selection.Resize(1, nCols - 2).Select
Selection.Copy
Range("A1").Select
Selection.End(xlToRight).Offset(0, 3).Select
ActiveCell = "ADR"
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial xlPasteValues, Transpose:=True

Range("A1").End(xlToRight).Offset(1, 2).Resize(nRows3, 1).Select
Selection = myOCC
Range("A1").End(xlToRight).Offset(1, 3).Resize(nRows3, 1).Select
Selection = myADR

【问题讨论】:

  • 我会尽量避免使用 .Select 和 ActiveCell,并尽可能在同一行上执行这些操作。它会更容易阅读和理解,并且可能更容易发现问题。代码在哪一行失败?
  • 不知道你为什么会得到这个,但仅供参考,.selects 可能有问题。在这里查看如何避免使用它们。 stackoverflow.com/questions/10714251/…
  • 我也想知道当这个sub被调用时你怎么可能知道activecell是什么,那么你怎么知道你将要做什么。
  • 谢谢大家,我用了 set myOCC = selection

标签: vba excel runtime-error


【解决方案1】:

然后:

Selection = myOCC

使用:

Set myOCC = Selection

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多