将备用单元格行剪切/复制/粘贴到下一列,然后删除空行

问题描述 投票:2回答:2

我在突出显示/复制一列中的备用行并将其粘贴到下一列并对齐时遇到了挑战。

这是一个截图:

enter image description here

excel vba excel-2007
2个回答
1
投票

以下代码假设您有两个单独的选项卡,SRC和DST以及第一个单元格中的数据范围。将一步到位:

Public Sub CopyAlternate()
Dim i As Long

i = 2

While Len(Sheets("SRC").Cells(i, 1).Value) > 0
    Sheets("DST").Cells(i / 2 + 1, 1).Value = Sheets("SRC").Cells(i, 1).Value
    Sheets("DST").Cells(i / 2 + 1, 2).Value = Sheets("SRC").Cells(i + 1, 1).Value
    i = i + 2
Wend
End Sub

0
投票

您可以使用此代码并根据需要进行调整:

Sub alternate()
    Dim i As Integer
    Dim j As Integer
    Dim n As Integer

    i = 0
    j = 0
    n = 0

    With ActiveSheet        
        For Each c In .Range("A4:A16")
            .Cells(20 + j, 1 + i).Value = c.Value
            If n = 0 Or n Mod 2 = 0 Then
                i = 1
                j = j
            Else
                i = 0
                j = j + 1
            End If
            n = n + 1
        Next c
    End With    
End Sub

当用字母重建你的例子时,这对我有用(为了更快的检查)。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.