复制工作表1中的范围,并仅插入工作表2中现有表中非空白的行

问题描述 投票:0回答:1

这是我的表1(源数据);

sheet 1 = source (ws1)

我只需要“复制”行,其中A7:C1009范围内的整行不是空白。然后应该从表格中的A7开始在我的表2(目的地)中输入该选择;

sheet 2 = destination (ws2)

我设法使这个VBA工作,但它将包括所有行(也意味着行,其中A:C都是空白/空;

Private Sub CommandButton1_Click()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim cl1 As Range, rng1 As Range, rng2 As Range, rng3 As Range
Dim r As Long, c As Long
    Set ws1 = Worksheets("Indkøb") 'Indkøb
    Set ws2 = Worksheets("Kalkulation3") 'Kalkulation3

    Set rng1 = ws1.Range("a7:a1009")
    Set rng2 = ws1.Range("b7:b1009")
    Set rng3 = ws1.Range("c7:c1009")

    For Each cl1 In rng1
        r = cl1.Row
        c = cl1.Column

        If cl1.Value <> ws2.Cells(r, c).Value Then
            ws2.Cells(r, c).EntireRow.Insert
            ws2.Cells(r, c).Value = cl1.Value
        End If
    Next cl1
    For Each cl1 In rng2
        r = cl1.Row
        c = cl1.Column

        If cl1.Value <> ws2.Cells(r, c).Value Then
            ws2.Cells(r, c).Value = cl1.Value
        End If
    Next cl1
    For Each cl1 In rng3
        r = cl1.Row
        c = cl1.Column

        If cl1.Value <> ws2.Cells(r, c).Value Then
            ws2.Cells(r, c).Value = cl1.Value
        End If
    Next cl1

End Sub

这就是我想要完成的事情;

  1. 只有A:C不为空的行应从工作表1复制并插入工作表2的表中。
  2. 如果在工作表1中添加了更多行,则再次运行宏应该在工作表2中的表中插入行并移动现有行 - 即。如果我在工作表1中插入第20行和第21行,则应复制第21行并将其作为工作表2中表格中的“新”第8行插入,将第8行下方的所有其他值/公式向下移动。

任何有关如何使这项工作的意见将不胜感激。谢谢 :-)

excel-vba vba excel
1个回答
0
投票

这将完成你想要的#1请求。您将需要一个不同的宏来完成您的#2请求。对不起,我帮不了你。

Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 7 To lRow
    If IsEmpty(Cells(i, 1)) And IsEmpty(Cells(i, 2)) And IsEmpty(Cells(i, 3)) Then
        Rows(i).EntireRow.Hidden = True
    End If
Next i

Dim Rng As Range
Set Rng = ActiveSheet.Cells.SpecialCells(xlCellTypeVisible)
    Rng.Copy Destination:=Sheets("Sheet2").Range("A7")
© www.soinside.com 2019 - 2024. All rights reserved.