2016-07-29 104 views
0

我的环境:MS Excel的2013如何分配细胞的范围的范围内变化

我要分配单元的范围与代码我的范围变量低于

Dim strSourceFile As String 
Dim wbSource As Workbook 

strSourceFile = "D:\csv1.csv" 
Set wbSource = Workbooks.Open(strSourceFile) 
Set rngY = wbSource.Sheets(1).Range(Cells(2, RefCol), Cells(LastSource, RefCol)) 

我在最后一排,而得到错误试图将值分配给rngY

Run-time error '1004': 
Application-defined or object-defined error 

回答

1

Cells(2, RefCol)正在引用活动工作表上的单元格。您必须限定所有范围对象。

例1:

Set rngY = wbSource.Sheets(1).Range(wbSource.Sheets(1).Cells(2, RefCol), wbSource.Sheets(1).Cells(LastSource, RefCol)) 

例2:

With wbSource.Sheets(1) 
    Set rngY = .Range(.Cells(2, RefCol), .Cells(LastSource, RefCol)) 
End With 
+0

哇,托马斯,你救了我的命<3个吻。 – NNOPP

+0

LOL ...谢谢你的支票! – 2016-07-29 07:45:50

相关问题