【问题标题】:How do I create a loop such that if the cell in a table contains characters, it will automatically create another row of cell如何创建一个循环,如果表格中的单元格包含字符,它将自动创建另一行单元格
【发布时间】:2018-08-17 17:03:48
【问题描述】:

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

在 Excel 中,这组代码可以工作,但不能在 word 中工作。

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

目前这就是我所拥有的。

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"

【问题讨论】:

  • 你能解释一下你的问题和代码吗,因为不清楚你想在这里做什么。
  • 目前我有从源中提取的数据,并希望在 MS Word 中输入表格。该表包含多行和 2 列。数据将在每行的第二列中输入。现在,我有很多数据,我不可能继续指定每个行号,因此我想找到一种方法在单元格被填充时添加一个额外的行
  • 您可以随时编辑您的问题,并在此处插入此说明,以便每个人都可以彻底阅读您的问题。
  • 在 Excel 中,这组代码可以工作,但不能在 word 中工作。. Do While (Len(Worksheets("Overall Performance").Cells(NewRecordRow, 12).Value) 0) NewRecordRow = NewRecordRow + 1 循环
  • 您的数据来自哪里?听起来您只想创建一个 Word 表,源数据中的每一行都有一行。这个对吗?如果是这样,很可能有更简单的方法来做你想做的事。

标签: vba ms-word


【解决方案1】:

根据我的测试,下面的代码对我来说效果很好(关键是InStr( [start], string, substring, [compare] ))。

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

工作的完整代码(出于测试目的,首先手动创建一个N行三列的表):

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

【讨论】:

  • 您输入的第一行有错误。 .Range 有一条错误消息,指出这是一个编译错误:无效或不合格的引用。
  • 完整代码中可以看到“With objTable”,需要将其添加到第一行。我已经编辑了代码。
  • 我使用了下面的代码,没有错误,但没有创建新行。 With objTable For Each c In .Range.Cells 'MsgBox .Cell(intNoOfRows, 2).Range.Text If Len(.Cell(intNoOfRows, 2).Range.Text) 2 Then intNoOfRows = intNoOfRows + 1 End If Next
  • 上面的代码只是更新了行数。要创建新行,我们只需要执行类似 Selection.InsertRowsBelow 1
  • “selection.InserRowsBelow 1”中有错误。它说“对象不支持此属性或方法”
猜你喜欢
  • 2023-01-21
  • 2013-07-28
  • 2014-05-09
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-01
相关资源
最近更新 更多