Excel 如何避免重复记录?
我正在使用“保存”按钮将新记录插入另一张工作表。但我希望避免插入相同的数据(如果name+lastname+birthday等于新数据,则数据是相同的)。我正在尝试以下代码,但速度太慢,无法工作。下面的代码有什么问题?谢谢Excel 如何避免重复记录?,excel,vba,Excel,Vba,我正在使用“保存”按钮将新记录插入另一张工作表。但我希望避免插入相同的数据(如果name+lastname+birthday等于新数据,则数据是相同的)。我正在尝试以下代码,但速度太慢,无法工作。下面的代码有什么问题?谢谢 Sub saveFormData() Dim name As String, lastname As String, birthday As String ' Get last empty row lastRow = Sheets("saveData").Ce
Sub saveFormData()
Dim name As String, lastname As String, birthday As String
' Get last empty row
lastRow = Sheets("saveData").Cells(Rows.Count, 1).End(xlUp).Row + 1
name = Worksheets("form").Range("A1").Value
lastname = Worksheets("form").Range("A2").Value
birthday = Worksheets("form").Range("A3").Value
For i = 2 To lastRow
' Check if data exist (record is unique if we have name + lastname + birthday
If Worksheets("saveData").Range("A" & lastRow).Value = name and Worksheets("saveData").Range("B" & lastRow).Value = lastname and Worksheets("saveData").Range("C" & lastRow).Value = birthday Then
MsgBox "Data already exist"
Exit Sub 'Exit from Sub
End If
Next
' Save name
Worksheets("saveData").Range("A" & lastRow).Value = name
' Save lastname
Worksheets("saveData").Range("B" & lastRow).Value = lastname
' Save birthday
Worksheets("saveData").Range("C" & lastRow).Value = birthday
End Sub
使用数组和字典始终是读取大量数据的最快方式:
Option Explicit
Sub saveFormData()
Dim arrSaveData
Dim LastRow As Long
Dim SavedData As New Scripting.Dictionary 'Need Microsoft Scripting Runtime reference to work
Dim i As Long
'store the saved data inside the array
With ThisWorkbook.Sheets("saveData")
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
arrSaveData = .Range("A2:C" & LastRow)
End With
'Save every entry into the dictionary
For i = LBound(arrSaveData) To UBound(arrSaveData)
SavedData.Add arrSaveData(i, 1) & arrSaveData(i, 2) & arrSaveData(i, 3), 1
Next i
Dim name As String, lastname As String, birthday As String
'store your variables
With ThisWorkbook.Sheets("form")
name = .Range("A1")
lastname = .Range("A2")
birthday = .Range("A3")
End With
'Check if the new entry doesn't exists and if it doesn't add it
With ThisWorkbook.Sheets("SaveData")
If Not SavedData.Exists(name & lastname & birthday) Then
LastRow = LastRow + 1
.Cells(LastRow, 1) = name
.Cells(LastRow, 2) = lastname
.Cells(LastRow, 3) = birthday
Else
MsgBox "Data already exists."
End If
End With
End Sub
如果您的
生日
数据是日期,则该代码可能不起作用,数组将它们存储为日期,而生日
变量是字符串,因此在这种情况下,您应该将生日切换为日期
请尝试以下代码:
Sub saveFormData()
Dim name As String, lastname As String, birthday As String
'Declare the worksheets
Dim sdSH As Worksheet, fSH As Worksheet
Set sdSH = ThisWorkbook.Sheets("saveData")
Set fSH = ThisWorkbook.Sheets("form")
' Get last empty row
lastrow = sdSH.Cells(Rows.Count, 1).End(xlUp).Row + 1
name = fSH.Range("A1").Value
lastname = fSH.Range("A2").Value
birthday = fSH.Range("A3").Value
'Transfer the data for 'saveData to array
Dim saveData() As String
ReDim Preserve saveData(1 To lastrow, 1 To 3) As String
For a = 1 To lastrow
For b = 1 To 3
saveData(a, b) = sdSH.Cells(a, b).Value
Next b
Next a
For i = 2 To UBound(saveData)
' Check if data exist (record is unique if we have name + lastname + birthday
If saveData(i, 1) = name And saveData(i, 2) = lastname And saveData(i, 3) = birthday Then
MsgBox "Data already exist"
Exit Sub 'Exit from Sub
End If
Next
' Save name
sdSH.Range("A" & lastrow).Value = name
' Save lastname
sdSH.Range("B" & lastrow).Value = lastname
' Save birthday
sdSH.Range("C" & lastrow).Value = birthday
End Sub
您应该在for循环中使用i变量而不是lastRow,我正在尝试使用数组重建代码,以提高处理数千项的速度,但如果没有,只需将变量lastRow更改为i即可。我会尝试用arrayHi发布代码,您的代码可以改进。当您可以立即执行时,您正在循环存储该数组,然后再次在该数组中循环以检查新条目是否存在。使用字典将大大加快代码的速度(取决于数据量),请查看我的答案,看看我在说什么:)非常感谢它的工作。我将接受@Damian Suggestion非常感谢,我将与@Love coding one结合使用