Word 粘贴 Excel 选择之间的所有行,而不是单独选择

Word pastes all rows in between from Excel selection instead of selection alone

我想select Excel-电子表格中的一些行,然后使用Selection.PasteExcelTable将这些行作为Word-Table粘贴到Word文档中。 假设宏是由 Word 中的按钮触发的:

Private Sub CommandButton1_Click()

Dim xlApp As Object
Dim xlSheet As Object
Dim table As Object

Const strWorkbookName As String = "PATH"

Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.Workbooks.Open(FileName:=strWorkbookName).Sheets("Sheet1")

' returns a range of rows
select_rows_with_given_string("TEST", xlSheet).Copy
Selection.PasteExcelTable False, False, False

End Sub

在我的例子中 select_rows_with_given_string returns 第 1、2、5 和 6 行。 当我触发 Word 中的按钮时,将粘贴第 1、2、3、4、5 和 6 行,而不是第 1、2、5、6 行。当我在 excel 中使用 select_rows_with_given_string 然后将其粘贴到电子表格中时,一切正常。仅粘贴 1、2、5、6。我如何在 Word 中解决这个问题。

PS:如果我在 Excel 中手动 select 一些不相邻的行并将它们粘贴 (ctrl+v) 到 Word 中,同样的事情会发生。中间的所有行也被粘贴。

下面是select离子函数:

Function select_rows_with_given_string(searchString As String, xlSheet As Object) As Object
 
 Dim myUnion As Object
 Dim Row As Integer
 
 endCol = "N"
 endRow = 46
 startRow = 3
 
 xlSheet.Activate
 Set myUnion = Excel.Range("A1:" & endCol & startRow - 1)
 
 For Row = startRow To endRow
    If Excel.Range("A" & Row).Value = searchString Then
        If Not myUnion Is Nothing Then
            Set myUnion = Excel.Union(myUnion, Excel.Range("A" & Row & ":" & endCol & Row))
        Else
            Set myUnion = Excel.Range("A" & Row & ":" & endCol & Row)
        End If
    End If
 Next Row
 
 Set select_rows_with_given_string = myUnion
 
 End Function

我发现了一种非常 的方法。 基本上我复制行,创建一个新的 sheet,然后将这些行粘贴到那里,然后从新的 sheet 中再次复制它们,然后删除 sheet。一定有更好的方法吧?

Private Sub CommandButton1_Click()

Dim xlApp As Object
Dim xlSheet As Object
Dim table As Object

Const strWorkbookName As String = "PATH"

Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.Workbooks.Open(FileName:=strWorkbookName).Sheets("Sheet1")

' returns a range of rows
select_rows_with_given_string("TEST", xlSheet).Copy

' sketchy fix
Excel.Sheets.Add After:=Excel.ActiveSheet
Excel.ActiveSheet.Paste
Excel.Selection.Copy
Excel.ActiveSheet.Delete

Selection.PasteExcelTable False, False, False

请尝试下一个适配的功能。它隐藏了不需要的行和 return 要复制的可见单元格。以及使所有行可见的隐藏范围:

Function select_rows_with_given_string(searchString As String, xlSheet As Object, xlApp As Object) As Variant
 Dim rngH As Object, allRng As Object, endCol As String
 Dim Row As Integer, endRow As Long, startRow As Long
 
 endCol = "N": endRow = 46: startRow = 3
 Set allRng = xlSheet.Range("A1:" & endCol & endRow - 1) 'all the range to be returned
 
 For Row = startRow To endRow
    If xlSheet.Range("A" & Row).Value <> searchString Then '<>!!!
        If rngH Is Nothing Then
            Set rngH = xlSheet.Range("A" & Row)
        Else
            Set rngH = xlApp.Union(rngH, xlSheet.Range("A" & Row))
        End If
    End If
 Next Row
 rngH.EntireRow.Hidden = True 'hide all unnecessary rows...
 select_rows_with_given_string = Array(allRng.SpecialCells(12), rngH)
End Function

它需要一些错误处理对于保留 searchString 的任何行,当 SpecialCells(xlCellTypeVisible) 将 return 出错时。

您现有的代码应按以下方式进行调整:

Private Sub CommandButton1_Click_()
 Dim xlApp As Object, xlSheet As Object, table As Object, arr

 Const strWorkbookName As String = "PATH"

 Set xlApp = CreateObject("Excel.Application")
 Set xlSheet = xlApp.Workbooks.Open(FileName:=strWorkbookName).Sheets("Sheet1")

 ' returns a range of ranges:
 arr = select_rows_with_given_string("TEST", xlSheet, xlApp)
 arr(0).Copy
 Selection.PasteExcelTable False, False, False
 arr(1).EntireRow.Hidden = False 'unhide the previously hidden rows...
End Sub

请测试它并发送一些反馈。