从另一张表中select范围时出错
我想要select位于Excel工作簿的第一个工作表上的范围,无论它是否处于活动状态。
Set WorkRng = Sheets(1).Range(Cells(9, 4), Cells(39, colNum))
这是返回错误:
Run-time error: 1004 Application-defined or object-defined error
只有当第一张没有被选中时
注意:colNum = 11
我该如何解决这个错误?
你在正确的轨道上,你正在使用一个明确的表单对象(这是很好)。 它分解的地方是你如何分配你的Range对象。
你的代码说
Set WorkRng = Sheets(1) <-- use sheet "1" as the reference for generating the range .Range( Cells(9, 4) <-- compose the range starting with cells R9C4 in the *Activesheet* , Cells(39, colNum) <-- end the range composure with cells R29C[colnum] in the *Activesheet* )
你需要哟限定所有的范围参考
Set WorkRng = Sheets(1).Range(Sheets(1).Cells(9, 4), Sheets(1).Cells(39, colNum))
或者,
With Sheets(1) Set WorkRng = .Range(.Cells(9, 4), .Cells(39, colNum)) End With
注意到.
Range
和Cells
之前。 这意味着它们都是从同一张表中取出的。 否则,这是不合逻辑的。