Help with maths

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,940
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
My maths is lousy and I can't quite work this out!
If each line in a comment starts at position
51
98
145
213
How can I calculate each lines length ?

I got the start position with
Code:
For f = 1 To Len(comment.text)
 If Mid(comment.text, f, 1) = vbLf Then Debug.Print f + 1
Next
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
This might be a better option for you.

VBA Code:
Sub LineLength()

Dim splitComment As Variant
Dim f As Long

splitComment = Split(Comment.Text, vbLf)

For f = LBound(splitComment) To UBound(splitComment)
    Debug.Print f, Len(splitComment(f))
Next i
 
Upvote 0
Yes, thanks but I also need to know the starting position of each line in the comment'
I thought there may be a formula, once having the length of each row.
 
Upvote 0
It would be easier if we knew how you are trying to use it.
You can do formula but you also need the length of the full string and the formula for the last item is slightly different to the other lines since your start position includes the previous vbLF

Book1
ABCD
1StartLine Feed PositionLine End of TextLine Length Excl vbLF
21504949
351979646
49814414346
514521221167
621330029987
7300Length End of String
Sheet2
Cell Formulas
RangeFormula
B2:B6B2=IF(N(B3)=0,A3,A3-1)
C2:C6C2=B2-1
D2:D6D2=C2-A2+1
 
Upvote 0
I'm changing the attributes (bold, non-bold) and colors of certain rows in a comment.
Using .characters(start, len). I'm splitting the comment on vblf and applying it to the rows that contain a colon.
Thanks for your example I'll try converting it to VBA.
PS I thought this would be easy but couldn't crack it!
 
Upvote 0
Does this help:

VBA Code:
Sub LineFormatting()

    Dim sComment As String
    Dim f As Long
    Dim sLine As String
    Dim StartPos As Long, EndPos As Long
    
    sComment = Comment.Text

    StartPos = 1
    For f = 1 To Len(sComment)
        If Mid(sComment, f, 1) = vbLf Then
            EndPos = f - 1
            sLine = Mid(sComment, StartPos, EndPos - StartPos + 1)
            If InStr(1, sLine, ":") Then
                ' Do something
                Debug.Print sLine, Len(sLine)
            End If
            StartPos = f + 1                            ' Technically not required when f = full length
        End If
    Next f
End Sub
 
Upvote 0
Solution
Yep! Thanks very much Alex. I should have been able to nail this but StartPos was the bit I just couldn't get, even though it must be 1 more than the VBLF
Here's the full procedure, all working, having confirmed first that comment text exists. CleanEnd removes any unwanted leading or trailing LF chars.
VBA Code:
Sub LineFormatting(theComment As Range)
    Dim sComment As String
    Dim f As Long
    Dim sLine As String
    Dim StartPos As Long, EndPos As Long
    With theComment
            sComment = CleanEnd(.Comment.Text)
            StartPos = 1
            With .Comment
                With .Shape
                    With .TextFrame
                        For f = 1 To Len(sComment)
                            If Mid(sComment, f, 1) = vbLf Then
                                EndPos = f - 1
                                sLine = Mid(sComment, StartPos, EndPos - StartPos + 1)
                                If InStr(1, sLine, ":") Then
                                    .Characters(StartPos, Len(sLine)).Font.Bold = True
                                    .Characters(StartPos, Len(sLine)).Font.Color = RGB(128, 0, 0) 'Brown
                                End If
                                StartPos = f + 1                            ' Technically not required when f = full length
                            End If
                        Next f
                    End With
                End With
            End With
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top