This is what I did and it worked great! There is so much to learn and I
appreciate your feedback!
Tom
Dim sTemp As String
Dim i As Long
For i = 1 To Len(ScreenTextb)
sTemp = Mid(ScreenTextb, i, 1)
If sTemp Like "#" Then
CPPHONE = CPPHONE & sTemp
End If
Next i
CPPHONE = Format(CPPHONE, Right("(###) ###-####", 14 + 7 *
(Len(CPPHONE) = 7)))
"Rick Rothstein" wrote:
I went ahead and removed the function "RemoveUnderscores" which helped a
little. The scenario I'm giving is if there are no phone numbers to
grab,
the ELSE statement should provide an empty cell, but now it shows "( )-"
in
the cell.
Thank you!
Dim CPPHONE As String
ScreenTextB = CurrentScreenObject.getstring(18, 55, 12)
' RemoveUnderscores
If Len(ScreenTextB) = 12 Then
CPPHONE = "(" & Left(ScreenTextB, 3) & ") " &
Mid(ScreenTextB, 5, 3) & "-" & Right(ScreenTextB, 4)
Else
CPPHONE = ""
End If
Range("I32") = CPPHONE
The problem with your routine is that, as written, ScreenTextB will ALWAYS
be
12 characters long, whether or not there is a phone number in the string.
I think what you want is for the value you are operating on to be a phone
number.
One way to handle the problem would be to remove all of the non-numeric
characters from ScreenTextB before processing it.
I don't know what the "spacers" are in CurrentScreenObject, so I chose to
include only those values which are numeric in the routine below. But, if
you
know for sure, that the only non-numeric characters might be <underscore
or
<space, or <nbsp, for example, you could probably write a simpler
routine.
But try this:
=======================
Sub PhoneString(ScreenTextB)
Dim CPPHONE As String
Dim sTemp As String
Dim i As Long
For i = 1 To Len(ScreenTextB)
sTemp = Mid(ScreenTextB, i, 1)
If sTemp Like "#" Then
CPPHONE = CPPHONE & sTemp
End If
Next i
CPPHONE = Application.WorksheetFunction.Text _
(CPPHONE, "[<=9999999]###-####;(###) ###-####")
Debug.Print CPPHONE
End Sub
==========================
The OP may want to consider this slightly shorter alternative to the
next-to-the-last statement from your code which uses the native VB functions
instead...
CPPHONE = Format(CPPHONE, Right("(###) ###-####", _
14 + 7 * (Len(CPPHONE) = 7)))
--
Rick (MVP - Excel)