【发布时间】:2017-07-21 12:49:17
【问题描述】:
我有一个带有表格的 Word 文档 (*.docx)
**Name Description Dimension**
Level Text 1 Text 11
Text 2 Text 12
Text 3 Text 13
Text 4 Text 14
Text 5 Text 15
Text 6 Text 16
test Text 7 Text 17
有 3 列和 8 行。
我只想将“名称”列包含“测试”的“描述”列的内容提取到 Excel。
我做了下面的 Excel Marco
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer
Dim nextRow As Integer 'row index in Excel
On Error Resume Next
ActiveSheet.Range("A:AZ").ClearContents
With ActiveSheet.Range("A:AZ")
' Create Heading
HeadingRow = 1
.Cells(HeadingRow, 1).Formula = "Identifier"
wdFileName = Application.GetOpenFilename("Word files (*.docx),*.docx", , _
"Browse for file containing table to be imported")
If wdFileName = False Then Exit Sub '(user cancelled import file browser)
Set wdDoc = GetObject(wdFileName) 'open Word file
With wdDoc
TableNo = wdDoc.tables.Count
tableTot = wdDoc.tables.Count
If TableNo = 0 Then
MsgBox "The document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo >= 1 Then
TableNo = MsgBox("The document contains in TOTAL: " & TableNo & " tables." & vbCrLf)
End If
resultRow = 2
For tableStart = 1 To tableTot
With .tables(tableStart)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
'determine if the text of the 1th column contains the words "mike"
If (.cell(iRow, 1).Range.Text Like "*test*") _
Then
nextRow = ThisWorkbook.ActiveSheet.Range("a" & Rows.Count).End(xlUp).Row + 1
'find the last empty row in the current worksheet
nextRow = ThisWorkbook.ActiveSheet.Range("a" & Rows.Count).End(xlUp).Row + 1
MsgBox nextRow
'copy cell contents from Word table cells to Excel cells
For iCol = 1 To 2
ThisWorkbook.ActiveSheet.Cells(nextRow, 1) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Else
MsgBox "do not containt the word *test*"
End If
Next iRow
End With
Next tableStart
End With
End With
End Sub
但结果不是我所期望的。它是:
Identifier
Text 2
Text 3
Text 4
Text 5
Text 6
Text 7
我会期待
Identifier
Text 7
你能帮帮我吗?
看起来这是因为我在 Word 中的行被“合并”了。如果我拆分它们,我会收到我期望的结果,但问题是我有大约 300 个表,所以我无法将它们一一拆分...
谢谢。
【问题讨论】:
-
加“.”后可以试试吗(点)为行:If (.cell(iRow, 1).Range.Text Like ".test.") _
-
它不起作用...标识符文本 2 文本 3 文本 4 文本 5 文本 6
-
你能具体点,你得到了什么
-
标识符文本 2 文本 3 文本 4 文本 5 文本 6
-
使用 Instr 函数,这更容易 If Instr(.cell(iRow, 1).Range.Text, "test")
标签: excel merge ms-word copy vba