【问题标题】:how to extract a table , where table name contains the given sub string如何提取表,其中表名包含给定的子字符串
【发布时间】:2014-02-24 22:37:34
【问题描述】:

我有一个包含许多表格的 word 文档。 我使用下面的宏将表格导出到 excel 文件。 Macro to export MS Word tables to Excel sheets

这真的很有帮助。我非常感谢。

但我面临的问题是我的工作还没有完全完成。

因此我刚刚开始学习 VBA , 我不知道如何完成以下任务。

  • 我有一个 Word 文档,其中 Appx 包含 20 个表格。 我只需要提取一个表,其中表名包含一个固定的子字符串。

例如:

如果word文档有3个表名 “表 1:性别和工资表” “表 2:工资信息表” "表3:姓名、年龄、性别、工资表"

如果我运行上述宏,我会成功地将所有表格添加到我的 Excel 文档中。 但我的需要是,只获取带有名称的表格。

"表格包含姓名、年龄、性别和薪水"

请给我建议。

真的提前谢谢了。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您在输入框中输入的桌号是否正确?

    这一行将导入限制在特定的表中:

    TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
    "Enter table number of table to import", "Import Word Table", "1")
    

    【讨论】:

    • 感谢您的回复。
    • 脚本能够提取给定word文档中的所有表格。数字 1 用于从 word 文档中的第一个表开始默认提取。
    • i 要求“用给定的子字符串过滤表名”
    • For tableStart = 1 To tableTot found_t = 0 found_t = WorksheetFunction.FindB("记录字段数据类型", .tables(tableStart), 1) If found_t Then
    • 我可以通过添加上述逻辑来做到这一点。
    【解决方案2】:

    我已根据您的要求更改了一些行,请检查并告诉我

    Sub ImportWordTable()
    Dim wdDoc As Object
    Dim wdFileName As Variant
    Dim TableNo As string 
    Dim iRow As Long 'row index in Excel
    Dim iCol As Integer 'column index in Excel
    
    wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
    "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 name of table to import", "Import Word Table", "table with name , age , gender and salary")
    End If
    With .tables(TableNo)
    'copy cell contents from Word table cells to Excel cells
    For iRow = 1 To .Rows.Count
    For iCol = 1 To .Columns.Count
    Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
    Next iCol
    Next iRow
    End With
    End With
    Set wdDoc = Nothing
    End Sub
    

    【讨论】:

    • 你试过上面的代码吗? ? ..它会问你表名而不是表号..
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    相关资源
    最近更新 更多