我是VBA的新手。我正在尝试编写一个清理实验中某些数据的脚本。我一直收到一个错误说“需要对象”,它突出显示了pold。有谁知道为什么?
至于脚本,我试图找到一列参与者数字,并确定每个参与者所在的范围。每个参与者大约有30行,我想将其定义为数组中的值。
Sub Cleanthismofoup()
Dim pranges(1 To 50) As Long
Dim pbegin As Range
Dim pend As Range
Dim pold As Integer
Dim pnew As Integer
Dim pcell As Range
Dim pcounter As Long
Dim i As Long
Set pcell = Range("A1:A1")
Set pbegin = Range("A2:A2")
Set pold = Range("B2:B2").Value
pcounter = 0
'for every item, store value in pnew
' move down one line. Check pnew = pold
' if it is, do again. else create new range
For i = 1 To rngl
pcell = pcell.Offset(-1, 0)
pnew = pcell.Cells.Value
If pnnew <> pold Then pcell = pend
If pcell = pend Then
counter = counter + 1
pranges(counter) = pbegin
counter = counter + 1
pranges(counter) = pend
pbegin = pcell.Offset(-1, 0)
Else: pold = pnew
End If
i = i + 1
Next
End Sub
答案 0 :(得分:2)
错误是因为您使用Set
关键字来指定对象的引用。由于Set pold = Range("B2:B2").Value
的RHS上的输出是整数,因此vba会给出错误。要解决此问题,只需删除Set
关键字即可。但是,我还注意到您在rng1
循环中使用for
而未初始化rng1
变量,在这种情况下,您的for
循环将永远不会执行。您可能还想纠正这一点。