0

I have the following code that looks for values with the word "Record" in column L and then copies the row to a separate sheet.

I am getting a type-mismatch error on rows where the value #REF! occurs in column L.

What is the best way to amend the following in order to completely delete those rows and then continue the intended function?

N.B. LastRow is of 'Long' variable type

Sheets("Demand").Rows("1").Copy Sheets("Data").Range("A1")

With Sheets("Demand")

LastRow = .Range("A" & .Rows.Count).End(xlUp).Row

pasteRowIndex = 2

 For r = 1 To LastRow
        If LCase(.Cells(r, "L").Value) Like LCase("Record*") Then
          If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
              i = i + 1
              ReDim v(1 To i)
              v(i) = pasteRowIndex
              End If

          Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
         pasteRowIndex = pasteRowIndex + 1
      End If
 Next r
End With

2 Answers 2

1

First, you need to iterate from last to first row:

For r = LastRow to 1 step -1

    'Next, add if statement inside the loop checking if cell returns error:
    If IsError(.Cells(r, "L").Value) then

        'if so, delete this row:
        .Cells(r, "L").entirerow.delete

    '..... the rest of your code as ElseIf part of conditional statement

    ElseIf LCase(.Cells(r, "L").Value) Like LCase("Record*") Then
      If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
          i = i + 1
          ReDim v(1 To i)
          v(i) = pasteRowIndex
          End If

      Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
     pasteRowIndex = pasteRowIndex + 1
    End If
 Next r
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this fixed it!
0

Like this...

For r = 1 To LastRow
    If Not IsError(.Cells(r, "L").Value) Then
        If LCase(.Cells(r, "L").Value) Like "record*" Then
            If UBound(Split(.Cells(r, "L"), ",")) > 0 Then
                i = i + 1
                ReDim v(1 To i)
                v(i) = pasteRowIndex
            End If

            Sheets("Demand").Rows(r).Copy Sheets("Data").Rows(pasteRowIndex)
            pasteRowIndex = pasteRowIndex + 1
        End If
    End If
Next r

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.