Hello all,
I've a question about cells.find. I know I can use different code to find a cell with a specific value and then delete its row but I rather like the simplicity of cells.find.
My code is as follows:
The form has this code for the RefNo field:
As you can see, I have a form that pops up when the button is clicked on in the sheet and this works perfectly well. I was recently asked to change this code a bit and need to delete a line that I create in the code. The problem is, the above code errors out at line "Cells.Find(What:=RefNo).Select". The error is "run time error 91 object variable or with block variable not set"
Looking online, the reason seems to be that the cells.find cannot find the value I am looking for, RefNo. But I can see the value in the sheet (and the Vlookup works too) and if I replace the line
with a simple
and Step into the code (F8) then it works. I though maybe the RefNo from the form isn't numeric but I checked with an IsNumeric() and it's a number, same as the spreadsheet. I haven't really used cells.find before but everything except the "What" is optional so I don't think I am missing something from its syntax. Can't figure out what the problem is. Any ideas?
Thanks,
Sinon
I've a question about cells.find. I know I can use different code to find a cell with a specific value and then delete its row but I rather like the simplicity of cells.find.
My code is as follows:
Code:
Dim RefNo As Long
RefNo = Form.txtReferenceNumber.Value
ThisWorkbook.Worksheets("Sheet1").Activate
Range("A1").Activate
ActiveCell.End(xlDown).Offset(1, 0).Select
ActiveCell.Value = RefNo
'Other bits of code that work
ThisWorkbook.Worksheets("Sheet1").Activate
ProductCat = WorksheetFunction.Application.WorksheetFunction.VLookup(RefNo, ThisWorkbook.Worksheets("Sheet1").Range("A:AJ"), 36, False)
If ProductCat = "Product1" Then
Cells.Find(What:=RefNo).Select
Selection.EntireRow.Delete
Else
'Do nothing
End If
The form has this code for the RefNo field:
Code:
Private Sub txtReferenceNumber_Change()
'Centre Text Alignment
txtReferenceNumber.TextAlign = fmTextAlignCenter
End Sub
Looking online, the reason seems to be that the cells.find cannot find the value I am looking for, RefNo. But I can see the value in the sheet (and the Vlookup works too) and if I replace the line
Code:
RefNo = Form.txtReferenceNumber.Value
Code:
RefNo="123"
and Step into the code (F8) then it works. I though maybe the RefNo from the form isn't numeric but I checked with an IsNumeric() and it's a number, same as the spreadsheet. I haven't really used cells.find before but everything except the "What" is optional so I don't think I am missing something from its syntax. Can't figure out what the problem is. Any ideas?
Thanks,
Sinon