从一张纸上复制几列,然后在另一张纸上插入

时间:2017-05-11 08:30:20

标签: excel vba excel-vba

我是Excel新手,正在学习创建公式。 最近,我得到一个情况,我需要创建一个宏,在点击“更新”按钮后,它复制工作表中特定行的几列,并将这些复制的列插入到最后一行中的另一个工作表。

实施例: 以下是一页(GUI)中的数据:

GUI Sheet - Sheet1

根据这些数据,我想阅读所有黄色突出显示的数据,并希望在最后将其插入另一张表(DataTogether): DataTogether - Sheet2

以上读取值需要插入第二张中黄色突出显示的字段。休息所有遗体。

方案:
从第一张表中显示的数据中,当我按下更新按钮时,它将读取所有突出显示但将数据插入到第二列,其中J列中的值通过。
即,第11行和第13行中的值将被读取并插入表2中。

插入操作后,工作表2中的数据将添加以下两个突出显示的新行(第15行和第16行): FinalOuput Image

如果需要任何其他细节,请告诉我。

我希望我能尽快学到一些基础知识,不会再发布这些类型的查询。

非常感谢提前。

我尝试工作并创建了一个代码,在该代码中,我在硬编码的行中插入数据,但是我需要在下一个可用的空白行中插入数据。 我无法找到实现目标的方法。

请帮忙。

我创建了以下代码:

Sub Button1_Click()

If Not IsEmpty(Worksheets("GUI").Range("J7").Value) Then
    Worksheets("DataTogether").Range("G17").Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N17").Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E17").Value = Worksheets("GUI").Range("C7").Value
    Worksheets("DataTogether").Range("H17").Value = Worksheets("GUI").Range("D7").Value
    Worksheets("DataTogether").Range("O17").Value = Worksheets("GUI").Range("J7").Value
    Worksheets("DataTogether").Range("K17").Value = Worksheets("GUI").Range("L7").Value
End If

If Not IsEmpty(Worksheets("GUI").Range("J8").Value) Then
    Worksheets("DataTogether").Range("G17").Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N17").Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E17").Value = Worksheets("GUI").Range("C8").Value
    Worksheets("DataTogether").Range("H17").Value = Worksheets("GUI").Range("D8").Value
    Worksheets("DataTogether").Range("O17").Value = Worksheets("GUI").Range("J8").Value
    Worksheets("DataTogether").Range("K17").Value = Worksheets("GUI").Range("L8").Value
End If

If Not IsEmpty(Worksheets("GUI").Range("J9").Value) Then
    Worksheets("DataTogether").Range("G17").Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N17").Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E17").Value = Worksheets("GUI").Range("C9").Value
    Worksheets("DataTogether").Range("H17").Value = Worksheets("GUI").Range("D9").Value
    Worksheets("DataTogether").Range("O17").Value = Worksheets("GUI").Range("J9").Value
    Worksheets("DataTogether").Range("K17").Value = Worksheets("GUI").Range("L9").Value
End If

If Not IsEmpty(Worksheets("GUI").Range("J10").Value) Then
    Worksheets("DataTogether").Range("G17").Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N17").Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E17").Value = Worksheets("GUI").Range("C10").Value
    Worksheets("DataTogether").Range("H17").Value = Worksheets("GUI").Range("D10").Value
    Worksheets("DataTogether").Range("O17").Value = Worksheets("GUI").Range("J10").Value
    Worksheets("DataTogether").Range("K17").Value = Worksheets("GUI").Range("L10").Value
End If

' First One Created
If Not IsEmpty(Worksheets("GUI").Range("J11").Value) Then
    Worksheets("DataTogether").Range("G17").Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N17").Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E17").Value = Worksheets("GUI").Range("C11").Value
    Worksheets("DataTogether").Range("H17").Value = Worksheets("GUI").Range("D11").Value
    Worksheets("DataTogether").Range("O17").Value = Worksheets("GUI").Range("J11").Value
    Worksheets("DataTogether").Range("K17").Value = Worksheets("GUI").Range("L11").Value
End If

If Not IsEmpty(Worksheets("GUI").Range("J12").Value) Then
    Worksheets("DataTogether").Range("G17").Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N17").Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C17").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E17").Value = Worksheets("GUI").Range("C12").Value
    Worksheets("DataTogether").Range("H17").Value = Worksheets("GUI").Range("D12").Value
    Worksheets("DataTogether").Range("O17").Value = Worksheets("GUI").Range("J12").Value
    Worksheets("DataTogether").Range("K17").Value = Worksheets("GUI").Range("L12").Value
End If

If Not IsEmpty(Worksheets("GUI").Range("J13").Value) Then
    Worksheets("DataTogether").Range("G18").Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N18").Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B18").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C18").Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E18").Value = Worksheets("GUI").Range("C13").Value
    Worksheets("DataTogether").Range("H18").Value = Worksheets("GUI").Range("D13").Value
    Worksheets("DataTogether").Range("O18").Value = Worksheets("GUI").Range("J13").Value
    Worksheets("DataTogether").Range("K18").Value = Worksheets("GUI").Range("L13").Value
End If


End Sub

1 个答案:

答案 0 :(得分:0)

这可以帮助其他人,所以在这里发布答案:

Sub Button1_Click()

LastRow = Worksheets("DataTogether").Range("E" & Rows.Count).End(xlUp).Row + 1
If Not IsEmpty(Worksheets("GUI").Range("J7").Value) Then
    Worksheets("DataTogether").Range("G" & LastRow).Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N" & LastRow).Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E" & LastRow).Value = Worksheets("GUI").Range("C7").Value
    Worksheets("DataTogether").Range("H" & LastRow).Value = Worksheets("GUI").Range("D7").Value
    Worksheets("DataTogether").Range("O" & LastRow).Value = Worksheets("GUI").Range("J7").Value
    Worksheets("DataTogether").Range("K" & LastRow).Value = Worksheets("GUI").Range("L7").Value
End If

LastRow = Worksheets("DataTogether").Range("E" & Rows.Count).End(xlUp).Row + 1
If Not IsEmpty(Worksheets("GUI").Range("J8").Value) Then
    Worksheets("DataTogether").Range("G" & LastRow).Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N" & LastRow).Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E" & LastRow).Value = Worksheets("GUI").Range("C8").Value
    Worksheets("DataTogether").Range("H" & LastRow).Value = Worksheets("GUI").Range("D8").Value
    Worksheets("DataTogether").Range("O" & LastRow).Value = Worksheets("GUI").Range("J8").Value
    Worksheets("DataTogether").Range("K" & LastRow).Value = Worksheets("GUI").Range("L8").Value
End If

LastRow = Worksheets("DataTogether").Range("E" & Rows.Count).End(xlUp).Row + 1
If Not IsEmpty(Worksheets("GUI").Range("J9").Value) Then
    Worksheets("DataTogether").Range("G" & LastRow).Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N" & LastRow).Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E" & LastRow).Value = Worksheets("GUI").Range("C9").Value
    Worksheets("DataTogether").Range("H" & LastRow).Value = Worksheets("GUI").Range("D9").Value
    Worksheets("DataTogether").Range("O" & LastRow).Value = Worksheets("GUI").Range("J9").Value
    Worksheets("DataTogether").Range("K" & LastRow).Value = Worksheets("GUI").Range("L9").Value
End If

LastRow = Worksheets("DataTogether").Range("E" & Rows.Count).End(xlUp).Row + 1
If Not IsEmpty(Worksheets("GUI").Range("J10").Value) Then
    Worksheets("DataTogether").Range("G" & LastRow).Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N" & LastRow).Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E" & LastRow).Value = Worksheets("GUI").Range("C10").Value
    Worksheets("DataTogether").Range("H" & LastRow).Value = Worksheets("GUI").Range("D10").Value
    Worksheets("DataTogether").Range("O" & LastRow).Value = Worksheets("GUI").Range("J10").Value
    Worksheets("DataTogether").Range("K" & LastRow).Value = Worksheets("GUI").Range("L10").Value
End If

LastRow = Worksheets("DataTogether").Range("E" & Rows.Count).End(xlUp).Row + 1
' First One Created
If Not IsEmpty(Worksheets("GUI").Range("J11").Value) Then
    Worksheets("DataTogether").Range("G" & LastRow).Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N" & LastRow).Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E" & LastRow).Value = Worksheets("GUI").Range("C11").Value
    Worksheets("DataTogether").Range("H" & LastRow).Value = Worksheets("GUI").Range("D11").Value
    Worksheets("DataTogether").Range("O" & LastRow).Value = Worksheets("GUI").Range("J11").Value
    'Worksheets("DataTogether").Range("K17").Value = Worksheets("GUI").Range("L11").Value
    Worksheets("DataTogether").Range("K" & LastRow).Value = Worksheets("GUI").Range("L11").Value
End If

LastRow = Worksheets("DataTogether").Range("E" & Rows.Count).End(xlUp).Row + 1
If Not IsEmpty(Worksheets("GUI").Range("J12").Value) Then
    Worksheets("DataTogether").Range("G" & LastRow).Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N" & LastRow).Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E" & LastRow).Value = Worksheets("GUI").Range("C12").Value
    Worksheets("DataTogether").Range("H" & LastRow).Value = Worksheets("GUI").Range("D12").Value
    Worksheets("DataTogether").Range("O" & LastRow).Value = Worksheets("GUI").Range("J12").Value
    Worksheets("DataTogether").Range("K" & LastRow).Value = Worksheets("GUI").Range("L12").Value
End If

LastRow = Worksheets("DataTogether").Range("E" & Rows.Count).End(xlUp).Row + 1
If Not IsEmpty(Worksheets("GUI").Range("J13").Value) Then
    Worksheets("DataTogether").Range("G" & LastRow).Value = Worksheets("GUI").Range("D4").Value 'Same for All
    Worksheets("DataTogether").Range("N" & LastRow).Value = Worksheets("GUI").Range("L2").Value 'Same for All
    Worksheets("DataTogether").Range("B" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("C" & LastRow).Value = Worksheets("GUI").Range("B7").Value 'Same for All
    Worksheets("DataTogether").Range("E" & LastRow).Value = Worksheets("GUI").Range("C13").Value
    Worksheets("DataTogether").Range("H" & LastRow).Value = Worksheets("GUI").Range("D13").Value
    Worksheets("DataTogether").Range("O" & LastRow).Value = Worksheets("GUI").Range("J13").Value
    'Worksheets("DataTogether").Range("K18").Value = Worksheets("GUI").Range("L13").Value
    Worksheets("DataTogether").Range("K" & LastRow).Value = Worksheets("GUI").Range("L13").Value
End If


End Sub