一次复制2列以前的单元格?

时间:2017-03-27 23:02:35

标签: excel vba excel-vba

enter image description here

可以同时复制单元格的值,2列(两列中的值不同=字符串)。现在我分开做。

1:

 Do Until ActiveCell.Row >= LastRow
        If Trim(ActiveCell.Offset(1, 0)) = "" Then
        ActiveCell.Offset(1, 0).Value = ActiveCell
        End If
        ActiveCell.Offset(1, 0).Select
    Loop

然后再次为第3列

    Cells(FirstRow + 2, 2).Select
  Do Until ActiveCell.Row >= LastRow
            If Trim(ActiveCell.Offset(1, 0)) = "" Then
            ActiveCell.Offset(1, 0).Value = ActiveCell
            End If
            ActiveCell.Offset(1, 0).Select
        Loop

实际上我在下面对此进行了编码,它同时进行,但是它很慢,而且很慢

Sub Kopi()
Dim i, y As Integer
For i = 1 To 100
    For y = 1 To 100
If Trim(Cells(i + 1, 1)) = "" And Trim(Cells(y + 1, 2)) = "" Then
    Cells(i + 1, 1).Value = Cells(i, 1)
    Cells(y + 1, 2).Value = Cells(y, 2)
    End If

Next y
Next i
End Sub

1 个答案:

答案 0 :(得分:1)

这是你需要的吗?它遍历给定范围内的空单元格(A1:B10)并将任何空白设置为等于上面的值

Sub Test()
    Dim rng As Range, r As Range
    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:B10").SpecialCells(xlCellTypeBlanks)

    For Each r In rng
        If Not r.Row = 1 Then r.Value = r.Offset(-1, 0).Value
    Next r
End Sub