Excel VBA-将单元格拆分为1000个部分,并将其复制到不同的单元格中

Excel VBA-将单元格拆分为1000个部分,并将其复制到不同的单元格中,vba,excel,Vba,Excel,我想知道是否有办法将一个包含6000个单词的单元格拆分为1000个单词。例如,单元格C1中有1000个单词,然后接下来的1000个单词在C2中,依此类推 这是我到目前为止的代码 该代码(单元格C1)的输出应该被拆分,C6有1000个字,C7有1000个字,依此类推,直到没有更多的字可用为止 提前谢谢你 Option Explicit Option Base 1 Dim dStr As String, aCell As Range Dim cet, i As Long Sub countWor

我想知道是否有办法将一个包含6000个单词的单元格拆分为1000个单词。例如,单元格C1中有1000个单词,然后接下来的1000个单词在C2中,依此类推

这是我到目前为止的代码

该代码(单元格C1)的输出应该被拆分,C6有1000个字,C7有1000个字,依此类推,直到没有更多的字可用为止

提前谢谢你

Option Explicit
Option Base 1

Dim dStr As String, aCell As Range
Dim cet, i As Long

Sub countWords()

Application.ScreenUpdating = False
Dim iniWords As Long, lWords As Long
Dim wK As Worksheet
On Error GoTo Err

Set wK = ActiveSheet

dStr = Join(WorksheetFunction.Transpose(wK.Range("A1:A" & wK.Range("A" & Rows.Count).End(xlUp).Row).Value), " ")
'iniWords = WorksheetFunction.CountA(wK.Range("A1:A" & wK.Rows.Count))
cet = Split(dStr, " ")
iniWords = UBound(cet)

wK.Range("A1:A" & wK.Range("A" & Rows.Count).End(xlUp).Row).RemoveDuplicates Columns:=1, Header:=xlNo

'lWords = WorksheetFunction.CountA(wK.Range("A1:A" & wK.Rows.Count))

dStr = Join(WorksheetFunction.Transpose(wK.Range("A1:A" & wK.Range("A" & Rows.Count).End(xlUp).Row).Value), " ")
cet = Split(dStr, " ")

dStr = ""
For i = LBound(cet) To UBound(cet)
    If Trim(cet(i)) <> "" And InStr(dStr, Trim(cet(i))) = 0 Then
        dStr = Trim(dStr) & " " & Trim(cet(i))
    End If
Next i
dStr = Trim(dStr)

cet = Split(dStr, " ")
lWords = UBound(cet)
wK.Range("C1") = dStr

Application.ScreenUpdating = True
MsgBox "Words: " & iniWords & vbNewLine & _
        "Removed duplicates " & iniWords - lWords & vbNewLine & _
        "Remaining Words " & lWords




Exit Sub

Err:
    MsgBox "There is no data in row A"


End Sub
选项显式
选项基数1
Dim dStr作为字符串,aCell作为范围
昏暗的cet,我想
副词
Application.ScreenUpdating=False
单词一样长,单词一样长
将工作设置为工作表
上错下错
Set wK=ActiveSheet
dStr=Join(工作表函数.Transpose(wK.Range(“A1:A”)和wK.Range(“A”和Rows.Count).End(xlUp.Row).Value),“”)
'iniWords=WorksheetFunction.CountA(wK.Range(“A1:A”&wK.Rows.Count))
cet=拆分(dStr,“”)
iniWords=UBound(cet)
wK.Range(“A1:A”和wK.Range(“A”和Rows.Count).End(xlUp.Row).RemoveDuplicates列:=1,标题:=xlNo
'lWords=WorksheetFunction.CountA(wK.Range(“A1:A”&wK.Rows.Count))
dStr=Join(工作表函数.Transpose(wK.Range(“A1:A”)和wK.Range(“A”和Rows.Count).End(xlUp.Row).Value),“”)
cet=拆分(dStr,“”)
dStr=“”
对于i=LBound(cet)至UBound(cet)
如果微调(cet(i))“”和仪表(dStr,微调(cet(i)))=0,则
dStr=修剪(dStr)和“修剪”(cet(i))
如果结束
接下来我
dStr=配平(dStr)
cet=拆分(dStr,“”)
LWORD=UBound(cet)
工作范围(“C1”)=dStr
Application.ScreenUpdating=True
MsgBox“文字:&iniWords&vbNewLine&_
“删除的重复项”&iniWords-LWORD&vbNewLine&_
“剩余单词”和lWords
出口接头
错误:
MsgBox“A行中没有数据”
端接头
您可以使用以下方法:

Option Explicit

Sub main()
    Const NWORDS As Long = 100 '<--| it's the number of words you want each cell to be written with. change it to your needs

    Dim strng As String
    Dim rowOffset As Long

    With Range("C1")
        strng = .Value
        rowOffset = 5 '<--| point to C7 at the first iteration
        Do
            strng = Replace(strng, " ", "|", , NWORDS) '<--| "mark" the first NWORDS with a different separator (be sure pipe ("|") is not a character you can have in your words)
            .Offset(rowOffset).Value = Replace(Left(strng, InStrRev(strng, "|") - 1), "|", " ") '<--| write first NWORDS words in current 'rowoffset' cell
            strng = Right(strng, Len(strng) - InStrRev(strng, "|"))
            rowOffset = rowOffset + 1 '<--| update row offset
        Loop While UBound(Split(strng, " ")) > NWORDS - 1
        .Offset(rowOffset).Value = strng
    End With
End Sub
选项显式
副标题()

Const NWORDS当Long=100'时,您的代码有什么问题?到目前为止,您的代码实现了什么?您希望它实现什么?i、 这其中的哪一个方面对你来说比较棘手?这与代码无关。现在,该工具获取A行并将所有单词收集到一个单元格(单元格C1)。但我不知道如何将单元格C1拆分为1000个单词的单元格。该代码(单元格C1)的输出应拆分为包含1000个单词的C6,包含1000个单词的C7,依此类推,直到没有更多的单词可用。@Chris您好,如果您想计算字符数,请使用
,或
,请参见此处。@Chris,您完成了吗?@Chris,有机会得到你的反馈吗?