创建用于从 Word 复制到 Excel 的表格

发布于 2024-07-20 14:24:40 字数 1401 浏览 6 评论 0原文

因此,我需要获取一些在 MS Word 表格中完成的数据,并在 Excel 中进行操作。 我决定通过 VBA 子例程将其从 Word 转换为 Excel 以“节省时间”。 我的源Word文档包含大约二十多张表格。

我获取了源文档的表格,提取了数据并制作了一个新文档,其中包含一个新表格,只需将其复制并粘贴到 Excel 中即可。

然而,虽然副本之前的最终表格在文字上看起来不错。 当我将其复制到 Excel 时,它将包含整个段落的单元格分解为单独的单元格。

大多数 Excel 爱好者都知道,尽管解决方案看起来像 Excel 中的那样,但执行合并和居中操作 - 只会保留所选内容中最上面单元格中的内容!

因此,任何关于更好的合并和中心,或者更好的“节省时间”的建议都会很棒。

这是迄今为止的代码示例:

Sub First()

Dim tableLength, tableIndex
tableLength = ThisDocument.Tables.Count

Dim tblReport As Table
Dim docReport As Document

Set docReport = Documents.Add
Set tblReport = docReport.Tables.Add(Selection.Range, 1, 2)

With tblReport

Dim fieldOne, subvalueAription, subvalueA, subvalueB, subvalueC

For tableIndex = 1 To tableLength

    fieldOne = ThisDocument.Tables(tableIndex).Rows(2).Cells(2).Range.Text
    subvalueA = Trim(ThisDocument.Tables(tableIndex).Rows(4).Cells(2).Range.Text)
    subvalueB = "A: " & Trim(ThisDocument.Tables(tableIndex).Rows(5).Cells(2).Range.Text)
    subvalueC = "B: " & Trim(ThisDocument.Tables(tableIndex).Rows(6).Cells(2).Range.Text)
    subvalueAription = subvalueA & subvalueB & subvalueC & "C: "

    Dim rowNext As row
    Set rowNext = .Rows.Add
    rowNext.Cells(1).Range.Text = fieldOne
    rowNext.Cells(2).Range.Text = subvalueA & subvalueB & subvalueC


Next

End With


End Sub

So, I needed to take some data done in MS Word tables, and manipulate in excel.
I decided to get it from word to excel via a VBA subroutine to "save time".
My source word document contained like twentysomething tables.

I took my source document's tables, extracted my data and made a new document, with a new table, only needing me to copy and paste it into excel.

However, while the final table before copy looks good in word. When i copy it to excel, it breaks up the cells that contain whole paragraphs into separate cells.

As most excel peeps would know, even though a solution looks like in excel, doing a merge and center - that only preserves the content in the uppermost cell in the selection!

So, any advice, on either a better merge and center, or a better "time saver" alltogether, would be great.

Here's a sample of the code so far:

Sub First()

Dim tableLength, tableIndex
tableLength = ThisDocument.Tables.Count

Dim tblReport As Table
Dim docReport As Document

Set docReport = Documents.Add
Set tblReport = docReport.Tables.Add(Selection.Range, 1, 2)

With tblReport

Dim fieldOne, subvalueAription, subvalueA, subvalueB, subvalueC

For tableIndex = 1 To tableLength

    fieldOne = ThisDocument.Tables(tableIndex).Rows(2).Cells(2).Range.Text
    subvalueA = Trim(ThisDocument.Tables(tableIndex).Rows(4).Cells(2).Range.Text)
    subvalueB = "A: " & Trim(ThisDocument.Tables(tableIndex).Rows(5).Cells(2).Range.Text)
    subvalueC = "B: " & Trim(ThisDocument.Tables(tableIndex).Rows(6).Cells(2).Range.Text)
    subvalueAription = subvalueA & subvalueB & subvalueC & "C: "

    Dim rowNext As row
    Set rowNext = .Rows.Add
    rowNext.Cells(1).Range.Text = fieldOne
    rowNext.Cells(2).Range.Text = subvalueA & subvalueB & subvalueC


Next

End With


End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2024-07-27 14:24:41

Excel 使用与 Word 不同的行终止符。 为了避免 Word 表格中的文本被拆分到多个 Excel 单元格中的问题,您需要自行处理行终止符转换。

'Word > Excel
 newText = Replace(wordText, vbCrLf, vbLf)

我凭记忆发布此内容,但这就是问题的根源。

Excel uses a different line terminator than Word. In order to avoid the problem with the text from the Word table being split over several Excel cells, you need to handle the line terminator conversion yourself.

'Word > Excel
 newText = Replace(wordText, vbCrLf, vbLf)

I'm posting this by memory, but that's the root of that problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文