0

有没有办法在使用 .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
4

1 回答 1

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。

于 2013-01-15T16:49:32.740 回答