使用SpecialCells进行多列同行复制/粘贴

时间:2013-01-15 16:30:30

标签: excel vba

有没有办法在使用.SpecialCells(xlCellTypeConstants,1)获取“B”列中的所有数字时,同时复制同一行中的单元格?

示例:

假设脚本找到了带有数字的单元格B2,B4,B5。我怎么还复制D2,D4和D5?我可以这样做并仍然使用特殊电池吗?最后,我想将这些值复制/粘贴到A&列中。 B在另一张纸上。

谢谢!

Dim strOutputFile As Variant
Dim wbkOut As Workbook
Dim tenln As Range
Dim tenlnPaste As Range
Dim wbkVer As Workbook

If strOutputFile(u) Like "*Lines.csv" Then
    With wbkOut.Worksheets(1)
        Set tenln = wbkOut.Worksheets(1).Cells(Rows.Count, 2).End(xlUp)
        Set tenlnPaste = wbkVer.Worksheets("TLines").Range("A" & .Rows.Count).End(xlUp).Offset(1).Resize(tenln.Rows.Count, 1)
        wbkOut.Worksheets(1).Range("B:B").SpecialCells(xlCellTypeConstants, 1).Copy
             With wbkVer.Worksheets("TenLines")
             tenlnPaste.PasteSpecial xlPasteValues
             End With
    End With
End If

1 个答案:

答案 0 :(得分:2)

是。这实际上非常简单。如下所示:

Dim rngConst as Range
On Error Resume Next
Set rngConst = wbkOut.Worksheets(1).Range("B:B").SpecialCells(xlCellTypeConstants, 1)
If Not rngConst is Nothing Then 

    rngConst.Copy 
   'do your pasting

    Set rngConts = rngConst.Offset(,2) 'for moving from B to D
    rngConst.Copy
    'do your pasting

End If
On Error Go To 0

您也可以这样做,将其全部放入1个复制区域:

Dim rngConst as Range

On Error Resume Next

If Not rngConst is Nothing

    Set rngConst = wbkOut.Worksheets(1).Range("B:B").SpecialCells(xlCellTypeConstants, 1)
    Set rngConst = Range(rngConst, rngConst.Offset(,2))
    rngConst.Copy
    'do your pasting

End If

On Error Go To 0

但是这会将数据复制到新工作表中的两个连续列中。例如,它不会从B复制到B,D复制到D.