如何在不同的工作表中使用可变范围
我为两个范围之间的产品编写了一个代码。 其中一个范围是在其他的,当我试图使其变化,出现一些错误。 这是代码:
Sub Correlacionar() Dim col As Integer Dim random As Range Dim SDesv As Range Dim Chole As Range col = 4 Set Chole = Sheet4.Range("a20:d23") 'Set Chole = Sheet4.Range(Cells(20, 1), Cells(19 + col, col)) Set random = Range(Cells(3, 9), Cells(3, 8 + col)) Set SDesv = Range(Cells(10, 1), Cells(10, col)) SDesv = WorksheetFunction.MMult(random, WorksheetFunction.Transpose(Chole)) Application.CutCopyMode = False End Sub
我想用:
Set Chole = Sheet4.Range(Cells(20, 1), Cells(19 + col, col))
代替:
Set Chole = Sheet4.Range("a20:d23")
Set Chole = Sheet4.Range(Sheet4.Cells(20, 1), _ Sheet4.Cells(19 + col, col))
如果你只使用单元格,那么它将引用activesheet,而不是Sheet4。
如果没有特定的工作表限定符,最好不要使用Range
或Cells
。 你的代码不应该依赖于任何活动的特定工作表。
类似于如果工作簿和工作表未处于活动状态,则无法使用范围
单元格隐式引用ActiveSheet.Cells
with sheet4 set Chole = .range(.cells(20,1), cells(19 + cells(19 + col, col)) end with