banner



excel vba option compare binary

Make VBA String Comparisons Case In-sensitive [Quick Tip]

Chandoo

Chandoo

Today, while answering a reader's electronic mail, I wrote this VBA lawmaking,

If Target.Value = "yep" So
'do something
End If

Only I realized that my code would run only the Target cell has "yes" in information technology. It wont run if the target cell has "Yes", or "Yes" or "YeS".

This is because by default, all VBA comparisons are binary . That ways, "yes" ≠ "Yes".

One quick work-effectually for this problem is to use UCASE to convert target.value to Capital and then compare, like this,

If UCASE(Target.Value) = "Yes" And then
'practise something
Finish If

But this seemed painful, especially, if I had to practise similar comparison at multiple places in my lawmaking, I had to use UCASE() everywhere.

If only at that place is an option to tell VBA how to compare?!?

Well, there is an option.

Use Option Compare Text

If you write Option Compare Text at the superlative of your module, all the VBA comparisons with in that module will employ Text comparison instead of Binary comparing. Thus, "yes" volition exist equal to "Yeah".

Do you lot use Option Compare?

There are iii settings for Option Compare.

  1. Selection Compare Binary: This is the default setting. Compares everything at binary level.
  2. Option Compare Text: Used for situations like this.
  3. Option Compare Database: Tin can be used simply with MS Access VBA. Uses Database Table settings to decide how to compare.

This is the beginning time I have used Option Compare Text. Just information technology seems like an elegant way to tell Excel VBA how to compare. I will be using it more often.

What about you lot? Do you use Option Compare? What are your favorite tips & tricks? Please share with usa using comments.

More on Excel VBA

VBA (or Macros) is how you lot tin tell Excel to automate parts of your work. It is a powerful programming linguistic communication built right in to Excel (and other MS Office applications) to help yous do more. If you are new to VBA, why don't you get thru our Free crash course?

  1. Introduction to VBA & Excel Macros
  2. Understanding Variables, Conditions & Loops in VBA
  3. Using Cells, Ranges & Other Objects in your Macros
  4. Putting information technology all together – Your First VBA Application using Excel
  5. My Acme 10 Tips for Mastering VBA & Excel Macros

Also, go thru our VBA (Macros) article collection for more tips, tutorials & ideas.

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get Costless Excel + Power BI Tips

Uncomplicated, fun and useful emails, once per week.

Larn & be crawly.

Welcome to Chandoo.org

Thank you and then much for visiting. My aim is to make you lot awesome in Excel & Ability BI. I do this past sharing videos, tips, examples and downloads on this website. At that place are more than one,000 pages with all things Excel, Ability BI, Dashboards & VBA here. Go alee and spend few minutes to be Crawly.

Read my story • Gratuitous Excel tips book

Advanced Excel & Dashboards training - Excel School is here

Excel School fabricated me great at work.

5/v

Excel formula list - 100+ examples and howto guide for you

From unproblematic to circuitous, in that location is a formula for every occasion. Cheque out the list at present.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It'south all hither.

Still on fence most Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

Monthly calendar - Excel Template

  • Excel for beginners
  • Avant-garde Excel Skills
  • Excel Dashboards
  • Complete guide to Pivot Tables
  • Tiptop 10 Excel Formulas
  • Excel Shortcuts
  • #Awesome Budget vs. Actual Chart
  • 40+ VBA Examples

Related Tips

half dozen Responses to "Make VBA String Comparisons Instance In-sensitive [Quick Tip]"

  1. Rick Rothstein (MVP - Excel) says:

    Another way to examination if Target.Value equal a string constant without regard to letter casing is to utilise the StrCmp function...

    If StrComp("yes", Target.Value, vbTextCompare) = 0 Then
    ' Do something
    End If

    • Fares Al-Dhabbi says:

      That's a cool way to compare. i only converted my values to strings and used the above code to compare. worked nicely

      Thanks!

  2. Tim says:

    In case that option just needs to exist used for a single comparison, you could utilize

    If InStr(1, "yes", Target.Value, vbTextCompare) Then
    'practise something
    End If

    every bit well.

  3. Luke M says:

    Nice tip, cheers! I never even thought to think there might exist an easier mode.

  4. Cyril Z. says:

    Regarding Chronology of VB in full general, the Option Compare pragma appears at the very beginning of VB, way before classes and objects arrive (with VB6 - around 2000).

    Today StrComp() and InStr() function offers a more than local style to compare, fully object, thus more than consequent with object programming (even if VB is still interpreted).

    My only question hither is : "what if you want to binary compare locally with re-entering functions or concurrency (with events) ?". This volition lead to a real nightmare and probably a big nasty mess to debug.

    By the way, congrats for you Millions/calendar month visits 🙂

  5. Bhavik says:

    This is nice article.
    I used these examples to aid my understanding. Even Instr is like to Find but it can be example sensitive and also case insensitive.
    Hope the examples below assist.

    Public Sub CaseSensitive2()

    If InStr(1, "Look in this string", "look", vbBinaryCompare) = 0 So
    MsgBox "woops, no match"
    Else
    MsgBox "at least one match"
    Stop If

    Terminate Sub

    Public Sub CaseSensitive()

    If InStr("Wait in this string", "wait") = 0 And so
    MsgBox "woops, no friction match"
    Else
    MsgBox "at least 1 lucifer"
    Terminate If

    Cease Sub
    Public Sub NotCaseSensitive()
    'doing alot of example insensitive searching and whatnot, you can put Choice Compare Text
    If InStr(1, "Wait in this string", "await", vbTextCompare) = 0 So
    MsgBox "woops, no friction match"
    Else
    MsgBox "at to the lowest degree one match"
    End If

    Stop Sub

Leave a Answer

Source: https://chandoo.org/wp/case-in-sensitive-string-compare-vba/

Posted by: jenkinswasuff.blogspot.com

0 Response to "excel vba option compare binary"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel