VBA用户自定义命令button可在编辑信息之前将行复制到工作表

我有一个辅助用户的forms,可以编辑单元格数据的基础上从列表框中双击select。

用户表单有一个“更新”button,它将根据文本框和combobox的input执行单元格中数据的更改。

一切正常,但是我想在更新行之前保留数据的“存档”。 (基本上将行复制到“存档”表4)

我试图包括.Selection方法,但是这改变我的活动工作表和更新信息不会出现。

这里是代码:

Private Sub cmdUpdate_Click() ' To write edited info of userform2 to Sheets("Data") Dim LastRow As Long Dim ABnum As Double Dim ABrng As Range Dim WriteRow As Long 'error statement On Error GoTo errHandler: 'hold in memory and stop screen flicker Application.ScreenUpdating = False ' Make sure we're on the right sheet Sheets("Data").Select With ActiveSheet ' Get the last row used so can set up the search range LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' Set the range to search for the AB number Set ABrng = .Range("A1:A" & LastRow) ' Get the AB number from what is selected on userform2 ABnum = txtup1.Value ' Get the row of sheet for this AB number WriteRow = Application.Match(ABnum, ABrng, 0) ' Make this AB number the active cell Cells(WriteRow, 1).Select ' Write in all the editable options With ActiveCell .Offset(0, 4) = cboup3.Value .Offset(0, 5) = cboup4.Value .Offset(0, 6) = cboup5.Value .Offset(0, 7) = cboup6.Value .Offset(0, 8) = Date .Offset(0, 12) = txtup9.Value .Offset(0, 13) = txtup8.Value End With End With ' Filter the Data FilterMe ' Close the form Unload Me MsgBox ("Enquiry E0" + Me.txtup1.Text + " has been updated") errHandler: 'Protect all sheets if error occurs 'Protect_All 'show error information in a messagebox If Err.Number <> 0 Then MsgBox "Error " & Err.Number & " just occured." End If End Sub 

我不完全确定你在问什么,但这是一个最好的猜测。 我只是在想要存储一块数据的地方刺了一刀,但如果我在正确的路线上,那么可以修改代码来覆盖其他的代码。 如果您可以澄清将帮助的目标归档表。

 Private Sub cmdUpdate_Click() ' To write edited info of userform2 to Sheets("Data") Dim LastRow As Long Dim ABnum As Double Dim ABrng As Range Dim WriteRow As Long 'error statement On Error GoTo errHandler: 'hold in memory and stop screen flicker Application.ScreenUpdating = False ' Make sure we're on the right sheet With Sheets("Data") ' Get the last row used so can set up the search range LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' Set the range to search for the AB number Set ABrng = .Range("A1:A" & LastRow) ' Get the AB number from what is selected on userform2 ABnum = txtup1.Value ' Get the row of sheet for this AB number WriteRow = Application.Match(ABnum, ABrng, 0) ' Make this AB number the active cell With .Cells(WriteRow, 1) ' Write in all the editable options Sheets("Archive").Range("A" & Rows.Count).End(xlUp)(2).Resize(, 14).Value = .Resize(, 14).Value .Offset(0, 4) = cboup3.Value .Offset(0, 5) = cboup4.Value .Offset(0, 6) = cboup5.Value .Offset(0, 7) = cboup6.Value .Offset(0, 8) = Date .Offset(0, 12) = txtup9.Value .Offset(0, 13) = txtup8.Value End With End With ' Filter the Data FilterMe ' Close the form Unload Me MsgBox ("Enquiry E0" + Me.txtup1.Text + " has been updated") errHandler: 'Protect all sheets if error occurs 'Protect_All 'show error information in a messagebox If Err.Number <> 0 Then MsgBox "Error " & Err.Number & " just occured." End If End Sub