我有一個420字符串,我需要在每個單元格中分成42個字符。 [所以在這種情況下,10個單元格] 但是,如果子字符串打破了一個單詞,該單詞應該溢出到下一個子字符串中。使用VBA拆分文本字符串到相等的子字符串中,以確保沒有字中斷
-1
A
回答
0
如何將字符串拆分爲單詞數組作爲分隔符和分割函數?
https://msdn.microsoft.com/de-de/library/6x627e5f(v=vs.90).aspx
然後通過陣列走,並決定根據各值的長度做什麼。
1
這是一個裸機VBA腳本,將執行此操作。
Sub breakUpSentence()
Dim strSentenceIn As String
Dim strSentenceOut As String
Dim arrWords As Variant
Dim strWord As Variant
Dim cellWrite As Range
'get the sentence
strSentenceIn = Sheet1.Range("A1").Value
'break it up into words storing the words in an array
arrWords = Split(strSentenceIn, " ")
'Set the first cell to write out to
Set cellWrite = Sheet1.Range("B1")
'Iterate through words
For Each strWord In arrWords
'Add the word to the output sentence, only include a space if the sentence is already populated
If strSentenceOut = "" Then strSentenceOut = strWord Else strSentenceOut = strSentenceOut & " " & strWord
'If the output sentence is now greater than or equal to 42 characters, then write it out
If Len(strSentenceOut) >= 42 Then
'Write it out
cellWrite.Value = strSentenceOut
'Increment to the next cell we want to write to
Set cellWrite = cellWrite.Offset(, 1)
'Clear the output sentence
strSentenceOut = ""
End If
Next strWord
End Sub
0
感謝您的幫助!
有些麻煩,但已經想通了。 [放到原始代碼]
這使用了一個UDF。
splittext(range, part number, maximum allowed number)
唯一的問題是需要使用多個公式來獲取各個部分。
Sub breakUpSentenceEqualParts()
Dim strSentenceIn As String
Dim strSentenceOut As String
Dim arrWords As Variant
Dim strWord As Variant
Dim cellWrite As Range
strSentenceIn = Sheet1.Range("A1").Value
arrWords = Split(strSentenceIn, " ")
Set cellWrite = Sheet1.Range("B1")
For Each strWord In arrWords
If strSentenceOut = "" Then strSentenceOut = strWord Else strSentenceOut = strSentenceOut & " " & strWord
If Len(strSentenceOut) >= 42 Then
cellWrite.Value = strSentenceOut
Set cellWrite = cellWrite.Offset(, 1)
strSentenceOut = ""
End If
Next strWord
End Sub
相關問題
- 1. 如何將字符串拆分爲等長的子字符串?
- 2. VBA +字符串拆分
- 3. 使用多字符字符串的拆分字符串
- 4. Elisp拆分字符串函數來拆分字符串。字符
- 5. 如何將字符串拆分爲java中的子字符串
- 6. 如何將字符串拆分爲iOS中的子字符串?
- 7. 將字符串拆分成haskell中的子字符串
- 8. 如何使用子字符串拆分字符串
- 9. 使用split.string(「」);將字符串拆分爲子字符串。
- 10. 在iOS中將字符串拆分爲子字符串
- 11. 使用字符串作爲分隔符來拆分字符串
- 12. 將字符串拆分爲字符串
- 13. 將字符串拆分爲字符串
- 14. 從字符串拆分字符串
- 15. 拆分(字符串)導航 - 「轉到下一個子字符串」
- 16. 字符串分割到相等長度的字符串
- 17. 確保字符串中沒有整數?
- 18. 字符串中使用沒有得到的字符串池
- 19. 拆分複雜字符串中的字符串部分
- 20. 的Perl:字符串中子字符串或子字符串中
- 21. 拆分字符串在字
- 22. 將文本文件中的字符串拆分爲字符串數組
- 23. 在Go中的字符串列表中拆分字符串
- 24. 如何將字符串拆分爲重複的子字符串
- 25. 確保字符串每n個字符都有一箇中斷字符
- 26. 以大寫字母拆分字符串
- 27. 使用awk拆分非字母數字字符的字符串
- 28. C#拆分字符串 - 將字符串拆分爲數組
- 29. 將字符串拆分後的序列中的字符串
- 30. 將字符串拆分爲矩陣vba
感謝您告訴我們您要做什麼。如果您在任何時候都有問題,那麼當您首次發佈您的嘗試代碼時,請隨時詢問。 – SJR
使用分割函數:split(「要分割的文本字符串」,「」)獲取單詞,然後檢查相鄰單詞的len並連接,如果Len(a)+ len(B)<= 10 – KacireeSoftware