Help in function creation

ADS_SDF

New Member
Joined
Dec 21, 2022
Messages
5
Platform
  1. Windows
Hello everyone I need to write a VBA function that performs the following tasks:

- enter "<p>" before each capital letter at the beginning of the sentence.
- enter "<strong>" before each capital letter within a sentence, and "</strong> at the end of that word.
- After each dot, type "</p>"

Can anyone help me?
Thank you so much!
 
Ok, so the mid-sentence capital M in Maree does not get a <strong> before it.

And a letters followed by "-" do not get a </strong> between but letters followed by "." or "," or "!" or "?" do get a </strong> between. Could we have definitive lists of other non-letter characters that do/don't get the </strong> before them? Are any of these characters possible in your data and if so which category do they fit in?
@ # $ % ^ & * ( ) _ { } [ ] < > : ; " ' / | \
Could digits be involved in the text? If so, some examples of data and results?

To help confirm the rules, the result for my second example in post 4 would be?
I forgot to answer to your second question.

The output of the sentence What is on ABC today? should be
<p>What is on <strong>ABC</strong> today?</p>
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
This sort of thing is notoriously difficult. A human can usually identify a sentence pretty easily but to tell Excel how to do that is not so easy. In a paragraph it would be easy to say that a break between sentences is identified by a "." followed by a space followed by an upper case letter, but ..
a. As demonstrated in my examples, a sentence can end with characters other then "."
b. A "." followed by a space followed by an upper case letter does not necessarily mean a sentence break anyway (eg "I like poetry by J. Blake and his brother.")

There is also some discrepancy in your example below
The result of the function I would need is
<p>Today is <strong>Wednesday</strong>, <strong>December</strong>21. </p><p>Tomorrow will be <strong>Thrusday</strong>!</p><p>Is that you <strong>Ann-Maree</strong>?</p>
You have retained the space character before the first "</p>" but omitted it before the second "</p>"

I have assumed that it should be omitted. So you could give this UDF a try.

VBA Code:
Function AddTags(para As String) As String
  With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(^|[.?!] )(?=[A-Z])"
    AddTags = .Replace(para, "$1<p>")
    .Pattern = "([.?!] |$)"
    AddTags = .Replace(AddTags, "$1</p>")
    .Pattern = "( )(?=[A-Z])"
    AddTags = .Replace(AddTags, "$1<strong>")
    .Pattern = "(<strong>[^, .?!]+)([, .?!])"
    AddTags = Replace(.Replace(AddTags, "$1</strong>$2"), " </p", "</p")
  End With
End Function

Cell Formulas
RangeFormula
B1:B2B1=AddTags(A1)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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