Vba 如何创建循环,以便在表中的单元格包含字符时,自动创建另一行单元格

Vba 如何创建循环,以便在表中的单元格包含字符时,自动创建另一行单元格,vba,ms-word,Vba,Ms Word,该表是在微软WORD,但我试图使用VBA循环它。目前,我有从源提取的数据,并希望在MS word的表格中输入。该表包含多行和两列。数据将输入到每行的第二列中。现在,我有很多数据,不可能一直指定每个行号,因此我想找到一种方法,在单元格填充时添加一行 在Excel中,这组代码可以工作,但在word中无法工作 Do While (Len(Worksheets("Overall Performance").Cells(NewRecordRow, 12).Value) <> 0)

该表是在微软WORD,但我试图使用VBA循环它。目前,我有从源提取的数据,并希望在MS word的表格中输入。该表包含多行和两列。数据将输入到每行的第二列中。现在,我有很多数据,不可能一直指定每个行号,因此我想找到一种方法,在单元格填充时添加一行


在Excel中,这组代码可以工作,但在word中无法工作

 Do While (Len(Worksheets("Overall Performance").Cells(NewRecordRow, 12).Value) <> 0)
     NewRecordRow = NewRecordRow + 1
 Loop
Do-While(Len(工作表(“总体性能”).Cells(NewRecordRow,12.Value)0)
NewRecordRow=NewRecordRow+1
环
目前这就是我所拥有的

Dim intNoOfRows
Dim intNoOfColumns
Dim objWord
Dim objDoc
Dim objRange
Dim objTable

intNoOfRows = 1 (Initially 6 because I Preset it)
intNoOfColumns = 2

Set objWord = CreateObject ("Word.Application")
Set objDoc = objWord.Document.Add

objDoc.Tables.Add objRange, intNoOfRows, intNoOfColumns

Set objTable = objDoc.Tables(1)
objTable.Borders.Enable = True

For Each cell(intNoOfRows,2) in objDoc.Table(1).range.cells
    If Len(cell.range.text) <1 Then
        intNoOfRows = intNoOfRows + 1
    End if
Next

ObjTable.Cell(intNoOfRows,2).Range.Text = "ABC"
Dim intNoOfRows
昏暗的夜空
模糊词
Dim objDoc
模糊对象范围
暗淡的对象
intNoOfRows=1(最初为6,因为我预先设置了它)
intNoOfColumns=2
设置objWord=CreateObject(“Word.Application”)
设置objDoc=objWord.Document.Add
objDoc.Tables.Add objRange、intNoOfRows、intNoOfColumns
Set objTable=objDoc.Tables(1)
objTable.Borders.Enable=True
对于objDoc.Table(1.range.cells)中的每个单元格(intNoOfRows,2)

根据我的测试,如果Len(cell.range.text),那么下面的代码在我这边运行得很好(关键点是InStr([start]、string、substring、[compare])

Do While InStr(1,strCellText,“,vbBinaryCompare)1
'MsgBox“确定”
Selection.InsertRowsBelow 1
环
工作完整代码(出于测试目的,首先手动创建一个包含N行和三列的表):

子测试()
昏昏欲睡
昏暗的夜空
模糊词
Dim objDoc
模糊对象范围
模糊对象,如Word.Table
Dim strCellText作为字符串
intNoOfRows=1'(需要是talbe的实际行数)
intNoOfColumns=3
设置objDoc=ActiveDocument
Set objTable=objDoc.Tables(1)
objTable.Borders.Enable=True
可反对的
strCellText=.Cell(intNoOfRow,2).Range.Text
intNoOfRows=.Rows.Count
在安装时执行(1,strCellText,“,vbBinaryCompare)1
'MsgBox“确定”
intNoOfRows=intNoOfRows+1
环
.Rows.Last.Select
Selection.InsertRowsBelow 1
.Cell(intNoOfRows,3).Range.Text=“ABC”
MsgBox intNoOfRows
以
端接头
您也可以使用以下代码,但这会导致内存问题,因此不建议使用:

With objTable
Do While Len(.Cell(intNoOfRows, 2).Range.Text) <> 0
     intNoOfRows = intNoOfRows + 1
     MsgBox intNoOfRows
Loop
End With
带有objTable
Do While Len(.Cell(intNoOfRows,2).Range.Text)0
intNoOfRows=intNoOfRows+1
MsgBox intNoOfRows
环
以

您能解释一下您的问题和代码吗,因为不清楚您想在这里做什么。目前,我从源代码中提取了数据,并希望在MS word中的表格中输入数据。该表包含多行和两列。数据将输入到每行的第二列中。现在,我有很多数据,我不可能一直指定每个行号,因此我想找到一种方法,在填充单元格时添加一行。您可以随时编辑您的问题,并在其中插入此解释,以便每个人都能彻底阅读您的问题。在Excel中,这组代码可以工作,但不能在word中使用。。Do While(Len(工作表(“总体性能”).Cells(NewRecordRow,12).Value)0)NewRecordRow=NewRecordRow+1循环您的数据来自哪里?听起来您只是想创建一个Word表,源数据中的每一行对应一行。这是正确的吗?如果是这样,很可能有一种更简单的方法来做你想做的事情。你输入的第一行有一个错误。在.Range中有一条错误消息,指出它是编译错误:引用无效或不合格。您可以在完整代码中看到“With objTable”,需要将其添加到第一行。我已经编辑了代码。我使用了下面的代码,没有错误,但是没有创建新行。对于.Range.Cells的MsgBox.Cell(intNoOfRows,2).Range.Text中的每个c,如果Len(.Cell(intNoOfRows,2).Range.Text)2,则intNoOfRows=intNoOfRows+1 End,如果下一个代码只更新行数。要创建新行,只需执行Selection.InsertRowsBelow 1“Selection.InsertRowsBelow 1”中的错误。它表示“对象不支持此属性或方法”
Sub Test()
Dim intNoOfRows
Dim intNoOfColumns
Dim objWord
Dim objDoc
Dim objRange
Dim objTable As Word.Table
Dim strCellText As String

intNoOfRows = 1 '(Need to be the actully row count of your talbe)
intNoOfColumns = 3

Set objDoc = ActiveDocument
Set objTable = objDoc.Tables(1)
objTable.Borders.Enable = True

With objTable  

 strCellText = .Cell(intNoOfRow, 2).Range.Text
 intNoOfRows = .Rows.Count

    Do While InStr(1, strCellText, "", vbBinaryCompare) <> 1
           'MsgBox "OK"
           intNoOfRows = intNoOfRows + 1
    Loop      

.Rows.Last.Select
Selection.InsertRowsBelow 1

.Cell(intNoOfRows, 3).Range.Text = "ABC"
MsgBox intNoOfRows
End With
End Sub
With objTable
Do While Len(.Cell(intNoOfRows, 2).Range.Text) <> 0
     intNoOfRows = intNoOfRows + 1
     MsgBox intNoOfRows
Loop
End With