+ Reply to Thread
Results 1 to 7 of 7

Search For string in TextBoxes

Hybrid View

  1. #1
    Registered User
    Join Date
    08-12-2013
    Location
    Denton, Texas
    MS-Off Ver
    Excel 2010
    Posts
    89

    Search For string in TextBoxes

    Hello,

    I am trying to loop through a group of TextBoxes. If the TextBox contains the string "FAIL" in it then the next line of code should execute. But I seem to be doing something wrong . I figured that I could use an array to accomplish this. Below is what I have so far...

    Thank you!

    strTxt1 = Me.txtNotes1.Text
    strTxt2 = Me.txtNotes2.Text
    strTxt3 = Me.txtNotes3.Text
    strTxt4 = Me.txtNotes4.Text
    strTxt5 = Me.txtNotes5.Text
    strTxt6 = Me.txtNotes6.Text
    strTxt7 = Me.txtNotes7.Text
    strTxt8 = Me.txtNotes8.Text
    strTxt9 = Me.txtNotes9.Text
    strTxt10 = Me.txtNotes10.Text
    strTxt11 = Me.txtNotes11.Text
    strTxt12 = Me.txtNotes12.Text
    strTxt13 = Me.txtNotes13.Text
    strTxt14 = Me.txtNotes14.Text
    strTxt15 = Me.txtNotes15.Text
    strTxt16 = Me.txtNotes16.Text
    strTxt17 = Me.txtNotes17.Text
    strTxt18 = Me.txtNotes18.Text
    strTxt19 = Me.txtNotes19.Text
    strTxt20 = Me.txtNotes20.Text
    strTxt21 = Me.txtNotes21.Text
    strTxt22 = Me.txtNotes22.Text
    
    
    Dim strTxtArry As Variant
    Dim i As Integer
    strTxtArry = Array(strTxt1, strTxt2, strTxt3, strTxt4, strTxt5, strTxt6, strTxt7, strTxt8, strTxt9, strTxt10, strTxt11, strTxt12, strTxt13, strTxt14, strTxt15, strTxt16, strTxt17, strTxt18, strTxt19, strTxt20, strTxt21, strTxt22)
    
    For i = 0 To UBound(strTxtArry)
    If InStr("FAIL", strTxtArry(i)) Then
    
    'Code Goes Here
    
    Next i

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Search For string in TextBoxes

    Why not read the values directly into the strTxtArray

    Dim strTextArray(0 to 21) As String
    
    For i = 0 to 21
        strTextArray(i) = Me.Controls("TextBox" & (i + 1)).Text
    Next i
    The arguments in InStr are in the wrong order. Also, InStr is case sensitive, that might also throw some things off

    If InsStr(1, strTxtArray(i), "FAIL") <> 0 Then
    ' or
    If InStr(1, LCase(strTxtArray(i)), "fail") <> 0 Then
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    08-12-2013
    Location
    Denton, Texas
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Search For string in TextBoxes

    Hey Mikerickson,

    The only problem I worry about is I have about 50 more other Textboxes that I don't want to loop thru'. By using the generic "TextBox" in the code wont it loop thru' everything when using the below?

    Dim strTextArray(0 to 21) As String
    
    For i = 0 to 21
        strTextArray(i) = Me.Controls("TextBox" & (i + 1)).Text
    Next i

  4. #4
    Registered User
    Join Date
    08-12-2013
    Location
    Denton, Texas
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Search For string in TextBoxes

    I am doing something wrong because I'm getting a subscript out of range error.

    Dim strTextArray(0 To 21) As String
    
    For i = 0 To 21
        strTextArray(i) = Me.Controls("txtNotes" & (i + 1)).Text
    Next i
    
    If InStr(1, strTextArray(i), "FAIL") <> 0 Then
    MsgBox ("Found It")
    Else
    MsgBox

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,636

    Re: Search For string in TextBoxes

    Notice that the line: If InStr(1, strTextArray(i), "FAIL") <> 0 Then
    is outside of the For..Next loop, so the the value of i is 22 and the array has no element 22. The Next i should be after the line Msgbox...
    Ben Van Johnson

  6. #6
    Registered User
    Join Date
    08-12-2013
    Location
    Denton, Texas
    MS-Off Ver
    Excel 2010
    Posts
    89

    Re: Search For string in TextBoxes

    <---shaking my head in embarrassment...

    Sorry, really long night while I juggle one project from another.

    Thank you!

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Search For string in TextBoxes

    You can do this with a single loop. Eve

        Dim Flag As Boolean
        Dim i As Long
        
        Flag = False
        For i = 1 To 22
            Flag = Flag Or (UCase(Me.Controls("TextBox" & i).Text) Like "*FAIL*")
        Next i
        
        If Flag Then
            MsgBox "there is at least one Fail"
            ' do stuff
        End If

+ 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. [SOLVED] can not search textboxes
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-26-2013, 03:24 PM
  2. [SOLVED] textboxes change value on search
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-05-2013, 05:55 PM
  3. [SOLVED] Search for string across header row, then search for another string down found column
    By TucsonJack in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-14-2012, 02:09 PM
  4. Using a string to reverse fill Textboxes on a userform
    By VBA Noob in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-27-2009, 03:27 PM
  5. search a string withing a string : find / search hangs
    By itarnak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-24-2005, 11:05 AM

Tags for this Thread

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