PDA

View Full Version : Counting Characters



Rakesh
05-06-2013, 09:40 AM
Hi Guys,

I had a Text with Tab Separator (Attached).
I will do the following process manually.

Count the characters of the text in between second tab to third tab.
If the character count is less than 10 go to the next line.
If the character count is more than 10 go to the 12th character.
Find the space reversely.
replace the space with “~~~”

Can anyone help me to do this using VB.

Thanks,
Rakesh

macropod
05-06-2013, 10:07 PM
Try:
Sub Demo()
Dim Para As Paragraph, Rng As Range, i As Long
Dim StrOut As String, StrTmp As String
With ActiveDocument
For Each Para In .Paragraphs
If Len(Split(Para.Range.Text, vbTab)(2)) > 10 Then
StrOut = ""
Set Rng = Para.Range
Rng.End = Rng.End - 1
For i = 0 To UBound(Split(Rng, vbTab))
If i <> 2 Then
StrOut = StrOut & Split(Rng, vbTab)(i) & vbTab
Else
StrTmp = Left(Split(Rng, vbTab)(i), 12)
StrTmp = Left(StrTmp, InStrRev(StrTmp, " ") - 1) & "~~~"
StrTmp = StrTmp & Mid(Split(Rng, vbTab)(i), Len(StrTmp) - 1, Len(Split(Rng, vbTab)(i)))
StrOut = StrOut & StrTmp & vbTab
End If
Next
Rng.Text = Left(StrOut, Len(StrOut) - 1)
End If
Next
End With
End Sub

fumei
05-06-2013, 11:22 PM
Nice

Rakesh
05-07-2013, 08:27 AM
Hi macropod,


It shows "Subscript out of range" error in the line.


If Len(Split(Para.Range.Text, vbTab)(2)) > 10 Then


Thanks,
Rakesh

macropod
05-07-2013, 08:45 AM
The code works fine with the sample strings you posted. I seem to recall that in at least two of the other threads I've helped you with, you've repeatedly changed your requirements after the code had been developed. I am not going to play that game again - it's a waste of my time.