+ Reply to Thread
Results 1 to 6 of 6

VBA, search and return a message

Hybrid View

hluk VBA, search and return a... 07-11-2008, 03:42 PM
VBA Noob Maybe Sub FindPhrase()... 07-11-2008, 03:54 PM
hluk THANK YOU! I want to add... 07-14-2008, 12:44 PM
VBA Noob It use in a loop. 0 to Ubound... 07-14-2008, 01:14 PM
hluk Thanks for the prompt reply. ... 07-14-2008, 01:27 PM
VBA Noob Arr = Array("N o r m a l t... 07-14-2008, 01:32 PM
  1. #1
    Registered User
    Join Date
    07-09-2008
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    91

    VBA, search and return a message

    Hi there, thanks in advance for the help!

    So I imported a text file and now I want to search column "A" in a sheet "Info" to see if these phrases ("N o r m a l t e r m i n a t i o n", "E r r o r t e r m i n a t i o n") exist. Here is what I recorded:

    PHP Code: 
    Cells.Find(What:="N o r m a l    t e r m i n a t i o n"After:=ActiveCellLookIn:= _
            xlFormulas
    LookAt:=xlPartSearchOrder:=xlByRowsSearchDirection:= _
            xlNext
    MatchCase:=FalseSearchFormat:=False).Activate
        Cells
    .Find(What:="E r r o r   t e r m i n a t i o n"After:=ActiveCell_
            LookIn
    :=xlFormulasLookAt:=xlPartSearchOrder:=xlByRows_
            SearchDirection
    :=xlNextMatchCase:=FalseSearchFormat:=False).Activate
    Range
    ("B4").Select
        ActiveCell
    .FormulaR1C1 "The analysis resulted in ______" 
    After finding them, I want excel to display in "B4" in sheet "Info": "This analysis resulted in 'Normal termination'" OR "This analysis resulted in 'Error termination'".

    I picture the code being something like:
    Find "phrase". If phrase is found, display "str". Else find "other phrase". Display "other phrase".


    Regards,
    Harry

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Maybe

    Sub FindPhrase()
    Dim Arr As Variant
    Dim i As Long
    Dim Rng As Range
    Arr = Array("N o r m a l    t e r m i n a t i o n", "E r r o r   t e r m i n a t i o n")
       
    For i = 0 To UBound(Arr)
        With Sheets("Info")
            Set Rng = .Columns(1).Find(What:=Arr(i), After:=.Cells(1, "A"), LookIn:= _
                xlFormulas, LookAt:=xlPart)
            
            If Not Rng Is Nothing Then
                .Range("B4").Value = "This analysis resulted in " & Arr(i)
            End If
        End With
    Next i
    End Sub
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    07-09-2008
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    91
    THANK YOU!

    I want to add "else, if two two phrases aren't found, then display 'this analysis was not complete' ". Just trying to understand what the suggested code is doing (my comments are in ""):

    PHP Code: 
    Dim Arr As Variant  "this defines Arr as variable?"
    Dim i As Long  "this defines i as numbers?"
    Dim Rng As Range  "this defines Rng as range of values?"
    Arr = Array("N o r m a l    t e r m i n a t i o n""E r r o r   t e r m i n a t i o n")
       
    For 
    0 To UBound(Arr)  "what is UBound(Arr)?"
        
    With Sheets("Info")
            
    Set Rng = .Columns(1).Find(What:=Arr(i), After:=.Cells(1"A"), LookIn:= _
                xlFormulas
    LookAt:=xlPart)  "what is After:=?"
            
            
    If Not Rng Is Nothing Then  "i am not sure what this means"
                
    .Range("B4").Value "This analysis resulted in " Arr(i)
            
    End If

            Else 
    "this is what i added to get the 3 case phrase displayed"
                
    .Range("B4").Value "This analysis was not completed/ interrupted.  Please rerun or discard."

        
    End With
    Next i 

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    "what is UBound(Arr)?"
    It use in a loop. 0 to Ubound (highest available subscript for the indicated dimension of an array) which will be 1 as only two items e.g 0 and 1


    http://msdn.microsoft.com/en-us/libr...2f(VS.80).aspx

    "what is After:=?"
    The dot tells you it relates to Sheet Info. Then the rest is Row 1 of Col A eg Cell A1 in Sheets info
    :=.Cells(1, "A"),
    "i am not sure what this means"
    This statement says if it the Rng isn't empty then
    If Not Rng Is Nothing Then
    HTH

    VBA Noob

  5. #5
    Registered User
    Join Date
    07-09-2008
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    91
    Thanks for the prompt reply. I tried to add a 3rd condition to the script and it didn't turn out too well.

    So, from what I understand, the for loop tries to find the Arr(0) first. Then if Rng isn't empty, it will put value into cell B4. Then it tries Arr(1). However, now I need a third condition, a case where both "normal termination" and "error termination" aren't found.

    Is this what I should add? If so, where should I add this?
    PHP Code: 
    Else
                .
    Range("B4").Value "This analysis was not completed/ interrupted.  Please rerun or discard." 

  6. #6
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Arr = Array("N o r m a l    t e r m i n a t i o n", "E r r o r   t e r m i n a t i o n")
    The i statement loops throught the two conditions above (0 = Normal and 1= Error). If it finds both it will accept the last one.

    If none found I would use the Else statement for that.

    As you are finding out you need to spend more time thinking about all the options before posting a question

    VBA Noob

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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