我目前有一个用户表格,可以将其输入到电子表格中。我要求一旦将信息输入到UserForm中,就必须在电子表格中搜索与相同PRI匹配的行,如果是的话,则删除先前的信息,并在工作表中填充新的信息。重要的是,仅删除匹配的PRI编号,并且如果新提交作为以前提交的其他选项,则不会覆盖其他行。这是我目前的编码:
Private Sub CmdAdd_Click()
Dim ws As Worksheet
Dim info
Dim rw As Range
Dim n As Long
Dim r As Range
Const strPwd As String = "Transfer19"
ThisWorkbook.Unprotect Password:=strPwd
Set ws = Worksheets("Inventory")
With ws
'get all the tombstone info into an array
info = Array(Me.TxtFirst.Value, Me.TxtLast.Value, _
Me.TxtPRI.Value, Me.TxtGR.Value, _
Me.TxtLV.Value, Me.TxtLinguistic.Value, _
Me.TxtEmail.Value, Me.TxtResumeNum.Value, _
Me.TxtReason.Value, Me.TxtDate.Value)
.Unprotect Password:="Transfer19"
'get the first empty row...
Set rw = .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow
'loop over the province and city controls
For n = 1 To 10
'get province and city values
p = Me.Controls("ListProv" & n).Value
c = Me.Controls("ListCity" & n).Value
If n = 1 Or p <> "" Then
rw.Cells(1).Resize(1, 10).Value = info
rw.Cells(11).Value = p
rw.Cells(12).Value = c
Set rw = rw.Offset(1, 0)
End If
Next n
.Protect Password:="Transfer19"
End With
ThisWorkbook.Protect Password:=strPwd
ThisWorkbook.Save
End Sub
这是我尝试过的部分有效的方法。它找到重复项并替换为新信息,但不适用于当前循环
'searching for duplicates
Set r = ws.Range("C:C").Find(Me.TxtPRI.Value, LookIn:=xlValues, lookat:=xlWhole)
If Not r Is Nothing Then
MsgBox "Duplicate entry. The record is deleted and the new data is placed"
iRow = r.Row
End If