将列从选定的行复制到另一个工作表vba

时间:2014-09-01 17:17:48

标签: excel vba excel-vba

用户应该选择一些行(不一定在彼此之下),然后每行中的一些列将被复制到另一张表中,我该如何实现? 我已经尝试过application.inputbox,但它似乎不适用于范围。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。不是超级高效,但如果我理解你正在寻找的东西,它应该完成工作。这将生成一行数组,其中所有行都突出显示了行中的第一个单元格。希望这会有所帮助。

Sub macro1()
Dim a As Range
Dim i As Integer
Dim numRowsToCheck As Integer
Dim rows() As Integer
Dim length As Integer

Set a = Selection
numRowsToCheck = 100
length = 0
For i = 1 To numRowsToCheck
    If Not Application.Intersect(Worksheets(1).Cells(i, 1), a) Is Nothing Then
        length = length + 1
        ReDim Preserve rows(length)
        rows(length - 1) = i
    End If
Next i

MsgBox (rows(0) & "," & rows(1) & "," & rows(2)) 'Check results

For i = 0 To length - 1
    'Copy stuff from row i to wherever you want it to go
Next i
End Sub