Find and Write data

G

Guest

This procedure is not working. After the procedure has run, I can go to
edit>find and it finds my value whats wrong here, please help. It displays
the message for error 91.

Private Sub CommandButton1_Click()

Dim x As Workbook

OrderToFind = ActiveSheet.Range("E17").Value
TrackingWorkbook = "\\Fs1\Material\Scheduling\OrderTracking.xls"
myvalue = ActiveSheet.Range("F15").Value


' Check if workbook is already opened.
On Error Resume Next
Set x = Workbooks("OrderTracking1")
If Err = 0 Then
Windows("OrderTracking1").Activate
Else
Workbooks.Open Filename:=TrackingWorkbook
End If

Sheets("To Finish").Select
'Find the order numbers comment line
Cells.Find(What:=OrderToFind & " Count", LookIn:=xlValues,
LookAt:=xlPart).Activate

If Err = "91" Then
MsgBox "Could not find Order#: " & OrderToFind
Exit Sub
End If

'Offset to the correct column and insert data
ActiveCell.Offset(, 32).Value = myvalue

End Sub
 
G

Guest

You are best not teo generate errors whenever possible (they have a lot of
overhead associated with them that slows things down) Here is some better
code that will avoid the error

dim rngFound as range

set rngFound = cells.find((What:=OrderToFind & " Count", LookIn:=xlValues,
LookAt:=xlPart)

if rngFound is nothing then
msgbox "Not Found"
else
rngFound.offset(0,32).value = myvalue
endif

set rngfound = nothing

You will have to paste this into your code as appropriate...

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top