Results 1 to 13 of 13

Thread: Capitalize first letter on sentence

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Capitalize first letter on sentence

    I'm using Excel and I don't have experience creating macros and scripts.

    I need to capitalize the first letter of each sentence, for example:

    "Capital first letter only. After Period Should be capitalized only The First Letter"

    to this:

    "Capital first letter only. After period should be capitalized only the first letter"

    How I do that? - a step-by-step will be really appreciated. I'm sorry I'm not a programmer, I just need help.

    thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Capitalize first letter on sentence

    Welcome to the forums.

    Is this what you mean?
    Code:
    Private Sub Command1_Click()
    Dim strString As String
    strString = "this is the string I will use."
    strString = StrConv(strString, vbProperCase)
    MsgBox strString
    End Sub

  3. #3
    Addicted Member
    Join Date
    Jul 2008
    Location
    Colorado
    Posts
    193

    Re: Capitalize first letter on sentence

    Hack, I copied and pasted your code (I hadn't seen vbProperCase before and was intrigued) into a new Excel sheet, and stepped through it and it capitalized the first letter of every word.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: Capitalize first letter on sentence

    Hack,
    Thank you for your help.

    The code capitalize first letter each word in the string. I'm not sure if doing right, I went to Vb and paste the code and hit F5. I not sure how implement it in excel.

    Thanks

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Capitalize first letter on sentence

    Ah, I misunderstood your question.

    That gets a bit more complicated. Have a look here at what anhn posted.

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: Capitalize first letter on sentence

    Thank you.

    Do you know if there is a step-by-step to implement this. I guess this is what I need but don't know how to do it.

    I'm sorry I'm not programmer.

    Thanks

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Capitalize first letter on sentence

    Quote Originally Posted by Hack
    That gets a bit more complicated. Have a look here at what anhn posted.
    Indeed, that thread is quite complicated.

    Another post of mine in CodeBank to do SentenceCase here.
    • 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

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Capitalize first letter on sentence

    Hi Jordan

    Would you like to try this simpler version

    http://www.vbforums.com/showpost.php...8&postcount=17
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: Capitalize first letter on sentence

    Hi koolsid,

    Can you describe step-by-step how running this code?

    Thanks

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Capitalize first letter on sentence

    Definitely


    I have commented the code so that it is easy to understand. how do you want to use it. Maybe I can explain a little bit more after that...

    vb Code:
    1. Sub ConvertToSentenceCase()
    2. 'Declare variables
    3. Dim TestString As String, strg2 As String
    4.  
    5. 'Example string that you want to convert to sentence case
    6. 'Replace this string with your string
    7. TestString = "wake me up before you go go. i want to have my breakfast! don't forget."
    8.  
    9. 'test the 1st 10 chars to get the first - incase the selection includes
    10. 'some spaces at the beginning.
    11. For X = 1 To 10
    12.   strg2 = Mid(TestString, X, 1)
    13.   If strg2 Like "[a-zA-Z]" Then
    14.     Mid(TestString, X, 1) = UCase(strg2)
    15.     Exit For
    16.   End If
    17. Next X
    18.  
    19. 'test criteria (.!?) for the end of a sentence, then Capitalize the next letter.
    20. For X = X To Len(TestString)
    21.   strg2 = Mid(TestString, X, 1)
    22.   If strg2 Like "[.!?]" Then
    23.     For Y = X + 1 To X + 10
    24.       strg2 = Mid(TestString, Y, 1)
    25.       If strg2 Like "[a-zA-Z]" Then
    26.         Mid(TestString, Y, 1) = UCase(strg2)
    27.         X = Y + 1
    28.         Exit For
    29.       End If
    30.     Next Y
    31.   End If
    32. Next X
    33.  
    34. 'This will give you the result
    35. MsgBox TestString
    36.  
    37. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Capitalize first letter on sentence

    What make your function simpler? Is it faster or slower? Why 10?

    Test your function with this:
    Code:
    TestString = "1.          wake me up. 2.    before YOu go. 12/09/2008: you Are dreaming.
    and then
    Code:
    TestString = "1. wake me Up" & vbCrLf & "2. before you go. 12/09: you ARE dreaming.
    Last edited by anhn; Aug 27th, 2008 at 03:47 AM.
    • 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

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    5

    Re: Capitalize first letter on sentence

    Well, I have a database and several columns are text cells. The text is like "All Words Start With..." and I want "All words start with....".
    The rows are over 5000.

    I know that going to VB editor I can enter your code, then return to excel, but then what?

    Suppossed I want to fix the problem in all cells in column A. What are the steps to do that?

    Thanks

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Capitalize first letter on sentence

    In that case you can change the above sub to a function and then use it to convert the case of the text while looping through your records...

    Let me change the code for you

    Edit:
    Okay here it is

    vb Code:
    1. 'You can use the function "ConvertToSentenceCase" to change the
    2. 'case of the text fields in the records
    3. Sub changecase()
    4.     Dim TestString As String
    5.     TestString = "wake me up before you go go. i want to have my breakfast! don't forget."
    6.     'you can loop here through the records of the database and then
    7.     'convert them to sentence case
    8.     MsgBox ConvertToSentenceCase(TestString)
    9. End Sub

    Paste the code below in a module...
    vb Code:
    1. 'Paste this in a module
    2. Function ConvertToSentenceCase(TestString As String) As String
    3. 'Declare variables
    4. Dim strg2 As String
    5.  
    6. 'test the 1st 10 chars to get the first - incase the selection includes
    7. 'some spaces at the beginning.
    8. For X = 1 To 10
    9.   strg2 = Mid(TestString, X, 1)
    10.   If strg2 Like "[a-zA-Z]" Then
    11.     Mid(TestString, X, 1) = UCase(strg2)
    12.     Exit For
    13.   End If
    14. Next X
    15.  
    16. 'test criteria (.!?) for the end of a sentence, then Capitalize the next letter.
    17. For X = X To Len(TestString)
    18.   strg2 = Mid(TestString, X, 1)
    19.   If strg2 Like "[.!?]" Then
    20.     For Y = X + 1 To X + 10
    21.       strg2 = Mid(TestString, Y, 1)
    22.       If strg2 Like "[a-zA-Z]" Then
    23.         Mid(TestString, Y, 1) = UCase(strg2)
    24.         X = Y + 1
    25.         Exit For
    26.       End If
    27.     Next Y
    28.   End If
    29. Next X
    30. ConvertToSentenceCase = TestString
    31. End Function

    If you have any queries, do ask

    Edit Sorry I missed the question you had on Column A

    After you have pasted the function in the module then type this formula in column B, Say cell B1

    =ConvertToSentenceCase(A1)
    Last edited by Siddharth Rout; Aug 27th, 2008 at 02:30 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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