MS Excel: How To Use The INSTR Function (VBA) - TechOnTheNet

TechOnTheNet Logo
  1. Home
  2. MS Excel
  3. Formulas / Functions
totn Excel Functions MS Excel: How to use the INSTR Function (VBA)

This Excel tutorial explains how to use the Excel INSTR function with syntax and examples.

Description

The Microsoft Excel INSTR function returns the position of the first occurrence of a substring in a string.

The INSTR function is a built-in function in Excel that is categorized as a String/Text Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.

Syntax

The syntax for the INSTR function in Microsoft Excel is:

InStr( [start], string, substring, [compare] )

Parameters or Arguments

start Optional. It is the starting position for the search. If this parameter is omitted, the search will begin at position 1. string The string to search within. substring The substring that you want to find. compare

Optional. It is the type of comparison to perform. It can be one of the following values:

VBA Constant Value Explanation
vbUseCompareOption -1 Uses option compare
vbBinaryCompare 0 Binary comparison
vbTextCompare 1 Textual comparison

Returns

The INSTR function returns a numeric value. The first position in string is 1. If substring is not found in string, then the INSTR function will return 0.

Note

  • When finding the location of a substring in a string, the INSTR function performs a case-sensitive search.

Applies To

  • Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The INSTR function can only be used in VBA code in Microsoft Excel.

Let's look at some Excel INSTR function examples and explore how to use the INSTR function in Excel VBA code:

InStr("Tech on the Net", "T") Result: 1 'Shows how start is defaulted to 1 if omitted InStr(1, "Tech on the Net", "T") Result: 1 InStr(1, "Tech on the Net", "t") Result: 9 'Shows that search is case-sensitive InStr(10, "Tech on the Net", "t") Result: 15 InStr(1, "Tech on the Net", "the") Result: 9 InStr(1, "Tech on the Net", "M") Result: 0 'Shows what is returned if substring is not found

For example:

Dim LPosition As Integer LPosition = InStr(10, "Tech on the Net", "t")

In this example, the variable called LPosition would now contain the value 15.

Share on:

Databases

  • SQL
  • Oracle / PLSQL
  • SQL Server
  • MySQL
  • MariaDB
  • PostgreSQL
  • SQLite

MS Office

  • Excel
  • Access
  • Word

Web Development

  • HTML
  • CSS
  • JavaScript
  • Color Picker

Programming

  • C Language

More

  • ASCII
  • Unicode
  • Linux
  • UNIX
  • Techie Humor
clear filter right caret

Lookup/Ref Functions

  • ADDRESS (WS)
  • AREAS (WS)
  • CHOOSE (WS, VBA)
  • COLUMN (WS)
  • COLUMNS (WS)
  • HLOOKUP (WS)
  • HYPERLINK (WS)
  • INDEX (WS)
  • INDIRECT (WS)
  • LOOKUP (WS)
  • MATCH (WS)
  • OFFSET (WS)
  • ROW (WS)
  • ROWS (WS)
  • TRANSPOSE (WS)
  • VLOOKUP (WS)
  • XLOOKUP (WS)
down caret

String/Text Functions

  • ASC (VBA)
  • BAHTTEXT (WS)
  • CHAR (WS)
  • CHR (VBA)
  • CLEAN (WS)
  • CODE (WS)
  • CONCAT (WS)
  • CONCATENATE (WS)
  • CONCATENATE with & (WS, VBA)
  • DOLLAR (WS)
  • EXACT (WS)
  • FIND (WS)
  • FIXED (WS)
  • FORMAT STRINGS (VBA)
  • INSTR (VBA)
  • INSTRREV (VBA)
  • LCASE (VBA)
  • LEFT (WS, VBA)
  • LEN (WS, VBA)
  • LOWER (WS)
  • LTRIM (VBA)
  • MID (WS, VBA)
  • NUMBERVALUE (WS)
  • PROPER (WS)
  • REPLACE (WS)
  • REPLACE (VBA)
  • REPT (WS)
  • RIGHT (WS, VBA)
  • RTRIM (VBA)
  • SEARCH (WS)
  • SPACE (VBA)
  • SPLIT (VBA)
  • STR (VBA)
  • STRCOMP (VBA)
  • STRCONV (VBA)
  • STRREVERSE (VBA)
  • SUBSTITUTE (WS)
  • T (WS)
  • TEXT (WS)
  • TEXTJOIN (WS)
  • TRIM (WS, VBA)
  • UCASE (VBA)
  • UNICHAR (WS)
  • UNICODE (WS)
  • UPPER (WS)
  • VAL (VBA)
  • VALUE (WS)
right caret

Date/Time Functions

  • DATE (WS)
  • DATE (VBA)
  • DATEADD (VBA)
  • DATEDIF (WS)
  • DATEDIFF (VBA)
  • DATEPART (VBA)
  • DATESERIAL (VBA)
  • DATEVALUE (WS, VBA)
  • DAY (WS, VBA)
  • DAYS (WS)
  • DAYS360 (WS)
  • EDATE (WS)
  • EOMONTH (WS)
  • FORMAT DATES (VBA)
  • HOUR (WS, VBA)
  • ISOWEEKNUM (WS)
  • MINUTE (WS, VBA)
  • MONTH (WS, VBA)
  • MONTHNAME (VBA)
  • NETWORKDAYS (WS)
  • NETWORKDAYS.INTL (WS)
  • NOW (WS, VBA)
  • SECOND (WS)
  • TIME (WS)
  • TIMESERIAL (VBA)
  • TIMEVALUE (WS, VBA)
  • TODAY (WS)
  • WEEKDAY (WS, VBA)
  • WEEKDAYNAME (VBA)
  • WEEKNUM (WS)
  • WORKDAY (WS)
  • WORKDAY.INTL (WS)
  • YEAR (WS, VBA)
  • YEARFRAC (WS)
right caret

Math/Trig Functions

  • ABS (WS, VBA)
  • ACOS (WS)
  • ACOSH (WS)
  • AGGREGATE (WS)
  • ASIN (WS)
  • ASINH (WS)
  • ATAN (WS)
  • ATAN2 (WS)
  • ATANH (WS)
  • ATN (VBA)
  • CEILING (WS)
  • CEILING.PRECISE (WS)
  • COMBIN (WS)
  • COMBINA (WS)
  • COS (WS, VBA)
  • COSH (WS)
  • DEGREES (WS)
  • EVEN (WS)
  • EXP (WS, VBA)
  • FACT (WS)
  • FIX (VBA)
  • FLOOR (WS)
  • FORMAT NUMBERS (VBA)
  • INT (WS, VBA)
  • LN (WS)
  • LOG (WS)
  • LOG (VBA)
  • LOG10 (WS)
  • MDETERM (WS)
  • MINVERSE (WS)
  • MMULT (WS)
  • MOD (WS)
  • MOD (VBA)
  • ODD (WS)
  • PI (WS)
  • POWER (WS)
  • PRODUCT (WS)
  • RADIANS (WS)
  • RAND (WS)
  • RANDBETWEEN (WS)
  • RANDOMIZE (VBA)
  • RND (VBA)
  • ROMAN (WS)
  • ROUND (WS)
  • ROUND (VBA)
  • ROUNDDOWN (WS)
  • ROUNDUP (WS)
  • SGN (VBA)
  • SIGN (WS)
  • SIN (WS, VBA)
  • SINH (WS)
  • SQR (VBA)
  • SQRT (WS)
  • SUBTOTAL (WS)
  • SUM (WS)
  • SUMIF (WS)
  • SUMIFS (WS)
  • SUMPRODUCT (WS)
  • SUMSQ (WS)
  • SUMX2MY2 (WS)
  • SUMX2PY2 (WS)
  • SUMXMY2 (WS)
  • TAN (WS, VBA)
  • TANH (WS)
  • TRUNC (WS)
right caret

Statistical Functions

  • AVEDEV (WS)
  • AVERAGE (WS)
  • AVERAGEA (WS)
  • AVERAGEIF (WS)
  • AVERAGEIFS (WS)
  • BETA.DIST (WS)
  • BETA.INV (WS)
  • BETADIST (WS)
  • BETAINV (WS)
  • BINOM.DIST (WS)
  • BINOM.INV (WS)
  • BINOMDIST (WS)
  • CHIDIST (WS)
  • CHIINV (WS)
  • CHITEST (WS)
  • COUNT (WS)
  • COUNTA (WS)
  • COUNTBLANK (WS)
  • COUNTIF (WS)
  • COUNTIFS (WS)
  • COVAR (WS)
  • FORECAST (WS)
  • FREQUENCY (WS)
  • GROWTH (WS)
  • INTERCEPT (WS)
  • LARGE (WS)
  • LINEST (WS)
  • MAX (WS)
  • MAXA (WS)
  • MAXIFS (WS)
  • MEDIAN (WS)
  • MIN (WS)
  • MINA (WS)
  • MINIFS (WS)
  • MODE (WS)
  • MODE.MULT (WS)
  • MODE.SNGL (WS)
  • PERCENTILE (WS)
  • PERCENTRANK (WS)
  • PERMUT (WS)
  • QUARTILE (WS)
  • RANK (WS)
  • SLOPE (WS)
  • SMALL (WS)
  • STDEV (WS)
  • STDEVA (WS)
  • STDEVP (WS)
  • STDEVPA (WS)
  • VAR (WS)
  • VARA (WS)
  • VARP (WS)
  • VARPA (WS)
right caret

Logical Functions

  • AND (WS)
  • AND (VBA)
  • CASE (VBA)
  • FALSE (WS)
  • FOR...NEXT (VBA)
  • IF (WS)
  • IF (more than 7) (WS)
  • IF (up to 7) (WS)
  • IF-THEN-ELSE (VBA)
  • IFERROR (WS)
  • IFNA (WS)
  • IFS (WS)
  • NOT (WS)
  • OR (WS)
  • OR (VBA)
  • SWITCH (WS)
  • SWITCH (VBA)
  • TRUE (WS)
  • WHILE...WEND (VBA)
right caret

Information Functions

  • CELL (WS)
  • ENVIRON (VBA)
  • ERROR.TYPE (WS)
  • INFO (WS)
  • ISBLANK (WS)
  • ISDATE (VBA)
  • ISEMPTY (VBA)
  • ISERR (WS)
  • ISERROR (WS, VBA)
  • ISLOGICAL (WS)
  • ISNA (WS)
  • ISNONTEXT (WS)
  • ISNULL (VBA)
  • ISNUMBER (WS)
  • ISNUMERIC (VBA)
  • ISREF (WS)
  • ISTEXT (WS)
  • N (WS)
  • NA (WS)
  • TYPE (WS)
right caret

Financial Functions

  • ACCRINT (WS)
  • ACCRINTM (WS)
  • AMORDEGRC (WS)
  • AMORLINC (WS)
  • DB (WS)
  • DDB (WS, VBA)
  • FV (WS, VBA)
  • IPMT (WS, VBA)
  • IRR (WS, VBA)
  • ISPMT (WS)
  • MIRR (WS, VBA)
  • NPER (WS, VBA)
  • NPV (WS, VBA)
  • PMT (WS, VBA)
  • PPMT (WS, VBA)
  • PV (WS, VBA)
  • RATE (WS, VBA)
  • SLN (WS, VBA)
  • SYD (WS, VBA)
  • VDB (WS)
  • XIRR (WS)
right caret

Database Functions

  • DAVERAGE (WS)
  • DCOUNT (WS)
  • DCOUNTA (WS)
  • DGET (WS)
  • DMAX (WS)
  • DMIN (WS)
  • DPRODUCT (WS)
  • DSTDEV (WS)
  • DSTDEVP (WS)
  • DSUM (WS)
  • DVAR (WS)
  • DVARP (WS)
right caret

Engineering Functions

  • BIN2DEC (WS)
  • BIN2HEX (WS)
  • BIN2OCT (WS)
  • COMPLEX (WS)
  • CONVERT (WS)
right caret

File/Directory Functions

  • CHDIR (VBA)
  • CHDRIVE (VBA)
  • CURDIR (VBA)
  • DIR (VBA)
  • FILEDATETIME (VBA)
  • FILELEN (VBA)
  • GETATTR (VBA)
  • MKDIR (VBA)
  • SETATTR (VBA)
right caret

Data Type Conv. Functions

  • CBOOL (VBA)
  • CBYTE (VBA)
  • CCUR (VBA)
  • CDATE (VBA)
  • CDBL (VBA)
  • CDEC (VBA)
  • CINT (VBA)
  • CLNG (VBA)
  • CSNG (VBA)
  • CSTR (VBA)
  • CVAR (VBA)

Home | About Us | Contact Us | Testimonials | Donate

While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.

Copyright © 2003-2025 TechOnTheNet.com. All rights reserved.

Từ khóa » Visual Basic Find Char In String