2015-12-08 92 views
2

我想要定义两个范围(myADR & myOCC)在一个最小公式中使用。但是我不断收到这个1004错误。我尝试了两种方式,第二种方式已被注释掉。有谁知道如何解决这一问题?应用程序定义的或对象定义的错误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 
+2

我会尽量避免使用。选择和ActiveCell,并做同样的这些行动如果可能的话。阅读和理解起来会更容易,并且可能更容易发现问题。 哪一行是代码失败? – Alex4336

+1

不知道为什么你会得到这个,但仅供参考.select可能有问题。看看这里看看如何避免使用它们。 http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros – MatthewD

+0

我也想知道你怎么可能知道什么是活动细胞时,这个子被称为,那么你怎么知道你将会在做什么。 –

回答

3

而不是:

Selection = myOCC 

使用:

Set myOCC = Selection 

+0

这工作,非常感谢你! –

相关问题