Consulting

Results 1 to 7 of 7

Thread: InStr - Not operating

  1. #1
    VBAX Regular
    Joined
    Nov 2016
    Posts
    17
    Location

    InStr - Not operating

    Hi,

    I'm running a script with the InStr function to find a string within a text in a specific cell. Let's that I want the code to enter the values "Nice" in M and "Cool" in N only if there is the string "Top" (not matter the upper cases) in K. I also included an error handler in case the cell is blank or any specific thing not managed by the InStr function.

    Problem is that when I run the code it doesn't do anything at all. Could you please help me with that ?

    My code is as follows :

    Option Compare as Text
    Private Sub GoButton_Click()
    Dim LastRow As Long
    Dim Row As Long
    Row = 2
    LastRow = wkb.Sheets("Working Data").Cells(Sheets("Working Data").Rows.Count, "A").End(xlUp).Row
    On Error GoTo Handler
    Do While Row <= LastRow
       If InStr(wkb.Sheets("Working Data").Cells(Row, "K"), "Top") <> 0 Then
            wkb.Sheets("Working Data").Cells(Row, "M") = "Nice"
            wkb.Sheets("Working Data").Cells(Row, "N") = "Cool"
       End If
       Row = Row + 1
    Handler : Row = Row + 1
    Loop
    End Sub
    Thanks a lot in advance !

    Kr,

    Christophe

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Using Instr
    Option Explicit
    Option Compare Text
    Private Sub GoButton_Click()
        Dim LastRow As Long
        Dim wkb As Workbook
        Dim Row As Long
        
        Set wkb = ThisWorkbook
        
        Row = 2
        LastRow = wkb.Sheets("Working Data").Cells(Sheets("Working Data").Rows.Count, "A").End(xlUp).Row
        On Error GoTo Handler
        Do While Row <= LastRow
            If InStr(wkb.Sheets("Working Data").Cells(Row, "K"), "Top") <> 0 Then
                wkb.Sheets("Working Data").Cells(Row, "M") = "Nice"
                wkb.Sheets("Working Data").Cells(Row, "N") = "Cool"
            End If
            Row = Row + 1
    Handler:          Row = Row + 1
        Loop
    End Sub
    Better with Find


    Sub test()
    Dim firstAddress As String
    Dim c As Range
    With Worksheets("Working Data").Range("K:K")
        Set c = .Find("top", LookIn:=xlValues, Lookat:=xlPart, MatchCase:=False)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Offset(, 2).Resize(, 2) = Array("Nice", "Cool")
                Set c = .FindNext(c)
                Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
    End Sub
    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
    Problem is that when I run the code it doesn't do anything at all.
    it works fine when I run it, however because you are incrementing your loop variable twice on each pass

           
     Row = Row + 1 
     Handler : Row = Row + 1
    you are only seeing even-numbered rows, and any occurrence of "Top" in an odd-numbered row is not seen.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Your original Code
    Row = 2 
        LastRow = Blah Blah .End(xlUp).Row '<-- Row = 2.    What is .End(xlUp).2?
    Yet another way
    Option Explicit 
    Option Compare Text 
    
    Private Sub GoButton_Click() 
        Dim Rw As Long 
         
        On Error GoTo Handler 
    
         With Sheets("Working Data")
        For Rw = 2 to .Cells(Rows.Count, "A").End(xlUp).Row 
            If InStr(LCase(.Cells(Rw, "K")), "top") <> 0 Then _
                .Cells(Rw, "M").Resize(, 2) = Array("Nice", "Cool")
           Handler:
        Next
    End With 
    End Sub
    Last edited by SamT; 05-09-2017 at 07:20 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Yet another way



    Option Explicit
    Option Compare Text '   <<<<< Not Compare AS Text
    
    Private Sub GoButton_Click()
        Dim c As Range, r As Range
        
        With Sheets("Working Data")
            Set r = Range(.Cells(1, "K"), .Cells(.Rows.Count, "K").End(xlUp))
        End With
        
        For Each c In r.Cells
            With c
                If InStr(.Value, "Top") <> 0 Then
                    .Offset(0, 2).Value = "Nice"
                    .Offset(0, 3).Value = "Cool"
                End If
            End With
        Next
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello Christophe,

    Rather than set the entire Module to ignore case when comparing text, you can choose how InStr will compare the data by setting the fourth argument Compare. This argument is optional. The default setting is Binary.

    The InStr function will return either a 0 (zero) or a positive integer. Error handling is not needed because InStr will never throw an error if the string being searched is empty.

    If the button is on the worksheet "Working Data", you can use this macro.
    Private Sub GoButton_Click()
    
    
        Dim Row As Long
            
            With GoButton.Parent
                For Row = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
                    If InStr(1, .Cells(Row, "K"), "Top", vbTextCompare) Then
                        .Cells(Row, "M").Resize(1, 2) = Array("Nice", "Cool")
                    End If
                Next Row
            End With
        
    End Sub
    If the button is not on the worksheet "Working Data" then you should this macro.
    Private Sub GoButton_Click()
    
    
        Dim Row As Long
            
            With Worksheets("Working Data")
                For Row = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
                    If InStr(1, .Cells(Row, "K"), "Top", vbTextCompare) Then
                        .Cells(Row, "M").Resize(1, 2) = Array("Nice", "Cool")
                    End If
                Next Row
            End With
        
    End Sub
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A filter solution
    Sub TestFilter()
        Dim r As Range
        With Worksheets("Working Data")
            Set r = Range(.Cells(1, 1), .Cells(Rows.Count, "A").End(xlUp)).Offset(, 10)
        End With
        
        r.AutoFilter Field:=1, Criteria1:="=*top*"
        Set r = r.Offset(1).Resize(r.Count - 1)
        r.Offset(, 2).Resize(, 2) = Array("Nice", "Cool")
        r.AutoFilter
    End Sub
    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'

Posting Permissions

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