Consulting

Results 1 to 3 of 3

Thread: Stop FIND() errors - related to my late post

  1. #1

    Stop FIND() errors - related to my late post

    Hi guys!

    I've used some code that you guys have helped me on earlier.

    [VBA]
    y = UserForm1.TextBox1
    If Cells(i, 1) = "" Then
    Worksheets("instructionsreceived").Columns(1).Find(y, lookat:=xlPart).EntireRow.Range("A1,C1,F1,G1,J1,K1").Copy

    Worksheets("Sheet1").Cells(i, 1).Select
    Worksheets("Sheet1").Paste
    [/VBA]

    This is great, BUT if there is a number that is not in "instructionsreceived".Columns(1), an error message comes up and it wants debugging on that line of code.

    Is there anyway I can put an if it can't find then Warning.Show

    Rather than having the VBA/Excel error message come up??

    (Warning being my own userform that informs the user that the number they're trying to find does not exist in the database.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Set a variable to the found range (if any) and test for Nothing

    Dim c As Range, y As String
    [VBA]y = "Test"
    Set c = Worksheets("Sheet1").Columns(1).Find(y, lookat:=xlPart)
    If Not c Is Nothing Then c.Range("A1,C1,F1,G1,J1,K1").Copy Worksheets("Sheet2").Cells(1, 1)
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Will show the userform:

    [VBA]

    'Allow not found error
    On Error Resume Next

    If Cells(i, 1) = "" Then
    'Atempt to find
    Worksheets("instructionsreceived").Columns(1).Find(y, lookat:=xlPart).EntireRow.Range("A1,C1,F1,G1,J1,K1").Copy

    If Err = 0 Then
    'Error = 0 = found
    Worksheets("Sheet1").Cells(i, 1).Paste
    Else
    'Not found. Reset for next
    Err.Clear
    'Inform user
    'warning.Show
    'Reset Errorhandler to 0 or whatever...
    On Error GoTo 0
    End If
    End If

    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •