只要您从 excel sheet 中读取数据,就会在循环中自动将行添加到 MS Word table
Automatically add rows into a MS Word table in a loop as long as you read data from the excel sheet
我是一个 VBA 新手,在执行此任务时遇到了很多错误和问题。任何帮助将不胜感激。
基本上我在 Word 文档中有一个文本框 (TextBox1)。我必须让用户输入的内容。它将是一个数字,该文本引用回一个文件夹,该文件夹包含 Excel 个以数字作为名称的文件。
该文件夹中的所有 Excel 文件具有相同的格式(从相同的模板创建),但每个文件中的行数各不相同。
我的老板要我在 Word 中创建一个 VBA 代码以将数据从 Excel 导入到 word 文档中,但问题出现在每个文件中的行数不同,因为用户可以select 来自文件夹中的任何现有文件。
有人建议只要有数据可以从 Excel 中提取,就可以在循环中创建行,但我就是无法使代码正常工作。在我将 table 单元格和 Excel 单元格这个词设置为彼此相等(我什至不知道我做对了)的地方,它尤其失败。
这是我目前的情况:
Private Sub CommandButton1_Click()
Dim tbl As Table
Dim row As row
Set tbl = ActiveDocument.Tables(3)
Set row = tbl.Rows.Add(BeforeRow:=tbl.Rows(1))
tbl.Rows(1).Range.FormattedText = tbl.Rows(2).Range.FormattedText
'~~~> This is required as the above code inserts a blank row in between
tbl.Rows(2).Delete
Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook
On Error Resume Next
Set exWb = objExcel.Workbooks.Open("S:\Electro-Protocol\Mot_Protocols\" & TextBox1 & ".xls")
row.Cells(0) = exwb.Sheets("Tabelle1“).Cells(4,1)
'Now you just need to define the loop in which you execute the previous mentioned code. Here's some pseudo code you got to adapt in VBA for yourself:
For counter = 0 To ExcelSheetLength Step 1
row.Cells(1) = exWb.Sheets("Tabelle1").Cells(counter, 0)
row.Cells(2) = exWb.Sheets("Tabelle1").Cells(counter, 1)
row.Cells(3) = exWb.Sheets("Tabelle1").Cells(counter, 2)
Next counter
For counter = 0 To ExcelSheetLength Step 1
If counter = 25 Or counter = 26 Or counter = 27 Then
'nothing
Else
Set row = tbl.Rows.Add(BeforeRow:=tbl.Rows(2))
End If
Next counter
End Sub
基本上,这是一团糟。我正在尝试从 selection of Excel worksheets 中导入数据,格式相同,但行数不同,将动态地导入 Word table只要我有数据要在 Excel sheet.
中读取,就在循环中添加行
我要的信息Excel从A4开始到D4,我要在Word doc Tables(3)中,从第1行第1列到第4列。 Excel 模板一遍又一遍地重复自己,直到完成所有数据。每 8 行后有 5 重复不必要的行。所以不确定如何循环,因为我计划在将数据导入 Word 时 ignoring/skipping 5 行重复的不必要信息。
我知道这听起来很混乱,所以如果您有任何问题,请告诉我。
提前致谢!
您可以在阅读时添加行 excel。在那个循环里面。
For counter = 0 To ExcelSheetLength Step 1
row.Cells(1) = exWb.Sheets("Tabelle1").Cells(counter, 0)
row.Cells(2) = exWb.Sheets("Tabelle1").Cells(counter, 1)
row.Cells(3) = exWb.Sheets("Tabelle1").Cells(counter, 2)
Set row = tbl.Rows.Add(BeforeRow:=tbl.Rows(2))
'set the table column values here with the row.cells 1-3
Next counter
我个人一直比较喜欢这种风格循环
Dim iCount as integer
Dim ws As Excel.Worksheet
Set ws = Application.ActiveSheet
lRow = 4
Do While lRow <= ws.UsedRange.Rows.Count
value1 = ws.Range("A" & lRow).Value
value2 = ws.Range("B" & lRow).Value
Set row = tbl.Rows.Add(BeforeRow:=tbl.Rows(2))
'set the table column values here with the row.cells 1-3
'Here you deal with the extras
iCount = iCount + 1
if icount = 8 then
iCount = 0
lrow = lrow + 5
else
lRow = lRow + 1
End if
ws.Range("A" & lRow).Activate
Loop
所以对于你的子问题:
There is 5 repeated unnecessary rows after every 8 rows. So not sure how to loop that around since I'm planning on ignoring/skipping the 5 repeating rows of unnecessary info while importing data into Word.
为此,有几种方法可以处理它。 1) 从你的 excel sheet 中提取所有内容,包括空白行,并在 MS WORD 中使用 VBA 后删除它们。我真的不像我想象的那样喜欢这种方法的想法。
2) 稍微慢一点的方法是编写一个 IF 语句来查看给定行上即将从 EXCEL 摄取的数据并确定是否该值为 BLANK/SPACE/EMPTY(与检查的部分相关的任何值),移至下一行并重新开始检查。如果检查显示有有效数据,请按要求摄取并添加到 MS WORD table。
我是一个 VBA 新手,在执行此任务时遇到了很多错误和问题。任何帮助将不胜感激。
基本上我在 Word 文档中有一个文本框 (TextBox1)。我必须让用户输入的内容。它将是一个数字,该文本引用回一个文件夹,该文件夹包含 Excel 个以数字作为名称的文件。
该文件夹中的所有 Excel 文件具有相同的格式(从相同的模板创建),但每个文件中的行数各不相同。
我的老板要我在 Word 中创建一个 VBA 代码以将数据从 Excel 导入到 word 文档中,但问题出现在每个文件中的行数不同,因为用户可以select 来自文件夹中的任何现有文件。
有人建议只要有数据可以从 Excel 中提取,就可以在循环中创建行,但我就是无法使代码正常工作。在我将 table 单元格和 Excel 单元格这个词设置为彼此相等(我什至不知道我做对了)的地方,它尤其失败。
这是我目前的情况:
Private Sub CommandButton1_Click()
Dim tbl As Table
Dim row As row
Set tbl = ActiveDocument.Tables(3)
Set row = tbl.Rows.Add(BeforeRow:=tbl.Rows(1))
tbl.Rows(1).Range.FormattedText = tbl.Rows(2).Range.FormattedText
'~~~> This is required as the above code inserts a blank row in between
tbl.Rows(2).Delete
Dim objExcel As Excel.Application
Dim exWb As Excel.Workbook
On Error Resume Next
Set exWb = objExcel.Workbooks.Open("S:\Electro-Protocol\Mot_Protocols\" & TextBox1 & ".xls")
row.Cells(0) = exwb.Sheets("Tabelle1“).Cells(4,1)
'Now you just need to define the loop in which you execute the previous mentioned code. Here's some pseudo code you got to adapt in VBA for yourself:
For counter = 0 To ExcelSheetLength Step 1
row.Cells(1) = exWb.Sheets("Tabelle1").Cells(counter, 0)
row.Cells(2) = exWb.Sheets("Tabelle1").Cells(counter, 1)
row.Cells(3) = exWb.Sheets("Tabelle1").Cells(counter, 2)
Next counter
For counter = 0 To ExcelSheetLength Step 1
If counter = 25 Or counter = 26 Or counter = 27 Then
'nothing
Else
Set row = tbl.Rows.Add(BeforeRow:=tbl.Rows(2))
End If
Next counter
End Sub
基本上,这是一团糟。我正在尝试从 selection of Excel worksheets 中导入数据,格式相同,但行数不同,将动态地导入 Word table只要我有数据要在 Excel sheet.
中读取,就在循环中添加行我要的信息Excel从A4开始到D4,我要在Word doc Tables(3)中,从第1行第1列到第4列。 Excel 模板一遍又一遍地重复自己,直到完成所有数据。每 8 行后有 5 重复不必要的行。所以不确定如何循环,因为我计划在将数据导入 Word 时 ignoring/skipping 5 行重复的不必要信息。
我知道这听起来很混乱,所以如果您有任何问题,请告诉我。 提前致谢!
您可以在阅读时添加行 excel。在那个循环里面。
For counter = 0 To ExcelSheetLength Step 1
row.Cells(1) = exWb.Sheets("Tabelle1").Cells(counter, 0)
row.Cells(2) = exWb.Sheets("Tabelle1").Cells(counter, 1)
row.Cells(3) = exWb.Sheets("Tabelle1").Cells(counter, 2)
Set row = tbl.Rows.Add(BeforeRow:=tbl.Rows(2))
'set the table column values here with the row.cells 1-3
Next counter
我个人一直比较喜欢这种风格循环
Dim iCount as integer
Dim ws As Excel.Worksheet
Set ws = Application.ActiveSheet
lRow = 4
Do While lRow <= ws.UsedRange.Rows.Count
value1 = ws.Range("A" & lRow).Value
value2 = ws.Range("B" & lRow).Value
Set row = tbl.Rows.Add(BeforeRow:=tbl.Rows(2))
'set the table column values here with the row.cells 1-3
'Here you deal with the extras
iCount = iCount + 1
if icount = 8 then
iCount = 0
lrow = lrow + 5
else
lRow = lRow + 1
End if
ws.Range("A" & lRow).Activate
Loop
所以对于你的子问题:
There is 5 repeated unnecessary rows after every 8 rows. So not sure how to loop that around since I'm planning on ignoring/skipping the 5 repeating rows of unnecessary info while importing data into Word.
为此,有几种方法可以处理它。 1) 从你的 excel sheet 中提取所有内容,包括空白行,并在 MS WORD 中使用 VBA 后删除它们。我真的不像我想象的那样喜欢这种方法的想法。
2) 稍微慢一点的方法是编写一个 IF 语句来查看给定行上即将从 EXCEL 摄取的数据并确定是否该值为 BLANK/SPACE/EMPTY(与检查的部分相关的任何值),移至下一行并重新开始检查。如果检查显示有有效数据,请按要求摄取并添加到 MS WORD table。