I would make the code more 'inefficient' to start with (You'll never notice any difference)
Sub deletequotefromdatabase()
Dim Quote As Variant
Dim r As Excel.Range
Dim wb As Excel.Workbook
'grab quote number from quote form
Quote = Workbooks("testsource.xlsm").Sheets(1).Range("B1").Value
'search for quote number in the database
Set wb = Workbooks.Open("S:\Sales Group\Quotes\TestDatabase.xlsx")
Set r = wb.Sheets("sheet1").Cells.Find(What:=Quote, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not r Is Nothing Then
r.EntireRow.Delete
Else
MsgBox "Not found...", vbExclamation
End If
'// Remainder of code
End Sub
Alternatively, you could just do sometihng like
Sub deletequotefromdatabase1()
Dim Quote As Variant
On Error GoTo Catch
'grab quote number from quote form
Quote = Workbooks("testsource.xlsm").Sheets(1).Range("B1").Value
'search for quote number in the database
With Workbooks.Open("S:\Sales Group\Quotes\TestDatabase.xlsx").Sheets("sheet1")
.Cells.Find(What:=Quote, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).EntireRow.Delete
End With
'save and close the dataabase
Workbooks("TestDatabase.xlsx").Save
Workbooks.Open("S:\Sales Group\Quotes\TestDatabase.xlsx").Close
Exit Sub
Catch:
MsgBox "Not found..."
End Sub
But you'll need to check the error number to make it is not some other error (like unable to open the file...), but the first method would be preferred.
If you actually read the line I mentioned above, you'll see it is opening a workbook only to close it immediately - just seems a little odd.
Bookmarks