Right, Right$, RightB, RightB$ Functions

Named Arguments

Yes

Syntax

Right(string, length)

string

Use: Required

Data Type: String

The string to be processed.

length

Use: Required

Data Type: Variant (Long)

The number of characters to return from the right of the string.

Return Value

A string or variant of subtype String.

Description

Returns a string containing the rightmost length characters of string.

Rules at a Glance

  • If length is 0, a zero-length string ("") is returned.

  • If length is greater than the length of string, string is returned.

  • If length is less than zero or is Null, an error is generated.

  • If string contains a Null, Right returns Null.

Example

The following function assumes it's passed either a filename or a complete path and filename, and returns the filename from the end of the string:

Private Function ParseFileName(strFullPath As String)

Dim lngPos As Long, lngStart As Long
Dim strFilename As String

lngStart = 1
Do
   lngPos = InStr(lngStart, strFullPath, "\")
   If lngPos = 0 Then
      strFilename = Right(strFullPath, Len(strFullPath) - lngStart + 1)
   Else
      lngStart = lngPos + 1
   End If
Loop While lngPos > 0

ParseFileName = strFilename

End Function

Programming Tips and Gotchas

  • Use the Len function to determine the total length of string.

  • When you use the RightB function with byte data, length specifies the number of bytes to return.

See Also

Len Function, Left Function

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.