dynamicvariables分配VBA

好,所以我一直在努力弄清楚我的variables“aveCellAdrs”和“q”的分配问题是什么。 每个患有错误:对象'Global_'的方法'Range'失败。

我希望variables'q'和'aveCellAdrs'所做的是指dynamicvariables'k'(k每次循环都会自行改变),然后从中得到新的范围。

下面是我的代码:

Dim i As Integer Dim j As Integer Dim k As Range Dim q As Range Dim tableRange2 As Range Dim total As Double Dim table2average As Double Dim aveCellAdrs As Range For i = 1 To 20 Set k = Range("B73").End(xlUp).Offset(i, 0) 'This finds the top-most cell as a starting point Set aveCellAdrs = Range(k).End(xlToRight) 'Used to enter the average found in the loops below to the correct cell Set q = Range(k).End(xlToRight).Offset(0, -1) 'This finds the right-most cell in the row Set table2Range2 = Range(k, q) 'Identifying the dynamic range to which we will calculate each rows' average 

希望这是有道理的。 下面是代码块的附加屏幕截图:

在这里输入图像说明

braX是正确的,在VBA范围内是一个对象,而不是数据types。 当你input:

 Set table2Range2 = Range(k, q) 

我想你要做的是将范围的最左边和最右边的限制传递给range()以设置table2Range2的范围。 如果是这样的话,你想传递单元地址 (即B2:E2),这是range()期望接收的地址。 你在上面的代码中实际做的是试图传递整个范围对象,这导致range()抛出一个错误。

下面是我要做的:首先将范围设置为参考点,创build一些stringvariables来保存要创build的范围边angular处的单元格地址。 将其构build到一个string中,并将其传递给range(),以便为循环的每次迭代dynamic设置tablerange。 一旦你设置了一个范围,你可以使用.address方法来获取单元地址。 见下文:

 Dim i As Integer Dim startingPoint As Range Dim rngStart As String Dim rngEnd As String Dim tableRange As Range For i = 1 To 20 Set startingPoint = Range("B73").End(xlUp).Offset(i, 0) rngStart = startingPoint.Address 'This finds the top-most cell as a starting point rngEnd = startingPoint.End(xlToRight).Offset(0, -1).Address 'This finds the right-most cell in the row Set tableRange = Range(rngStart & ":" & rngEnd) 'Identifying the dynamic range to which we will calculate each rows' average Next