StrComp Function

Named Arguments

Yes

Syntax

StrComp(string1, string2[, compare])

string1

Use: Required

Data Type: String

Any string expression

string2

Use: Required

Data Type: String

Any string expression

compare

Use: Optional

Data Type: Integer constant

The type of string comparison to perform.

Return Value

A Variant of subtype Integer.

Description

Determines whether two strings are equal and, if not, which of two strings has the greater value.

Rules at a Glance

  • The following intrinsic constants are available to use as the settings for the compare argument:

    Constant Value Comparison to perform
    vbBinaryCompare 0 Binary (default)
    vbTextCompare 1 Textual
    vbDatabase 2 Database (Microsoft Access only)
  • If compare isn't specified, the setting of Option Compare (if present) determines the type of comparison performed.

  • This table describes the possible return values from the StrComp function:

    Scenario Return Value
    string1 < string2 –1
    string1 = string2 0
    string1 > string2 1
    string1 or string2 is Null Null

Programming Tips and Gotchas

  • If you just need to know if string1 is greater than string2 (or vice versa), couldn't you simply use the < or > comparison operators? When you're dealing with strings of characters, Visual Basic sees each character as a number. Simply using the comparison operators therefore compares the numerical value of one string with the other. Take this scenario:

    Dim sString1 As String
    Dim sString2 As String
    
    sString1 = "hello world"
    sString2 = "HELLO WORLD"

    Subjectively, ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.