Thread: String help
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default String help

Better solution than my previous post. I didn't catch that you wanted the
final count, but just a string you could work with easier. This does it all.

Sub CountHomeRuns()
' Trims unwanted characters from a delimited HR text string
' Then replaces the string with the HR count in the string

Dim iPos1 As Integer, iPos2 As Integer, iRuns As Integer
Dim j As Long
Dim rngTextToTrim As Range
Dim s As Variant, sHR As Variant

Set rngTextToTrim = Selection

'Remove unwanted data
For j = 1 To rngTextToTrim.Cells.Count
With rngTextToTrim
sHR = .Cells(j).Value
Do
iPos1 = InStr(1, sHR, " (", vbTextCompare)
If iPos1 = 0 Then Exit Do
iPos2 = InStr(1, sHR, ")", vbTextCompare)
sHR = Left$(sHR, iPos1 - 1) & Mid$(sHR, iPos2 + 1)
Loop

'Get the count
iRuns = 0
For Each s In Split(sHR, ",", , vbTextCompare)
If IsNumeric(Right(s, 1)) Then
iRuns = iRuns + CInt(Right(s, 1))
Else
iRuns = iRuns + 1
End If
Next
.Cells(j).Value = iRuns
End With
Next

End Sub

Regards,
GS