Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VBA IF statement not case sensitive  (Read 3774 times)

0 Members and 1 Guest are viewing this topic.

raquelr

    Topic Starter


    Newbie

    • Experience: Beginner
    • OS: Windows 7
    VBA IF statement not case sensitive
    « on: January 09, 2017, 03:20:01 AM »
    Hi guys,

    I'm a beginner in VBA and I need your knowledge to help me with this code.
    i want to put it as case UNsensitive.
    can you type me the right code, please?



    Sub rename_raw()

    Dim i As Long

    For i = 2 To 70000
        If Cells(i, 3).Value = "Raw-material feed" Then
            If InStr(Cells(i, 1), "A") > 0 Or InStr(Cells(i, 1), "B") > 0 Then
                Cells(i, 9).Value = "AB"
             End If
        End If
    Next i

    End Sub


    thank you!!

    strollin



      Adviser
    • Thanked: 84
      • Yes
    • Certifications: List
    • Computer: Specs
    • Experience: Guru
    • OS: Windows 10
    Re: VBA IF statement not case sensitive
    « Reply #1 on: January 09, 2017, 12:06:38 PM »
    Quote
    Code: [Select]
    For i = 2 To 70000
        If Cells(i, 3).Value = "Raw-material feed" Then
            If InStr(Cells(i, 1), "A") > 0 Or InStr(Cells(i, 1), "B") > 0 Then
                Cells(i, 9).Value = "AB"
             End If
        End If
    Next i

    2 ways that should work:

    Code: [Select]
    For i = 2 To 70000
        If Cells(i, 3).Value = "Raw-material feed" Then
            If InStr(Cells(i, 1), "A") > 0 Or or InStr(Cells(i, 1), "a") > 0 InStr(Cells(i, 1), "B") > 0 Or or InStr(Cells(i, 1), "b") > 0 Then
                Cells(i, 9).Value = "AB"
             End If
        End If
    Next i

    Code: [Select]
    For i = 2 To 70000
        If Cells(i, 3).Value = "Raw-material feed" Then
            If InStr(UCASE(Cells(i, 1)), "A") > 0 Or InStr(UCASE(Cells(i, 1)), "B") > 0 Then
                Cells(i, 9).Value = "AB"
             End If
        End If
    Next i

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBA IF statement not case sensitive
    « Reply #2 on: January 09, 2017, 01:29:04 PM »
    InStr also supports a comparison argument, though you have to also provide the optional starting position argument if it is specified, so it doesn't end up any shorter than forcing case before the compare.

    Code: [Select]
    For i = 2 To 70000
        If Cells(i, 3).Value = "Raw-material feed" Then
            If InStr(1,Cells(i, 1), "A", vbTextCompare) > 0 Or InStr(1, Cells(i, 1), "B", vbTextCompare) > 0 Then
                Cells(i, 9).Value = "AB"
             End If
        End If
    Next i
    I was trying to dereference Null Pointers before it was cool.