+ Reply to Thread
Results 1 to 4 of 4

trouble with find values

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-14-2015
    Location
    Milan
    MS-Off Ver
    2013
    Posts
    110

    trouble with find values

    dear all,

    I use following code to find different values in a specific range.
    now I need to add the condition that,if the values are not found then the range have to be empty
    I try differen way but not working ...can someone help tks
    Dim sString As String
        Dim MyAr
        Dim i As Long
        Dim delRange As Range, aCell As Range
        Dim ws As Worksheet
    
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        '~~> Add more to the list here separated by "/"
        sString = "KIX/NRT/CGO"
    
        MyAr = Split(sString, "/")
    
        With ws
            For i = LBound(MyAr) To UBound(MyAr)
    
                Set aCell = .Range("G5:K5").Find(What:=MyAr(i), LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    
       
      If Not aCell Is Nothing Then
        Sheet1.Range("f24").Value = "***ADDITIONAL ADDRESSES NEEDED"
        Sheet1.Range("f12").Value = "***ADDITIONAL ADDRESSES NEEDED"
        Sheet1.Range("f14").Value = "***ADDITIONAL ADDRESSES NEEDED"
         
       
       
     End If
           Next i
    End With

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: trouble with find values

    Hello 9599lorenzo,

    Keep a count of the array elements that were not found. Compare that to the Ubound of the array. If they are equal then no matches were found.

    Example
        Dim sString As String
        Dim MyAr
        Dim i As Long
        Dim delRange As Range, aCell As Range
        Dim NotFound As Long
        Dim ws As Worksheet
    
            Set ws = ThisWorkbook.Sheets("Sheet1")
    
            '~~> Add more to the list here separated by "/"
            sString = "KIX/NRT/CGO"
    
            MyAr = Split(sString, "/")
    
            With ws
                For i = LBound(MyAr) To UBound(MyAr)
    
                    Set aCell = .Range("G5:K5").Find(What:=MyAr(i), LookIn:=xlValues, _
                        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False, SearchFormat:=False)
    
                    If Not aCell Is Nothing Then
                        Sheet1.Range("f24").Value = "***ADDITIONAL ADDRESSES NEEDED"
                        Sheet1.Range("f12").Value = "***ADDITIONAL ADDRESSES NEEDED"
                        Sheet1.Range("f14").Value = "***ADDITIONAL ADDRESSES NEEDED"
                    Else
                        NotFound = NotFound + 1
                    End If
                Next i
           
                If NotFound = UBound(MyAr) Then
                    MsgBox "No Matches were Found.", vbExclamation
                    Exit Sub
                End If
           
            End With
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    02-14-2015
    Location
    Milan
    MS-Off Ver
    2013
    Posts
    110

    Re: trouble with find values

    dear Ross,

    tks so much for your help .

    cheers
    Lorenzo

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: trouble with find values

    Hello 9599lorenzo,

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2014, 04:47 PM
  2. [SOLVED] Trouble with Find in VBA
    By Panda2000 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-24-2012, 05:26 PM
  3. trouble with vba code for Find, Insert copied cells and find next
    By jgelbach in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-26-2012, 01:45 PM
  4. Trouble with #N/A trying to find averages
    By adsxvii in forum Excel General
    Replies: 2
    Last Post: 11-26-2007, 09:39 AM
  5. Find/Replace trouble!
    By LACA in forum Excel General
    Replies: 3
    Last Post: 09-20-2006, 10:18 PM
  6. [SOLVED] Trouble with Find
    By vortex2k4 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-09-2006, 07:09 AM
  7. [SOLVED] Trouble with Find Method
    By Alan M in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-28-2005, 10:05 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1