VBA/Excel/Access/Word/Data Type/String Function

Материал из VB Эксперт
Перейти к: навигация, поиск

Format() Formats strings, dates, and numbers as specified.

 
Sub strDemo15()
   Debug.Print Format("ABCD", "<")
End Sub



InStr(): A number representing the place value of a particular character within a string

 
Sub InstrLeft()
    Dim userName As String
    Dim firstName As String
    Dim spaceLoc As Integer
    userName = "First Last"
    spaceLoc = InStr(1, userName, " ")
    firstName = Left(userName, spaceLoc - 1)
    Debug.Print firstName
End Sub



Instr function returns the position where one string begins within another string

 
Sub InstrExample()
  Debug.Print InStr("A Balter", "Balter") "Returns 8
  Debug.Print InStr("Hello", "l") "Returns 3
  Debug.Print InStr("c:\my documents\my file.txt", "\") "Returns 3
End Sub



InStr() Returns the location of the first character of a substring within another string.

 
Sub strDemo8()
   Debug.Print InStr("Test", "e")
End Sub



InStrRev begins searching at the end of a string and returns the position where one string is found within another string:

 
Sub InstrRevExample()
    Debug.Print InStrRev("c:\my documents\my file.txt", "\")
End Sub



LCase() Converts the string to lowercase.

 
Sub strDemo10()
   Debug.Print lcase("ABCD")
End Sub



Left(string, length) returns the specified number of characters from the left end of a string

 
Sub leftDemo()
    Dim strPhone As String
    strPhone = "123-1231233"
    Dim strArea As String
    strArea = Left(strPhone, 3)
    
    Debug.Print strArea
End Sub



Len() Returns the number of characters in a string.

 
Sub strDemo9()
   Debug.Print Len("Test")
End Sub



Len() return the number of characters in a string

 
Sub lenDemo()
    Dim userName As String
    Dim firstName As String
    Dim spaceLoc As Integer
    Dim strLength As Integer
    
    userName = "First Last"
    spaceLoc = InStr(1, userName, " ")
    firstName = Left(userName, spaceLoc - 1)
    strLength = Len(firstName)
    Debug.Print strLength
End Sub



Mid(string, start[, length]) returns the specified number of characters from inside the given string

 
Sub midDemo()
    Dim strPhone As String
    strPhone = "123-1231233"
    
    Dim strLocalExchange As String
    strLocalExchange = Mid(strPhone, 4, 3)
    
    Debug.Print strLocalExchange
End Sub



Right(string, length) returns a specified number of characters from the right end of a string

 
Sub rightDemo()
    Dim strPhone As String
    strPhone = "123-1231233"
    
    Dim strLocalNumber As String
    strLocalNumber = Right(strPhone, 7)
    Debug.Print strLocalNumber
End Sub



StrReverse() Reverses the order of the characters in a string.

 
Sub strDemo14()
   Debug.Print StrReverse("Test")
End Sub



UCase() Converts the string to uppercase.

 
Sub strDemo11()
   Debug.Print UCase("abcd")
End Sub



UCase returns a string that is all uppercase

 
Sub UCaseExample()
    Debug.Print UCase$("Hello World") "Prints HELLO WORLD
End Sub



Using LTrim, RTrim, and Trim to Trim Spaces from a String

 
Sub TrimDemo()
    Dim strUntrimmed As String, strTrimmed As String
    strUntrimmed = Selection.Text
    strTrimmed = Trim(strUntrimmed)
End Sub