从长段落中提取90个字符的句子文本(不打断单词)的Excel公式。

huangapple go评论77阅读模式
英文:

Excel formula to extract 90 character sentence text (without breaking word) from a long paragraph

问题

我在Excel的A列中有非常长的段落。以下是单元格A1中的示例输入:

> "The Essential Accessory • Adds comfort and safety for
> passengers by offering back support and also helps carry luggage
> • Built for the long haul from high strength steel and
> heavy-duty mounting brackets • Uses stock mounting points for a
> no fabrication installation Includes: Sissy Bar, Hardware, Standard
> Freedom Sissy Bar Pad (Freedom pad matches Cobra's Freedom and
> Smoothee seats) Also Available (Sold Separately): 1. Jumbo Freedom
> Pad 2. Replacement Standard Freedom Pad 3. Fluted Backrest Insert 4.
> Swept Backrest Insert"

我想在B1单元格中提取完整的90个字符的句子(不打断单词),并在C1单元格中提取剩余的90个字符的完整句子(不打断单词)。我想将B1和C1的值用于Google文本广告的描述行。

有人可以为我提供Excel公式或VBA脚本来提取这些数据吗?由于某种原因,带有90个字符条件的LEFT函数会提取句子,但最后一个单词不是完整的单词(它被打断了)。

我尝试了下面的公式:

=LEFT(A1,90)

这给我这个值:The Essential Accessory • Adds comfort and safety for passengers by offering back s

然而,我需要的是:"The Essential Accessory • Adds comfort and safety for passengers by offering back" 这是一个完整的句子。

而在C1中,我想要这个值:"support and also helps carry luggage • Built for the long haul from high strength"

英文:

I have very long paragraphs in column A in excel. Example entry in cell A1 is mentioned below:

> "The Essential Accessory • Adds comfort and safety for
> passengers by offering back support and also helps carry luggage
> • Built for the long haul from high strength steel and
> heavy-duty mounting brackets • Uses stock mounting points for a
> no fabrication installation Includes: Sissy Bar, Hardware, Standard
> Freedom Sissy Bar Pad (Freedom pad matches Cobra's Freedom and
> Smoothee seats) Also Available (Sold Separately): 1. Jumbo Freedom
> Pad 2. Replacement Standard Freedom Pad 3. Fluted Backrest Insert 4.
> Swept Backrest Insert"

I would like to pull up to 90 character complete sentence text (without breaking word) in B1 cell and remaining 90 character complete sentence text (without breaking word) in C1 cell. I would like to utilize value from B1 and C1 into description lines of google text ads.

Is there anyone who can provide me excel formulas or VBA script who would pull that data? For some reason LEFT function with 90 character condition is pulling sentence, but the last word is not a complete word (its breaking).

I tried the below formula:

=LEFT(A1,90)

which give me this value: The Essential Accessory • Adds comfort and safety for passengers by offering back s

However, i exactly need: "The Essential Accessory • Adds comfort and safety for passengers by offering back" which is a complete sentence.

And in C1 I want this value: "support and also helps carry luggage • Built for the long haul from high strength"

答案1

得分: 2

如果您只想要第一行,最多90个字符,并且您使用的是Microsoft 365,您可以使用以下公式:

=TRIM(INDEX(LET(a,LEFT(A1,SEQUENCE(25,,91,-1)),b, FILTER(a,RIGHT(a)=" "),b),1))

如果您想解析整个文本,这是一个使用正则表达式将数据解析为适当行长度的VBA宏。

按照现有的编写方式,它会将结果写入所选单元格下方的单独单元格中,但您可以根据自己的要求进行更改。

我添加了一个显示行长度的列,但这不是宏的一部分。

Option Explicit
Sub WordWrap()
'requires reference to Microsoft VBScript Regular Expressions 5.5
'Wraps at W characters, but will allow overflow if a word is longer than W
Dim RE As RegExp, MC As MatchCollection, M As Match
Dim str As String
Dim W As Long
Dim rSrc As Range, C As Range
Dim mBox As Long
Dim I As Long
'with offset as 1, split data will be below original data
'with offset = 0, split data will replace original data
Const lDestOffset As Long = 1

Set rSrc = Selection
    If rSrc.Rows.Count <> 1 Then
        MsgBox ("You may only select" & vbLf & " Data in One (1) Row")
        Exit Sub
    End If
Set RE = New RegExp
    RE.Global = True
W = InputBox("Maximum characters in a Line: ", , 72)
    If W < 1 Then W = 79
For Each C In rSrc
str = C.Value
'remove all line feeds and nbsp
    RE.Pattern = "[\xA0\r\n\s]+"
    str = RE.Replace(str, " ")
    RE.Pattern = "\S.{0," & W - 1 & "}(?=\s|$)|\S{" & W & ",}"
    Debug.Print RE.Pattern
If RE.Test(str) = True Then
    Set MC = RE.Execute(str)
'see if there is enough room
I = lDestOffset + 1
Do Until I > MC.Count + lDestOffset
    If Len(C(I, 1)) <> 0 Then
        mBox = MsgBox("Data in " & C(I, 1).Address & " will be erased if you contine", vbOKCancel)
        If mBox = vbCancel Then Exit Sub
    End If
I = I + 1
Loop

    I = lDestOffset
    For Each M In MC
        C.Offset(I, 0).Value = M
        I = I + 1
    Next M
End If
Next C
Set RE = Nothing
End Sub
英文:

If you only want the FIRST line up to a maximum of 90 characters, and you have Microsoft 365, you can use this formula:

=TRIM(INDEX(LET(a,LEFT(A1,SEQUENCE(25,,91,-1)),b, FILTER(a,RIGHT(a)=&quot; &quot;),b),1))

从长段落中提取90个字符的句子文本(不打断单词)的Excel公式。

If you want to parse the entire text, here is a VBA macro that uses Regular Expressions to parse the data into appropriate row lengths.

As written, it writes the results below the selected cell(s) in separate cells, but you can change this for your requirements.

I have added a column showing the line lengths, but that is not part of the macro.

Option Explicit
Sub WordWrap()
&#39;requires reference to Microsoft VBScript Regular Expressions 5.5
&#39;Wraps at W characters, but will allow overflow if a word is longer than W
Dim RE As RegExp, MC As MatchCollection, M As Match
Dim str As String
Dim W As Long
Dim rSrc As Range, C As Range
Dim mBox As Long
Dim I As Long
&#39;with offset as 1, split data will be below original data
&#39;with offset = 0, split data will replace original data
Const lDestOffset As Long = 1

Set rSrc = Selection
    If rSrc.Rows.Count &lt;&gt; 1 Then
        MsgBox (&quot;You may only select&quot; &amp; vbLf &amp; &quot; Data in One (1) Row&quot;)
        Exit Sub
    End If
Set RE = New RegExp
    RE.Global = True
W = InputBox(&quot;Maximum characters in a Line: &quot;, , 72)
    If W &lt; 1 Then W = 79
For Each C In rSrc
str = C.Value
&#39;remove all line feeds and nbsp
    RE.Pattern = &quot;[\xA0\r\n\s]+&quot;
    str = RE.Replace(str, &quot; &quot;)
    RE.Pattern = &quot;\S.{0,&quot; &amp; W - 1 &amp; &quot;}(?=\s|$)|\S{&quot; &amp; W &amp; &quot;,}&quot;
    Debug.Print RE.Pattern
If RE.Test(str) = True Then
    Set MC = RE.Execute(str)
&#39;see if there is enough room
I = lDestOffset + 1
Do Until I &gt; MC.Count + lDestOffset
    If Len(C(I, 1)) &lt;&gt; 0 Then
        mBox = MsgBox(&quot;Data in &quot; &amp; C(I, 1).Address &amp; &quot; will be erased if you contine&quot;, vbOKCancel)
        If mBox = vbCancel Then Exit Sub
    End If
I = I + 1
Loop

    I = lDestOffset
    For Each M In MC
        C.Offset(I, 0).Value = M
        I = I + 1
    Next M
End If
Next C
Set RE = Nothing
End Sub

从长段落中提取90个字符的句子文本(不打断单词)的Excel公式。

答案2

得分: 2

如果只是要获取前90个字符,可以使用以下公式:

=TEXTBEFORE(LEFT(A1,91)," ",-1)

如果你想将文本分成每个90个字符的句子(以完整单词为单位),你可以尝试使用以下公式(适用于Office 365):

=LET(a,90,
DROP(REDUCE("",SEQUENCE(INT(LEN(A1)/a)+2),
     LAMBDA(x, y,
            LET(z,LEN(TEXTJOIN(" ",1,x)),
VSTACK(x,
       IF(LEN(z)=LEN(A1),
          "",
          TEXTBEFORE( LEFT(
                           SUBSTITUTE(A1&" ",                                
                                      IF(LEN(z)=1,
                                         "",
                                         TEXTJOIN(" ",1,x)&" "),
                                      ""),
                           a+1),
                      " ",
                      -1)))))),
     1))
英文:

If it's just the first 90 use

=TEXTBEFORE(LEFT(A1,91),&quot; &quot;,-1)

if you want the text divided into chops of 90 character sentences of whole words,you could try the following using Office 365:

=LET(a,90,
DROP(REDUCE(&quot;&quot;,SEQUENCE(INT(LEN(A1)/a)+2),
     LAMBDA(x, y,
            LET(z,LEN(TEXTJOIN(&quot; &quot;,1,x)),
VSTACK(x,
       IF(LEN(z)=LEN(A1),
          &quot;&quot;,
          TEXTBEFORE( LEFT(
                           SUBSTITUTE(A1&amp;&quot; &quot;,                                
                                      IF(LEN(z)=1,
                                         &quot;&quot;,
                                         TEXTJOIN(&quot; &quot;,1,x)&amp;&quot; &quot;),
                                      &quot;&quot;),
                           a+1),
                      &quot; &quot;,
                      -1)))))),
     1))

答案3

得分: 1

如果您正在使用Office 365,这个公式应该可以工作:

=LEFT(A1,MAX(LET(spacePos,IFERROR(UNIQUE(FIND(" ",A1,SEQUENCE(LEN(A1)))),-1),IF(spacePos<=90,spacePos,-1))))

英文:

If you are on Office 365, this formula should work:

=LEFT(A1,MAX(LET(spacePos,IFERROR(UNIQUE(FIND(&quot; &quot;,A1,SEQUENCE(LEN(A1)))),-1),IF(spacePos&lt;=90,spacePos,-1))))

答案4

得分: 0

这个解决方案使用额外的单元格来存储每个“句子”中第一个字符的位置,根据你的示例:

在B1单元格中,使用Excel复制到C1、D1等:

=IFERROR(MID($A$1,B2,C2-B2),RIGHT($A$1,LEN($A$1)-B2+1))

辅助单元格B2 = 1

在B3单元格中,使用Excel复制到C3、D3等:

=+FIND(" ",$A$1,90+B2)+1

结果在这里

英文:

This solution uses extra cells to store the position of the first character for each "sentence" according to your example:

In B1 and copy with excel to C1 D1 etc.:

=IFERROR(MID($A$1,B2,C2-B2),RIGHT($A$1,LEN($A$1)-B2+1))

Helping cell B2 = 1

Helping cell in B3 and copy with excel to C3 D3 etc.:

=+FIND(&quot; &quot;,$A$1,90+B2)+1

Result here

huangapple
  • 本文由 发表于 2023年8月9日 15:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定