Home » excel » Excel VBA snippet causing Run-Time Error '438'

Excel VBA snippet causing Run-Time Error '438'

Posted by: admin May 14, 2020 Leave a comment

Questions:

1st time poster.

The following VBA script breaks a work macro everytime. I’ve tried different syntaxes to fix, but getting the same result.

The code is supposed to look through all data cells in column L, looking for the string, “ERROR”. If found, copy that cell and the one to the right over to the correct column “AX” and clear the text in L and M.

The step it breaks at every time is the “Cells(i, 50).Paste” line.

Set rng = Application.Range("L4:M" & lrow)
For i = rng.Rows.Count To 4 Step -1
    If Cells(i, 12).Value = "ERROR" Then
        Range("L" & i & ":M" & i).Copy
        Cells(i, 50).Paste
        Range("L" & i & ":M" & i).ClearContents
    End If
    If Cells(i, 21).Value = "ERROR" Then
        Rows(i).Delete
    End If
Next I
How to&Answers:

Paste is not a method available to the Range object. Cells is a Range object. Ergo, “Object does not support this property or method” 🙂

You could try:

Cells(i, 50).PasteSpecial xlPasteAll

Answer:

As @DavidZemens said, you missed the proper method name

furthermore your code seems to do unnecessary work where:

  • it first does something in current i row should If Cells(i, 12).Value = "ERROR" check return True

  • then it would delete the same i row should subsequent If Cells(i, 21).Value = "ERROR" Then return True again

Finally you’re not using any PasteSpecial feature so you may want to use plain Copy one

Hence I’d refactor it as follows

Dim lrow As Long, i As Long

lrow = Cells(Rows.Count, "L").End(xlUp).Row

For i = lrow To 4 Step -1
    If Cells(i, 21).value = "ERROR" Then
        Rows(i).Delete
    ElseIf Cells(i, 12).value = "ERROR" Then
        Range("L" & i & ":M" & i).Copy Destination:=Cells(i, 50)
        Range("L" & i & ":M" & i).ClearContents
    End If
Next i