0

I'm new to VBA. I'm trying to write a script that cleans up some data from an experiment. I keep getting an error saying "Object Required" and it highlights pold. Does anyone have any idea why?

As for the script, I'm trying to go down a column of participant numbers and map out what range each participant is in. There are around 30 lines per participant, and I want to define that as values in an array.

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

2
  • which line it is encountering an error on? Commented Jul 6, 2012 at 4:44
  • You seem to have a mix of pnew and pnnew going on but pnnew isn't declared anywhere. Add Option Explicit to the top of the module if it's not there already Commented Jul 11, 2012 at 0:57

1 Answer 1

4

The error is because you are using a Set keyword which is used to assign reference to the object. Since the output on the RHS of Set pold = Range("B2:B2").Value is an Integer, vba gives you an error. To resolve it simply remove the Set keyword. However I also noticed that you are using rng1 in the for loop without initializing the rng1 variable, in which case your for loop will never execute. You might also want to rectify that.

Sign up to request clarification or add additional context in comments.

3 Comments

The Set keyword is also missing from this line: pcell = pcell.Offset(-1, 0)
Thanks. I had been messing around with that part and didn't save the rng1 part.
Also, does the pcell=pcell.Offset(-1,0) seem alright? I'm trying to have the range value stored in pcell to move one down the column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.