如何创建循环,以便如果表中的单元格包含字符,它将自动创建另一行单元格 [英] How do I create a loop such that if the cell in a table contains characters, it will automatically create another row of cell

查看:115
本文介绍了如何创建循环,以便如果表中的单元格包含字符,它将自动创建另一行单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该表位于MS WORD中,但我试图使用VBA对其进行循环.目前,我已从来源中提取数据,并希望以MS Word形式输入表格.该表包含多行和2列.数据将输入到每一行的第二列中.现在,我有很多数据,无法继续指定每个行号,因此,我想找到一种方法,每当单元格已满时就添加一个额外的行.

The table is in MS WORD but I am trying to use VBA to loop it. Currently I have data extracted from a source and would like to input in a table in MS word. The table contains multiple rows and 2 columns. The data would be input inside the second column of each row. Right now, I have many data and it is impossible for me to keep on specifying each row number, hence I want to find a way to add an additional row whenever the cell is filled.

在Excel中,这组代码可以使用,但不能用文字显示..

In Excel, this set of codes would work but not in word..

 Do While (Len(Worksheets("Overall Performance").Cells(NewRecordRow, 12).Value) <> 0)
     NewRecordRow = NewRecordRow + 1
 Loop

目前这就是我所拥有的.

Currently this is what I have.

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"

推荐答案

根据我的测试,以下代码对我而言效果很好(关键点是 InStr([开始],字符串,子字符串,[比较])).

Based on my test, the following code works well on my side(The key point is InStr( [start], string, substring, [compare] )).

 Do While InStr(1, strCellText, "", vbBinaryCompare) <> 1
           'MsgBox "OK"
          Selection.InsertRowsBelow 1
    Loop

完整的代码(出于测试目的,请手动创建一个由N行和三列组成的表):

Worked full code(For test purpose, manually Create a table of N rows and three columns first):

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

您还可以使用以下代码,但这会遇到内存问题,因此不建议这样做:

You also can use the following code, but this will run into memory issue so it is not recommended:

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

这篇关于如何创建循环,以便如果表中的单元格包含字符,它将自动创建另一行单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆