Substring in VBA Substring in VBA vba vba

Substring in VBA


Shorter:

   Split(stringval,":")(0)


Test for ':' first, then take test string up to ':' or end, depending on if it was found

Dim strResult As String' Position of :intPos = InStr(1, strTest, ":")If intPos > 0 Then    ' : found, so take up to :    strResult = Left(strTest, intPos - 1)Else    ' : not found, so take whole string    strResult = strTestEnd If


You can first find the position of the string in this case ":"

'position = InStr(StringToSearch, StringToFind)position = InStr(StringToSearch, ":")

Then use Left(StringToCut, NumberOfCharacterToCut)

Result = Left(StringToSearch, position -1)