Excel-进入母版表的多个条目

时间:2018-12-02 23:17:44

标签: excel vba excel-vba copy

当前,我将数据输入3个单元格中,然后按下一个按钮,然后将数据传输到母版中。在我的代码中,当前一击等于一项。我希望有一个选项,尽管每次单击可以有多个条目,具体取决于字段中的值。我已经添加了该字段,但是我不确定如何编码。有什么建议吗?

这是条目页面的外观 This is what the entry page looks like

这是它写入的母版 This is the mastersheet it writes to

Private Sub CommandButton21_Click()

Dim s1, s2
Set s1 = Worksheets("Master")
Set s2 = Worksheets("Sheet1")

With s1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
    .Cells(, "a").Value = s2.Range("p20").Value
    .Cells(, "b").Value = s2.Range("p21").Value
    .Cells(, "c").Value = s2.Range("c4").Value
    .Cells(, "d").Value = s2.Range("c7").Value
    .Cells(, "e").Value = s2.Range("c10").Value
    s2.Range("c4").FormulaR1C1 = ""
    s2.Range("c7").FormulaR1C1 = ""
    s2.Range("c10").FormulaR1C1 = ""

End With
End Sub

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

不太优雅,但易于实现:

Private Sub CommandButton21_Click()

Dim s1 As Worksheet, s2 As Worksheet, i As Integer, LpCount As Integer
Set s1 = Worksheets("Master")
Set s2 = Worksheets("Sheet1")
If Not s2.Range("I7").Value > 1 Then LpCount = 1 Else LpCount = s2.Range("I7").Value

For i = 1 To LpCount
    With s1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
        .Cells(, "a").Value = s2.Range("p20").Value
        .Cells(, "b").Value = s2.Range("p21").Value
        .Cells(, "c").Value = s2.Range("c4").Value
        .Cells(, "d").Value = s2.Range("c7").Value
        .Cells(, "e").Value = s2.Range("c10").Value
    End With
Next i
s2.Range("c4").FormulaR1C1 = ""
s2.Range("c7").FormulaR1C1 = ""
s2.Range("c10").FormulaR1C1 = ""
End Sub

答案 1 :(得分:0)

Private Sub CommandButton21_Click()

Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Worksheets("Master")
Set s2 = Worksheets("Sheet1")

For i = 1 To s2.Range("I7").Value
With s1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
    .Cells(, "a").Value = s2.Range("p20").Value
    .Cells(, "b").Value = s2.Range("p21").Value
    .Cells(, "c").Value = s2.Range("c4").Value
    .Cells(, "d").Value = s2.Range("c7").Value
    .Cells(, "e").Value = s2.Range("c10").Value

End With
Next i
s2.Range("c4").FormulaR1C1 = ""
s2.Range("c7").FormulaR1C1 = ""
s2.Range("c10").FormulaR1C1 = ""
s2.Range("i7").FormulaR1C1 = "1"


End Sub