我想使用VBscript将表格内容从word导出到excel,并对特定行进行一些过滤

时间:2017-11-17 05:10:59

标签: vba vbscript

我已编写此代码,但包含在过滤后不会不断粘贴。请帮忙! 例如  word包含第5列中的一些行值是" y"或" n"。我想只复制那些包含" y"在他们的第5栏。我的代码在这里。

 Function FnPrint()

  Dim objWord

  Dim objDoc

  Dim wdFileName As Variant
Dim TableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel

  Set objWord = CreateObject("Word.Application")

  Set objDoc = objWord.Documents.Open("D:\doc1.docx")

  objWord.Visible = False

  With objDoc
TableNo = objDoc.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 number of table to import", "Import Word Table", "1")
        End If
        With .tables(TableNo)
            'copy cell contents from Word table cells to Excel cells
            For iRow = 1 To .Rows.Count
                'determine if the text of the 5th column contains the word "y"
              If .cell(iRow, 5).Range.Text Like "*y*" Then
                    'find the last empty row in the current worksheet
                    'nextRow = ThisWorkbook.ActiveSheet.Range("a" & Rows.Count).End(xlUp).Row + 1
                    For iCol = 1 To .Columns.Count
'ThisWorkbook.ActiveSheet.Cells(nextRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
  Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                    Next iCol
                End If
            Next iRow
        End With
    End With
    Set wdDoc = Nothing
End Function

this is the word table

this is what my code pasting

我不希望它们之间有空行。请帮助。

0 个答案:

没有答案