[英]Transfer Data from large Excel file to multiple tables in word document
I am trying to transfer data from a large excel sheet into small tables in a word document.我正在尝试将大型 excel 工作表中的数据传输到 Word 文档中的小表格中。 I want to do this by matching the first column in a row and then copying the cells in the in the columns to the right in the row.
我想通过匹配一行中的第一列然后将列中的单元格复制到行的右侧来做到这一点。
For example, here is the table in the excel file:例如,下面是 excel 文件中的表:
Company![]() |
First Name![]() |
Last Name![]() |
---|---|---|
Apple![]() |
Tom![]() |
Tommy![]() |
Google![]() |
Ann![]() |
Anny![]() |
Amazon![]() |
Driver![]() |
Ted![]() |
In the word documents there are multiple tables.在word文档中有多个表。 I want to populate the names that match with the company.
我想填充与公司匹配的名称。 Right now the tables look like this:
现在表格看起来像这样:
Company![]() |
First Name![]() |
Last Name![]() |
---|---|---|
Google![]() |
||
Amazon![]() |
Company![]() |
First Name![]() |
Last Name![]() |
---|---|---|
Google![]() |
||
Apple![]() |
Company![]() |
First Name![]() |
Last Name![]() |
---|---|---|
Google![]() |
||
Amazon![]() |
||
Apple![]() |
I want the names to be added so they would look like this:我希望添加名称,以便它们看起来像这样:
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![]() |
I am open to any option that would help me do this.我愿意接受任何可以帮助我做到这一点的选择。 I was trying Mail Merge but it seems to be to labor intensive and not save time.
我正在尝试邮件合并,但它似乎是劳动密集型的,而不是节省时间。 Thanks for the help.
谢谢您的帮助。
For the first approach you can use this code in MS Word, where you want to fill tables.对于第一种方法,您可以在要填写表格的 MS Word 中使用此代码。 The code is not optimized and error check is omitted.
代码未优化,省略了错误检查。 It expects the named range 'MyData' exists and contains exactly three columns [company, name1, name2]
它期望命名范围“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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.