+ Reply to Thread
Results 1 to 7 of 7

Thread: Instr Function

  1. #1
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    122

    Instr Function

    Hi,
    This is a portion of a larger Save event for a userform:

    On the userform the user chooses a combobox value.
    If the user chooses, in this case, Sundried Tomatoes,
    I would like to
    1. determine if Textbox12 contains text
    2. compare that text to a number of terms (jar, jarred, packed in oil)

    If any of theses terms are present, to raise a MsgBox event informing the user what to do.
    Then, once the OK button is clicked, run a Clear_MultiPageControls sub.

    If the condition is false just move along with the rest of the code.

    My code does not seem to either check for the terms or raise the MsgBox event.

    How do I fix this?

    Case 2 'page 3  DRIED FRUITS
                Cells(lRow, 2).Value = Me.ComboBox4.Value
    
                    If Me.ComboBox4.Text = "Sundried Tomatoes" And _
                         InStr(TextBox12.Text, "jar jarred packed in oil") > 0 Then
                        
                        MsgBox "For Jarred, Oil Packed or any other prepared type of Sundried Tomato, please list under OTHER"
                        
                          Exit Sub
                             Clear_MultiPageControls
                        
                    ElseIf Len(Me.TextBox12.Text) <> 0 Then
                       Cells(lRow, 2).Value = Me.ComboBox4.Value & ", " & TextBox12.Value
                    
                    If Len(Me.TextBox13.Text) <> 0 Then
                        Cells(lRow, 2).Value = Me.ComboBox4.Value & ", " & TextBox12.Value & " - " & TextBox13.Value
                
                        End If
                             End If

    Thanks
    Mark
    Last edited by max57; 05-01-2010 at 03:48 PM.

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Instr Function

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Instr Function

    InStr(TextBox12.Text, "jar jarred packed in oil") > 0
    That expression will only return True if the entire phrase "jar jarred packed in oil" appears in the textbox.

    This line will never be executed with the Exit Sub appearing immediately above it:
    Clear_MultiPageControls
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: Instr Function

    Shg,


    RE: Textbox -- In this instance, is there a method by which I could enter multiple terms to be checked against?

    RE: Clear_MultipageControls: I will change that

  5. #5
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Instr Function

        If Me.ComboBox4.Text = "Sundried Tomatoes" And _
            (InStr(TextBox12.Text, "jar") Or _
            InStr(TextBox12.Text, "packed in oil") Or _
            InStr(TextBox12.Text, "barbecued")) Then
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: Instr Function

    Thanks Shg,

    That did the trick!

    Here's the finished code:
            Case 2 'page 3  DRIED FRUITS
                Cells(lRow, 2).Value = Me.ComboBox4.Value
                    
                    If Me.ComboBox4.Text = "Sundried Tomatoes" And _
                         (InStr(TextBox12.Text, "jar") Or _
                            InStr(TextBox12.Text, "packed in oil") Or _
                                InStr(TextBox12.Text, "jarred")) Then
                    
                    MsgBox "For Jarred, Oil Packed or any other prepared type of Sundried Tomato, please list under OTHER"
                       Clear_MultiPageControls
                          Exit Sub
                             
                        
                    ElseIf Len(Me.TextBox12.Text) <> 0 Then
                       Cells(lRow, 2).Value = Me.ComboBox4.Value & ", " & TextBox12.Value
                    
                    If Len(Me.TextBox13.Text) <> 0 Then
                        Cells(lRow, 2).Value = Me.ComboBox4.Value & ", " & TextBox12.Value & " - " & TextBox13.Value
                
                        End If
                             End If
    Last edited by max57; 05-01-2010 at 03:53 PM.

  7. #7
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Instr Function

    Good job.

    Testing for jarred is redundant if you've tested for jar.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0