0

I have a VBA macro, part of which loops through an array of defined strings and finds them in column A of the worksheet.

This works perfectly fine if all of the strings of the array exist in column A, but falls over if the string does not exist.

I have struggled with a simple if statement that would help me out. If array value is not found, move next

For iArow = 0 To UBound(MyArray) - 1
    iRow = Range("A:A").Find(MyArray(iArow), LookIn:=xlValues, lookat:=xlWhole).Row
    Range("D" & iRow).ClearContents
    Range("I" & iRow).ClearContents
    Rows(iRow + 1).Insert
    Rows(iRow).Insert 
Next iArow

Any suggestions?

1 Answer 1

1

Dows that help?

For iArow = 0 To UBound(myArray) - 1
    Set Rng = Range("A:A").Find(myArray(iArow), LookIn:=xlValues, lookat:=xlWhole)
    If Not Rng Is Nothing Then
        iRow = Rng.Row
        Range("D" & iRow).ClearContents
        Range("I" & iRow).ClearContents
        Rows(iRow + 1).Insert
        Rows(iRow).Insert
    End If
Next iArow
Sign up to request clarification or add additional context in comments.

3 Comments

Works a treat thanks. But can you explain it to me! It is like you are using a double negative to make a positive in the if statement to allow me to step through the statement if the array value is found. Rng is set to the value of my array, that is fine. But then ----- "If NOT Rng is NOTHING Then" Am I reading that right?
Nothing is a keyword indicating that an object has no value. As there is no keyword Anything you have to use the double negation.
Nothing is a special value in vba. Range.Find() returns that value 'Nothing' when it does not find a match, and that's what Fratyx is checking in is if statement.

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.