所以我试图制作一个样本选择器。
我拥有的当前变量是用户指定的范围。
Settings.AmountColumn是一个看起来像B1:H100线的范围。 SampleRefArr是一个行号的数组,相对于Settings.AmountColumn的第一行。
例如,如果我的SampleRefArr显示(3,4,7),那么我将需要采用以下范围(B4:H4,B5:H5,B8:H8)并将它们复制到新的电子表格中。我已经构建了以下代码但是没有选择正确的单元格......
counter = UBound(SampleRefArr, 1)
For i = 1 To counter
rowSelector = SampleRefArr(i) - Settings.AmountColumn.Cells(1, 1).Row + 2
'Settings.AmountColumn.Cells(rowSelector, 1).EntireRow.Select
Settings.AmountColumn(Cells(rowSelector, 1), Cells(rowSelector, 10)).Select
Next i
答案 0 :(得分:1)
你可以用这个:
Sub RangeSelector(rng As Range, refArr As Variant)
Intersect(rng, rng.Range("A" & Replace(Join(refArr, ","), ",", ",A")).EntireRow.Offset(1)).Select
End Sub
被称为:
RangeSelector Range("B1:H100"), Array(3, 4, 7)
将返回选中的单元格“B4:H5”和“B8:H8”
答案 1 :(得分:0)
也许这个?
counter = UBound(SampleRefArr, 1)
For i = LBound(samplrefarr,1) To counter
With Settings.AmountColumn
rowSelector = SampleRefArr(i) - .Cells(1, 1).Row + 2
'Settings.AmountColumn.Cells(rowSelector, 1).EntireRow.Select
.Range(.Cells(rowSelector, 1), .Cells(rowSelector, 10)).Select
End With
Next i