0

I am a new learner of VBA programming. It is really so tough to figure out the error. For the following code, there is always a message of

'object rejected'

May I understand why it is happening ToT

Thank you so much!!!

Sub SelectAndCopyCellsWithValue()

    Dim SingleCell As Range

    Dim area As Range
    Set area = ActiveSheet.Range("C158", "ED158")

    For Each SingleCell In area
        If SingleCell <> 0 Then
            SingleCell.Select
            SingleCell.Value.Copy Worksheets("Sheet1").Range("A1").End(xlToRight).Offset(0, 1)
            SingleCell.Offset(-155, 0).Value.Copy Worksheets("Sheet1").Range("A2").End(xlToRight).Offset(0, 1)
            SingleCell.Offset(-154, 0).Value.Copy Worksheets("Sheet1").Range("A3").End(xlToRight).Offset(0, 1)

        End If
    Next SingleCell

End Sub
2
  • Which specific line is raising this error? I've never seen an error message that says "object rejected". Are you sure that wasn't "Object required"? Commented Nov 20, 2019 at 16:52
  • Also SingleCell.Select is redundant, remove it Commented Nov 20, 2019 at 16:52

1 Answer 1

1
SingleCell.Value.Copy

Range.Value is a value, not an object: values don't have members that can be invoked - this instruction is illegal and raises an object required error, because, well, an object would be required in order for that .Copy member call to be made.

You want to invoke Range.Copy, so do SingleCell.Copy.

If you mean to just copy the value and not the formatting, borders, conditional formats and whatnot, then don't involve the clipboard and just write to the cells:

Dim targetSheet As Worksheet
Set targetSheet = Worksheets("Sheet1")

targetSheet.Range("A1").End(xlToRight).Offset(0, 1).Value = SingleCell.Value
targetSheet.Range("A2").End(xlToRight).Offset(0, 1).Value = SingleCell.Offset(-155, 0).Value
targetSheet.Range("A3").End(xlToRight).Offset(0, 1).Value = SingleCell.Offset(-154, 0).Value
Sign up to request clarification or add additional context in comments.

Comments

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.