如何通知用户他们创建了重复记录,并询问他们是否要覆盖?
How to inform user they have created a duplicate record, and ask if they want to overwrite?
我有一个工作簿并创建了命令按钮。我已经成功地创建了 VBA 到 2 个按钮,但我正在努力处理重复代码。我正在将工作表中特定单元格的数据传输到名为“Master”的工作表中,我的文件是通过保存命令自动创建的,该命令会自动将工作表保存为发票 (c10),然后保存为项目参考 (a12)。我正在尝试将代码写入我现有的 VBA 以告诉我正在添加重复项,并询问我是否要覆盖它?
这是我的进展,我花了几个小时在重复功能上,试图纠正它,但我总是摔倒。除(“C10”)外,该行中允许的所有其他重复项都是允许的。我只是不能让这个为我工作。
请问有人能帮忙吗?
Dim Item As Range, DataEntry As Range
Dim Data() As Variant
Dim i As Integer
Dim DataRow As Long
Dim wsMaster As Worksheet
Set wsMaster = ThisWorkbook.Worksheets("Master")
Set DataEntry = ActiveSheet.Range("C10,A3,A12,E10,F33")
Dim assetIDRange, a As Range
For Each Item In DataEntry
i = i + 1
ReDim Preserve Data(1 To i)
Data(i) = Item.Value
Next
DataRow = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Row + 1
wsMaster.Cells(DataRow, 1).Resize(1, DataEntry.Cells.Count).Value = Data
MsgBox "Record Added To Master", 48, "Record Added"
End Sub
尝试这样的事情:
Sub InsertUpdate()
Dim Item As Range, DataEntry As Range
Dim Data() As Variant
Dim i As Integer
Dim wsMaster As Worksheet
Dim m, c As Range, action
Set wsMaster = ThisWorkbook.Worksheets("Master")
Set DataEntry = ActiveSheet.Range("C10,A3,A12,E10,F33")
ReDim Data(1 To 1, 1 To DataEntry.Cells.Count)
For Each Item In DataEntry.Cells
i = i + 1
Data(1, i) = Item.Value
Next
'existing row with same C10 value?
m = Application.Match(Data(1, 1), wsMaster.Columns("a"), 0)
If Not IsError(m) Then 'found existing match?
If MsgBox("Replace existing entry for '" & Data(1, 1) & "' ?", vbQuestion + vbYesNo) = vbYes Then
Set c = wsMaster.Columns("a").Cells(m)
action = "Updated"
End If
Else
Set c = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Offset(1, 0)
action = "Added"
End If
If Not c Is Nothing Then
c.Resize(1, UBound(Data, 2)).Value = Data
MsgBox "Record " & action & " To Master", 48, "Record Added"
End If
End Sub
我有一个工作簿并创建了命令按钮。我已经成功地创建了 VBA 到 2 个按钮,但我正在努力处理重复代码。我正在将工作表中特定单元格的数据传输到名为“Master”的工作表中,我的文件是通过保存命令自动创建的,该命令会自动将工作表保存为发票 (c10),然后保存为项目参考 (a12)。我正在尝试将代码写入我现有的 VBA 以告诉我正在添加重复项,并询问我是否要覆盖它?
这是我的进展,我花了几个小时在重复功能上,试图纠正它,但我总是摔倒。除(“C10”)外,该行中允许的所有其他重复项都是允许的。我只是不能让这个为我工作。 请问有人能帮忙吗?
Dim Item As Range, DataEntry As Range
Dim Data() As Variant
Dim i As Integer
Dim DataRow As Long
Dim wsMaster As Worksheet
Set wsMaster = ThisWorkbook.Worksheets("Master")
Set DataEntry = ActiveSheet.Range("C10,A3,A12,E10,F33")
Dim assetIDRange, a As Range
For Each Item In DataEntry
i = i + 1
ReDim Preserve Data(1 To i)
Data(i) = Item.Value
Next
DataRow = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Row + 1
wsMaster.Cells(DataRow, 1).Resize(1, DataEntry.Cells.Count).Value = Data
MsgBox "Record Added To Master", 48, "Record Added"
End Sub
尝试这样的事情:
Sub InsertUpdate()
Dim Item As Range, DataEntry As Range
Dim Data() As Variant
Dim i As Integer
Dim wsMaster As Worksheet
Dim m, c As Range, action
Set wsMaster = ThisWorkbook.Worksheets("Master")
Set DataEntry = ActiveSheet.Range("C10,A3,A12,E10,F33")
ReDim Data(1 To 1, 1 To DataEntry.Cells.Count)
For Each Item In DataEntry.Cells
i = i + 1
Data(1, i) = Item.Value
Next
'existing row with same C10 value?
m = Application.Match(Data(1, 1), wsMaster.Columns("a"), 0)
If Not IsError(m) Then 'found existing match?
If MsgBox("Replace existing entry for '" & Data(1, 1) & "' ?", vbQuestion + vbYesNo) = vbYes Then
Set c = wsMaster.Columns("a").Cells(m)
action = "Updated"
End If
Else
Set c = wsMaster.Cells(wsMaster.Rows.Count, "A").End(xlUp).Offset(1, 0)
action = "Added"
End If
If Not c Is Nothing Then
c.Resize(1, UBound(Data, 2)).Value = Data
MsgBox "Record " & action & " To Master", 48, "Record Added"
End If
End Sub