jeudi 15 septembre 2016

If IsError Function

first time post. I am not a programmer by trade, this is just for personal use at work to make my job more efficient and easy, so apologies for what might be a elementary question.

I have searched for examples to answer my question, but can't seem to find an answer that seems to work quite right for what I'm doing.

I want code to look in Row1 of my spreadsheet. If it finds a cell with the text "WarrantyCode", then select that cell. Otherwise, find the first empty cell in Row1 and make it "WarrantyCode"

Here's the code I have.

Sub WCFInd()
If IsError(ActiveWorkbook.Worksheets("Sku").Rows(1).Find("WarrantyCode").Select) Then
Range("A1").End(xlToRight).Offset(0, 1).Select
    Selection.Value = "WarrantyCode"
           Else
                 ActiveWorkbook.Worksheets("Sku").Rows(1).Find("WarrantyCode").Select
End If
End Sub

When Row1 does have a cell with "WarrantyCode" it selects the cell properly. But when it doesn't, I get the Run-time error '91': Object variable or With block variable not set.

Any assistance would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire