使用Userform更新多行

时间:2017-11-10 12:40:24

标签: excel

我正在开展一个项目。我使用以下代码通过Userform在Excel的Sales表中输入客户订单;

lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).Value = Me.Data1.Value
        .Cells(lRow, 2).Value = Me.Data2.Value
        .Cells(lRow, 3).Value = Me.Data3.Value
        .Cells(lRow, 4).Value = Me.Data4.Value
        .Cells(lRow, 5).Value = Me.Data5.Value
        .Cells(lRow, 6).Value = Me.Data6.Value
        .Cells(lRow, 7).Value = Me.Data7.Value
        .Cells(lRow, 8).Value = Me.Data8.Value
        .Cells(lRow, 9).Value = Me.Data9.Value
        .Cells(lRow, 10).Value = Me.Data10.Value
    End With

上面的代码一次只能更新一行记录。我只能输入一个连续输入的每个客户一个产品。我希望能够为一个客户输入各种产品并发布到不同的行。 我该怎么办?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您只想在工作表上多次复制相同的数据集。如果是这种情况,那么您可以像这样复制数据集:

ws.Range(ws.Cells(lRow, 1), ws.Cells(lRow, 10)).Copy ws.Range(ws.Cells(lRow + x_copies, 1), ws.Cells(lRow, 10))

在您的代码中:

lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
    .Cells(lRow, 1).Value = Me.Data1.Value
    .Cells(lRow, 2).Value = Me.Data2.Value
    .Cells(lRow, 3).Value = Me.Data3.Value
    .Cells(lRow, 4).Value = Me.Data4.Value
    .Cells(lRow, 5).Value = Me.Data5.Value
    .Cells(lRow, 6).Value = Me.Data6.Value
    .Cells(lRow, 7).Value = Me.Data7.Value
    .Cells(lRow, 8).Value = Me.Data8.Value
    .Cells(lRow, 9).Value = Me.Data9.Value
    .Cells(lRow, 10).Value = Me.Data10.Value
    ws.Range(ws.Cells(lRow, 1), ws.Cells(lRow, 10)).Copy ws.Range(ws.Cells(lRow + x_copies, 1), ws.Cells(lRow, 10))
End With

或者您可以选择在工作表上多次直接写入值,如下所示:

lRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
With ws
    .Cells(lRow, 1).Resize(x_copies).Value = Me.Data2.Value
    .Cells(lRow, 2).Resize(x_copies).Value = Me.Data2.Value
    .Cells(lRow, 3).Resize(x_copies).Value = Me.Data3.Value
    .Cells(lRow, 4).Resize(x_copies).Value = Me.Data4.Value
    .Cells(lRow, 5).Resize(x_copies).Value = Me.Data5.Value
    .Cells(lRow, 6).Resize(x_copies).Value = Me.Data6.Value
    .Cells(lRow, 7).Resize(x_copies).Value = Me.Data7.Value
    .Cells(lRow, 8).Resize(x_copies).Value = Me.Data8.Value
    .Cells(lRow, 9).Resize(x_copies).Value = Me.Data9.Value
    .Cells(lRow, 10).Resize(x_copies).Value = Me.Data10.Value
End With