Results 1 to 5 of 5

Thread: SentenceCase: Capitalize first letter of each sentence

  1. #1

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    SentenceCase: Capitalize first letter of each sentence

    Perhaps I still miss some features of an MS-Word's method. Please check/test. Any thought is welcome.
    vb Code:
    1. Public Function SentenceCase(sText As String) As String
    2.     Dim i As Long, bCap As Boolean, Ch As String * 1
    3.    
    4.     SentenceCase = LCase(sText)       '-- convert all to lowercase first
    5.     bCap = True
    6.     For i = 1 To Len(SentenceCase)
    7.         Ch = Mid$(SentenceCase, i, 1)
    8.         Select Case AscW(Ch)
    9.             Case 97 To 122 '-- a-z : separated and put on top as happens more often
    10.                 If bCap Then
    11.                     Mid$(SentenceCase, i, 1) = UCase(Ch)
    12.                     bCap = False
    13.                 End If
    14.             Case 33, 46, 63, 10, 13   '-- sentence terminators ! . ? Lf Cr
    15.                 bCap = True
    16.             Case 32, 160, 9           '-- space, non-break space, tab
    17.             Case 34, 41, 93, 125, 148 '-- closing quotes or brackets
    18.             Case Is < 128             '-- other chars between 0-127
    19.                 If bCap Then bCap = False
    20.             Case Else                 '-- Extended-Ascii (128-255) or Unicode (> 255)
    21.                 If bCap Then
    22.                     If StrComp(Ch, UCase(Ch), vbBinaryCompare) <> 0 Then
    23.                         '-- a letter that has uppercase.
    24.                         Mid$(SentenceCase, i, 1) = UCase(Ch)
    25.                     End If
    26.                     bCap = False
    27.                 End If
    28.         End Select
    29.     Next
    30. End Function
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  2. #2
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    355

    Re: SentenceCase: Capitalize first letter of each sentence

    Hm,, very neat function but it doesn't work correctly....

    It capitalizes, for example, everything that comes immediately after a period. So if you have a URL in your text it capitalizes all characters in the url as well.

    http://www.something.com/page.php

    becomes

    http://www.Something.Com/page.Php

  3. #3
    New Member
    Join Date
    Feb 2010
    Posts
    1

    Re: SentenceCase: Capitalize first letter of each sentence

    Hi
    I don't know if this would be of any help to you? It capitalizes the first letter in the sentence.

    here's the post with the dll to reference

  4. #4
    Junior Member Shadow-GK's Avatar
    Join Date
    Jul 2009
    Posts
    25

    Re: SentenceCase: Capitalize first letter of each sentence

    Quote Originally Posted by Deliriumxx View Post
    It capitalizes, for example, everything that comes immediately after a period. So if you have a URL in your text it capitalizes all characters in the url as well.

    http://www.something.com/page.php

    becomes

    http://www.Something.Com/page.Php
    Well, its not an intellect that determines if its a sentence or a link. nice job though.

  5. #5
    New Member
    Join Date
    Jul 2014
    Posts
    1

    Re: SentenceCase: Capitalize first letter of each sentence

    I'm not good at VB but this is the result I want in my excel. I tried for a lot of forums for macros with this output but in vain. Can you please create a macro for MS Excel 2010 with this logic. Thanks in advance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width