【发布时间】:2016-10-20 22:59:26
【问题描述】:
我已经有一段时间没有在 Excel VBA 中使用数组了,所以请原谅我......
我正在尝试定义一个基于动态数组的连续匹配单元格,这些单元格在循环中确定。我确定我的语法对于定义数组是错误的,我只是不确定如何。困难在于我的数组在 1 列中包含大约 6 个连续行,以及不同列中的另一个单元格。有什么想法吗?
Sub calib_range()
Dim instrument As Variant
Dim calibrator As Variant
Dim lastrow As Integer
lastrow = ThisWorkbook.ActiveSheet.Range("b2").SpecialCells(xlCellTypeLastCell).Row
For i = 4 To lastrow
If Cells(i, 4) Like "MPC*" Then
'enter loop to determine length of MPC* array
For x = i + 1 To lastrow
If Cells(x, 4) = Cells(x - 1, 4) Then
Else
x = x - 1
Exit For
End If
Next x
instrument = Array(Cells(i, 17), Range(Cells(i, 14), Cells(x, 14)))
calibrator = Array(0, Range(Cells(i, 12), Cells(x, 12)))
Slope = Application.WorksheetFunction.Slope(instrument, calibrator)
Intercept = Application.WorksheetFunction.Intercept(instrument, calibrator)
Cells(i, 22) = Slope
Cells(i, 23) = Intercept
End If
Next i
End Sub
【问题讨论】:
-
您有
Cells(x, 4) = Cells(x - 1, 4) Then,它直接转到Else- 您是否缺少代码?为什么If语句要这样写? -
@BruceWayne 可能是两个单元格中的值相似,因此不会转到 else。
-
@Vityata - 当然 - 只是好奇为什么不像
If Cells() <> Cells Then // x = x-1 // Exit For那样设置它。 (对不起,凯尔的切线问题,我认为这不是问题的一部分,只是我注意到的。) -
我想是一样的不同,就像我写这篇文章时的想法一样;)