如何以编程方式选择多个不同的单元格以复制到剪贴板,然后手动粘贴到另一个工作表

时间:2012-11-20 23:45:15

标签: excel-vba vba excel

作为一个简单的例子,我必须选择一列中的每个第22个单元格,直到我到达一个空单元格。我需要能够将所有这些单元格复制到剪贴板以粘贴到另一个电子表格中。我可以正确地选择每个单元格,但是在收集完所有单元格后,不知道如何将它们收集到要复制的对象中。

Stared评论需要代码。

Sub SelectAllValidCells()

  ' select first cell

  [J15].Select

  ' Test contents of active cell; if active cell is empty, exit loop.

  Do Until IsEmpty(ActiveCell)

     ' ***** need to figure out how to gather the valid cells 
     ' here to later copy to clipboard when we reach empty cell 

     ' Step down 22 rows to the next cell.
     ActiveCell.Offset(22, 0).Select

    ' Return to top of loop.
  Loop
  '***** copy gathered cells to clipboard

End Sub

1 个答案:

答案 0 :(得分:1)

Sub Tester()

Dim rng As Range, c As Range

    Set c = Range("J15")

    Do
        If rng Is Nothing Then
            Set rng = c
        Else
            Set rng = Application.Union(rng, c)
        End If

        Set c = c.Offset(22, 0)
    Loop While Len(c.Value) > 0


    If Not rng Is Nothing Then
        rng.Copy Range("K1")
    End If

End Sub