将数据从大型 Excel 文件传输到 word 文档中的多个表格
Transfer Data from large Excel file to multiple tables in word document
我正在尝试将数据从大型 excel sheet 传输到 word 文档中的小型 table。我想通过匹配一行中的第一列,然后将列中的单元格复制到行中的右侧来完成此操作。
例如,这里是 excel 文件中的 table:
Company
First Name
Last Name
Apple
Tom
Tommy
Google
Ann
Anny
Amazon
Driver
Ted
word文档中有多个table。我想填充与公司匹配的名称。现在 table 看起来像这样:
Company
First Name
Last Name
Google
Amazon
Company
First Name
Last Name
Google
Apple
Company
First Name
Last Name
Google
Amazon
Apple
我希望添加名称,使它们看起来像这样:
Company
First Name
Last Name
Google
Ann
Anny
Amazon
Driver
Ted
Company
First Name
Last Name
Google
Ann
Anny
Apple
Tom
Tommy
Company
First Name
Last Name
Google
Ann
Anny
Amazon
Driver
Ted
Apple
Tom
Tommy
我愿意接受任何可以帮助我做到这一点的选择。我正在尝试邮件合并,但它似乎是劳动密集型的并且不节省时间。感谢您的帮助。
对于第一种方法,您可以在要填充表格的 MS Word 中使用此代码。代码未优化,省略了错误检查。
它期望命名范围 'MyData' 存在并且恰好包含三列 [company, name1, name2]
'Add reference to the 'Microsoft Excel 16.0 Object Library' is required
Sub FillTablesFromExcel()
Dim doc As Word.Document
Set doc = ThisDocument
Dim data
data = ReadDataFromExcel
Dim t As Word.Table
For Each t In doc.Tables
If t.Columns.Count <> 3 Then Exit For
Dim r As Word.Row
For Each r In t.Rows
Dim txt As String
txt = r.Cells(1).Range.Text
company = Trim(Left(txt, Len(txt) - 2))
For i = 1 To UBound(data)
comp = data(i, 1)
name1 = data(i, 2)
name2 = data(i, 3)
If company = comp Then
r.Cells(2).Range.Text = name1
r.Cells(3).Range.Text = name2
End If
Next
Next
Next
End Sub
Function ReadDataFromExcel()
Dim xlsWorkbook As Excel.Workbook
Set xlsWorkbook = Excel.Workbooks.Open("C:\Path\To\The\File.xlsx", ReadOnly:=True)
Dim xlsSheet As Excel.Worksheet
Set xlsSheet = xlsWorkbook.Sheets(1)
Dim dataRange As Excel.Range
Set dataRange = xlsSheet.Range("MyData")
Value2 = dataRange.Value2
Call xlsWorkbook.Close(False)
ReadDataFromExcel = Value2
End Function
我正在尝试将数据从大型 excel sheet 传输到 word 文档中的小型 table。我想通过匹配一行中的第一列,然后将列中的单元格复制到行中的右侧来完成此操作。
例如,这里是 excel 文件中的 table:
Company | First Name | Last Name |
---|---|---|
Apple | Tom | Tommy |
Ann | Anny | |
Amazon | Driver | Ted |
word文档中有多个table。我想填充与公司匹配的名称。现在 table 看起来像这样:
Company | First Name | Last Name |
---|---|---|
Amazon |
Company | First Name | Last Name |
---|---|---|
Apple |
Company | First Name | Last Name |
---|---|---|
Amazon | ||
Apple |
我希望添加名称,使它们看起来像这样:
Company | First Name | Last Name |
---|---|---|
Ann | Anny | |
Amazon | Driver | Ted |
Company | First Name | Last Name |
---|---|---|
Ann | Anny | |
Apple | Tom | Tommy |
Company | First Name | Last Name |
---|---|---|
Ann | Anny | |
Amazon | Driver | Ted |
Apple | Tom | Tommy |
我愿意接受任何可以帮助我做到这一点的选择。我正在尝试邮件合并,但它似乎是劳动密集型的并且不节省时间。感谢您的帮助。
对于第一种方法,您可以在要填充表格的 MS Word 中使用此代码。代码未优化,省略了错误检查。 它期望命名范围 'MyData' 存在并且恰好包含三列 [company, name1, name2]
'Add reference to the 'Microsoft Excel 16.0 Object Library' is required
Sub FillTablesFromExcel()
Dim doc As Word.Document
Set doc = ThisDocument
Dim data
data = ReadDataFromExcel
Dim t As Word.Table
For Each t In doc.Tables
If t.Columns.Count <> 3 Then Exit For
Dim r As Word.Row
For Each r In t.Rows
Dim txt As String
txt = r.Cells(1).Range.Text
company = Trim(Left(txt, Len(txt) - 2))
For i = 1 To UBound(data)
comp = data(i, 1)
name1 = data(i, 2)
name2 = data(i, 3)
If company = comp Then
r.Cells(2).Range.Text = name1
r.Cells(3).Range.Text = name2
End If
Next
Next
Next
End Sub
Function ReadDataFromExcel()
Dim xlsWorkbook As Excel.Workbook
Set xlsWorkbook = Excel.Workbooks.Open("C:\Path\To\The\File.xlsx", ReadOnly:=True)
Dim xlsSheet As Excel.Worksheet
Set xlsSheet = xlsWorkbook.Sheets(1)
Dim dataRange As Excel.Range
Set dataRange = xlsSheet.Range("MyData")
Value2 = dataRange.Value2
Call xlsWorkbook.Close(False)
ReadDataFromExcel = Value2
End Function