Two observations, though... I would add "As String" to Ron's function declaration and I don't think the "On Error Resume Next" statement is needed.
Those are both the way they are purposely, as I don't know what the OP wants to
do with conditions when there might be none or only one component to the
string.
With the "return" as a variant, then it can be set as an error value.
With it set as a string, it will return a blank.
The On Error Resume Next avoids having to do testing to see what's there.
Moving it earlier, and also setting the return to string, would return a blank
if there is no data, but the OP has not specified what he wants.
For example:
=================================
Option Explicit
Function ParseColon(str As String, Optional Part1 As Boolean = True) As String
'If Part1 = True, or is not specified, return first part only
'If Part1 = False, return first and second parts
Dim sTemp() As String
On Error Resume Next
sTemp = Split(str, ":")
ParseColon = sTemp(0)
If Part1 = False Then
ParseColon = ParseColon & ":" & sTemp(1)
End If
End Function
======================================
will return a blank if there is no data in the original.
If you'll allow for two lines, one could accomplish the same with :
=============================================
Function PC2(str As String, Optional Part1 As Boolean = True) As String
'If Part1 = True, or is not specified, return first part only
'If Part1 = False, return first and second parts
On Error Resume Next
PC2 = Split(str, ":")(0) & IIf(Part1, "", ":" & Split(str, ":")(1))
End Function
=============================
Without the On Error ... statement, the above would be a one-liner, but would
give a #VALUE error if the subject is blank.
Your one-liners are "neat", but I think overly complex in this instance:
ParseColon = Replace(Split(Replace(Str & String(-(Str = ""), ":"), ":",
"|", , 1 + Part1), ":")(0), "|", Left(":", -(Str <> "")))
Of course, you could always use my "hammer":
=====================================
Function PC4(str As String, Optional Part1 As Boolean = True) As String
Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = IIf(Part1, "(\w+)
\w+.*)?", "(\w+
\w+)?).*")
PC4 = re.Replace(str, "$1")
End Function
=======================================
--ron