vba - 将 Word 表格中特定单元格的数据导入 Excel 工作簿中的特定单元格,每次导入时从第一个空白行开始

标签 vba excel

我发现我稍微修改了一个 VBA 脚本,这其中的一部分。我需要将单词表中的一些信息导入excel。我遇到的问题是脚本覆盖了第一行,每次使用它时我都需要它转到第一个空白行。

这是我所拥有的:

Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iTable As Integer  'table number index
Dim iRow As Long     'row index in Excel
Dim iCol As Integer  'column index in Excel

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
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")
End If

'Range("A1") = "Table #"
'Range("B1") = "Cell (3,2)"
'Range("C1") = "Cell (4,2)"

For iTable = 1 To TableNo
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
'copy cell contents from Word table cells to Excel cells in column B and C
Cells(iTable + 1, "A") = WorksheetFunction.Clean(.cell(14, 2).Range.Text)
Cells(iTable + 1, "B") = WorksheetFunction.Clean(.cell(2, 2).Range.Text) 'need to post current date
Cells(iTable + 1, "C") = WorksheetFunction.Clean(.cell(16, 2).Range.Text)
Cells(iTable + 1, "D") = WorksheetFunction.Clean(.cell(15, 2).Range.Text)
Cells(iTable + 1, "E") = WorksheetFunction.Clean(.cell(1, 2).Range.Text)
Cells(iTable + 1, "H") = WorksheetFunction.Clean(.cell(7, 2).Range.Text)
Cells(iTable + 1, "I") = WorksheetFunction.Clean(.cell(8, 2).Range.Text)
Cells(iTable + 1, "S") = WorksheetFunction.Clean(.cell(3, 2).Range.Text) 'need to post name of negotiatoe
End With
Next iTable
End With

斯科特的回答对此是正确的。数据或我的文件中的某些东西奇怪导致异常行为,所以下面是对我有用的东西。
Sub ImportWordTable()
Dim wdDoc As Object
Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iTable As Integer  'table number index
Dim iRow As Long     'row index in Excel
Dim iCol As Integer  'column index in Excel

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
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
ElseIf TableNo > 1 Then
TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
"Enter table number of table to import", "Import Word Table", "1")
End If

'Range("A1") = "Table #"
'Range("B1") = "Cell (3,2)"
'Range("C1") = "Cell (4,2)"

For iTable = 1 To TableNo

       Dim lRow As Long
       lRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row + 1

        With .tables(TableNo)

            'copy cell contents from Word table cells to Excel cells
            'copy cell contents from Word table cells to Excel cells in column B and C
            Cells(lRow - 1, "A") = WorksheetFunction.Clean(.cell(14, 2).Range.Text)
            Cells(lRow - 1, "B") = WorksheetFunction.Clean(.cell(2, 2).Range.Text) 'need to post current date
            Cells(lRow - 1, "C") = WorksheetFunction.Clean(.cell(16, 2).Range.Text)
            Cells(lRow - 1, "D") = WorksheetFunction.Clean(.cell(15, 2).Range.Text)
            Cells(lRow - 1, "E") = WorksheetFunction.Clean(.cell(1, 2).Range.Text)
            Cells(lRow - 1, "H") = WorksheetFunction.Clean(.cell(7, 2).Range.Text)
            Cells(lRow - 1, "I") = WorksheetFunction.Clean(.cell(8, 2).Range.Text)
            Cells(lRow - 1, "S") = WorksheetFunction.Clean(.cell(3, 2).Range.Text) 'need to post name of negotiatoe

        End With

Next iTable
End With

Set wdDoc = Nothing

End Sub

最佳答案

您可以在工作表中找到下一个可用行,并在每次对您的 For Loop 进行以下修改后写入该行。

For iTable = 1 To TableNo

       Dim lRow As Long
       lRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row +1

        With .tables(TableNo)

            'copy cell contents from Word table cells to Excel cells
            'copy cell contents from Word table cells to Excel cells in column B and C
            Cells(lRow, "A") = WorksheetFunction.Clean(.cell(14, 2).Range.Text)
            Cells(lRow, "B") = WorksheetFunction.Clean(.cell(2, 2).Range.Text) 'need to post current date
            Cells(lRow, "C") = WorksheetFunction.Clean(.cell(16, 2).Range.Text)
            Cells(lRow, "D") = WorksheetFunction.Clean(.cell(15, 2).Range.Text)
            Cells(lRow, "E") = WorksheetFunction.Clean(.cell(1, 2).Range.Text)
            Cells(lRow, "H") = WorksheetFunction.Clean(.cell(7, 2).Range.Text)
            Cells(lRow, "I") = WorksheetFunction.Clean(.cell(8, 2).Range.Text)
            Cells(lRow, "S") = WorksheetFunction.Clean(.cell(3, 2).Range.Text) 'need to post name of negotiatoe

        End With

Next iTable

关于vba - 将 Word 表格中特定单元格的数据导入 Excel 工作簿中的特定单元格,每次导入时从第一个空白行开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34636220/

相关文章:

vba - 运行时错误91 : Object variable or With block not set

vba - 运行 Excel 宏时,截图工具无法启动截图?

excel - 使用 vba 从多个子文件夹复制文件

Excel OFFSET 公式从水平到垂直获取数据

python - 在Matplotlib中模拟Excel的 "scatter with smooth curve"样条函数3分

excel - 如何设置图表 Axis 上的标签对齐方式?

excel - 复制工作表时 range.value 出现 VBA 错误 1004

java - 即使在执行 fileoutputstream.close() 之后,Apache-POI 也不保存 Excel 工作簿

string - 将外部单元格地址转换为vba中的范围

vba - 在 VBa 中重绘标签控件