Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Convert Text to cells Sentences by Punctuation Marks

  1. #1
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location

    Convert Text to cells Sentences by Punctuation Marks

    Hello
    I want to divide the texts I have into sentences according to punctuation marks. Each sentence will be written in a separate cell. For example, these are punctuation marks (.) (…) (!) (?). But I can add and remove later. And "TextToColumns" not working good.

    text before

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eget felis eget elit euismod dignissim a a felis... Ut imperdiet justo condimentum risus consequat facilisis? In hac habitasse platea dictumst! Integer eget ex faucibus, varius libero in, rhoncus mauris. Sed a posuere purus... Maecenas tincidunt enim sodales, blandit nunc a, pellentesque lorem. Proin dapibus ultricies ultricies. In pellentesque lectus quis dolor ultrices ultrices.

    text after
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Suspendisse eget felis eget elit euismod dignissim a a felis...
    Ut imperdiet justo condimentum risus consequat facilisis?
    In hac habitasse platea dictumst!
    Integer eget ex faucibus, varius libero in, rhoncus mauris.
    Sed a posuere purus...
    Maecenas tincidunt enim sodales, blandit nunc a, pellentesque lorem.
    Proin dapibus ultricies ultricies.
    In pellentesque lectus quis dolor ultrices ultrices.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Select the cell(s) with the original text (I've assumed they're in one column) then run this macro. New 'sentences' will appear in the cells to the right, filling as many cells as needed, but keeping the original cell(s) intact.
    Sub blah()
    For Each cll In Selection.Cells
      bt = Application.Trim(cll.Value)
      at = Replace(Replace(Replace(Replace(bt, "...", "…¬"), ".", ".¬"), "?", "?¬"), "!", "!¬")
      at2 = Split(at, "¬")
      For i = LBound(at2) To UBound(at2)
        at2(i) = Application.Trim(at2(i))
        at2(i) = Replace(at2(i), "…", "...")    'optional to replace an ellipsis with 3 dots.
        If Len(at2(i)) = 0 Then at2(i) = "¬"
      Next i
      at3 = Filter(at2, "¬", False)
      cll.Offset(, 1).Resize(, UBound(at3) - LBound(at3) + 1).Value = at3
    Next cll
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
      sn = Filter(Split(Replace(Replace(Cells(1), "!", "!."), "?", "?."), "."), " ")
      Cells(1, 2).Resize(UBound(sn)) = Application.Transpose(sn)
    End Sub

  4. #4
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location
    Quote Originally Posted by snb View Post
    Sub M_snb()
      sn = Filter(Split(Replace(Replace(Cells(1), "!", "!."), "?", "?."), "."), " ")
      Cells(1, 2).Resize(UBound(sn)) = Application.Transpose(sn)
    End Sub
    Is it possible to get the data from the text file? Thank you for transpose idea. But this macro removing "Punctuation Marks". I want to keep them.


    Quote Originally Posted by p45cal View Post
    Select the cell(s) with the original text (I've assumed they're in one column) then run this macro. New 'sentences' will appear in the cells to the right, filling as many cells as needed, but keeping the original cell(s) intact.
    Sub blah()
    For Each cll In Selection.Cells
      bt = Application.Trim(cll.Value)
      at = Replace(Replace(Replace(Replace(bt, "...", "…¬"), ".", ".¬"), "?", "?¬"), "!", "!¬")
      at2 = Split(at, "¬")
      For i = LBound(at2) To UBound(at2)
        at2(i) = Application.Trim(at2(i))
        at2(i) = Replace(at2(i), "…", "...")    'optional to replace an ellipsis with 3 dots.
        If Len(at2(i)) = 0 Then at2(i) = "¬"
      Next i
      at3 = Filter(at2, "¬", False)
      cll.Offset(, 1).Resize(, UBound(at3) - LBound(at3) + 1).Value = at3
    Next cll
    End Sub
    Is it possible to get the data from the text file? And how can we add "transpose"?

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Supply the text file and describe how you want the sentences arranged (especially if there are multiple original strings to split).
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location
    The rules are still the same sentences will be split according to punctuation marks (.) (…) (!) (?)
    It works fine when it comes to splitting your macro sentences.
    In addition, reading the data from the text file
    and
    I need it to be written to column A in the excel file. (A1 to A1048576)
    Attached Files Attached Files

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    That's an Excel workbook, not a text file, and it contains the results.
    Looking for the text file you were talking about.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location
    I cant upload text file here and I upload to another site
    Txt file

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    You can zip every txt-file.

  10. #10
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Renamed: dummy sentences.txt.zip (454 Bytes)

    Zipped: dummy sentences.zip (444 Bytes)

    Contents:
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eget felis eget elit euismod dignissim a a felis... Ut imperdiet justo condimentum risus consequat facilisis? In hac habitasse platea dictumst! Integer eget ex faucibus, varius libero in, rhoncus mauris. Sed a posuere purus... Maecenas tincidunt enim sodales, blandit nunc a, pellentesque lorem. Proin dapibus ultricies ultricies. In pellentesque lectus quis dolor ultrices ultrices.
    Attached Files Attached Files
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  11. #11
    Quote Originally Posted by snb View Post
    Sub M_snb()
      sn = Filter(Split(Replace(Replace(Cells(1), "!", "!."), "?", "?."), "."), " ")
      Cells(1, 2).Resize(UBound(sn)) = Application.Transpose(sn)
    End Sub

    snb,

    The sentences from row 2 down, have a space before the sentence, and the sentences no longer have the punctuation marks at the end of the sentence.

  12. #12
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    From a file:
    Sub blah()
    Set Destn = ActiveSheet.Cells(1)
    Set ts = CreateObject("Scripting.FileSystemObject").opentextfile("C:\Users\Public\Documents\dummy sentences.txt")
    bt = Application.Trim(ts.readall)
    ts.Close
    at = Replace(Replace(Replace(Replace(bt, "...", "…¬"), ".", ".¬"), "?", "?¬"), "!", "!¬")
    at2 = Split(at, "¬")
    For i = 0 To UBound(at2)
      at2(i) = Application.Trim(at2(i))
      at2(i) = Replace(at2(i), "…", "...")    'optional to replace an ellipsis with 3 dots.
      If Len(at2(i)) = 0 Then at2(i) = "¬"
    Next i
    at3 = Filter(at2, "¬", False)
    Destn.Resize(UBound(at3) + 1).Value = Application.Transpose(at3)
    'Set Destn = Destn.Offset(UBound(at3))
    End Sub
    There's only one line in the text file you supplied so I don't know if it will work if there are more lines.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  13. #13
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    That's why you have do something yourself too: adapt the code. I am not so interested in a desrciption of the results of my macro, becasue I was familiar with those before I posted it. You'd better provide the necessary information we asked for in this thread.

  14. #14
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location
    Quote Originally Posted by p45cal View Post
    From a file:
    Sub blah()
    Set Destn = ActiveSheet.Cells(1)
    Set ts = CreateObject("Scripting.FileSystemObject").opentextfile("C:\Users\Public\Documents\dummy sentences.txt")
    bt = Application.Trim(ts.readall)
    ts.Close
    at = Replace(Replace(Replace(Replace(bt, "...", "…¬"), ".", ".¬"), "?", "?¬"), "!", "!¬")
    at2 = Split(at, "¬")
    For i = 0 To UBound(at2)
      at2(i) = Application.Trim(at2(i))
      at2(i) = Replace(at2(i), "…", "...")    'optional to replace an ellipsis with 3 dots.
      If Len(at2(i)) = 0 Then at2(i) = "¬"
    Next i
    at3 = Filter(at2, "¬", False)
    Destn.Resize(UBound(at3) + 1).Value = Application.Transpose(at3)
    'Set Destn = Destn.Offset(UBound(at3))
    End Sub
    There's only one line in the text file you supplied so I don't know if it will work if there are more lines.
    Hello this code working well with small datas. But its giving error with big datas. i share another text file for example for big datas. İ hope you can help me. Thank you.
    Attached Files Attached Files

  15. #15
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try:
    Sub blah()
    Dim at3() As String
    Set Destn = ActiveSheet.Cells(1)
    Set ts = CreateObject("Scripting.FileSystemObject").opentextfile("C:\Users\Public\Documents\dummy sentences.txt")
    ct = ts.readall
    ts.Close
    dt = Split(ct, vbCrLf)
    j = 0
    For Each bt In dt
      at = Replace(Replace(Replace(Replace(bt, "...", "…¬"), ".", ".¬"), "?", "?¬"), "!", "!¬")
      at2 = Split(at, "¬")
      For i = 0 To UBound(at2)
        at2(i) = Application.Trim(at2(i))
        at2(i) = Replace(at2(i), "…", "...")    'optional to replace an ellipsis with 3 dots.
        If Len(at2(i)) = 0 Then at2(i) = "¬"
        ReDim Preserve at3(0 To j)
        at3(j) = at2(i)
        j = j + 1
      Next i
    Next bt
    at3 = Filter(at3, "¬", False)
    Destn.Resize(UBound(at3) + 1).Value = Application.Transpose(at3)
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  16. #16
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location
    Quote Originally Posted by p45cal View Post
    try:
    Sub blah()
    Dim at3() As String
    Set Destn = ActiveSheet.Cells(1)
    Set ts = CreateObject("Scripting.FileSystemObject").opentextfile("C:\Users\Public\Documents\dummy sentences.txt")
    ct = ts.readall
    ts.Close
    dt = Split(ct, vbCrLf)
    j = 0
    For Each bt In dt
      at = Replace(Replace(Replace(Replace(bt, "...", "…¬"), ".", ".¬"), "?", "?¬"), "!", "!¬")
      at2 = Split(at, "¬")
      For i = 0 To UBound(at2)
        at2(i) = Application.Trim(at2(i))
        at2(i) = Replace(at2(i), "…", "...")    'optional to replace an ellipsis with 3 dots.
        If Len(at2(i)) = 0 Then at2(i) = "¬"
        ReDim Preserve at3(0 To j)
        at3(j) = at2(i)
        j = j + 1
      Next i
    Next bt
    at3 = Filter(at3, "¬", False)
    Destn.Resize(UBound(at3) + 1).Value = Application.Transpose(at3)
    End Sub
    This macro working thank you + rep

    and last thing there is some character errors like this. And thay need replace. Can we handle? example

    Text
    —“I couldn’t, and didn’t, put it down until I’d read every last word.”
    Result
    —I couldn’t, and didn’t, put it down until I’d read every last word.

    — = -
    “ = "
    ” = "
    ’ = '

  17. #17
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Put a small text file together with examples including all the extra characters included in several lines and also post a workbook of how you expect this small text file to appear after processing.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  18. #18
    Hi,

    Text
    —“I couldn’t, and didn’t, put it down until I’d read every last word.”
    Result
    —I couldn’t, and didn’t, put it down until I’d read every last word.

    — = -
    “ = "
    ” = "
    ’ = '
    I think those characters appear due to the font used, and / or certain special characters of a regional font.

    I tested both VBA codes and both work perfectly.
    With both TEXT files.

  19. #19
    VBAX Regular
    Joined
    Apr 2016
    Posts
    35
    Location
    I upload sample files about characters.
    Thank you
    Attached Files Attached Files

  20. #20
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    The text file seems to be encoded as 65001 (unicode (UTF-8)).
    For the moment, in the attached, is an alternative solution, using Power Query. As it is I've left it looking at the most recent file you attached. Before it looks at your text file on your system, you need to point the query at that file.
    This picture attempts to show you what you need to do:
    2020-08-28_133518.jpg
    I've also attached the picture as a zip file in case the resolution's not good enough.
    After that has been done, you only need to right-click the table and choose Refresh which will update the table according to what's in the text file on your system.

    I will still try and tweak the macro solution to read this 65001-encoded text file - but I'm out of time just now.

    I'm interested, where does this text file come from?
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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