[英]How to assign range of cells to Range variable
My environment: MS Excel 2013 我的环境:MS Excel 2013
I want to assign range of cells to my range variable with code below 我想用下面的代码将单元格范围分配给我的范围变量
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))
I get error at last row while trying to assign value to rngY
. 尝试为rngY
赋值时,最后一行出现错误。
Run-time error '1004':
Application-defined or object-defined error
Cells(2, RefCol)
is referencing the cells on the active worksheet. Cells(2, RefCol)
引用活动工作表上的单元格。 You must qualify all the range objects. 您必须限定所有范围对象。
Example 1: 范例1:
Set rngY = wbSource.Sheets(1).Range( wbSource.Sheets(1).Cells(2, RefCol), wbSource.Sheets(1).Cells(LastSource, RefCol))
Example 2: 范例2:
With wbSource.Sheets(1)
Set rngY = .Range( .Cells(2, RefCol), .Cells(LastSource, RefCol))
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.