Работа с массивами и словарями - это всегда самый быстрый способ чтения больших объемов данных:
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
Код может не работать, если ваши данные birthday
являются датами, массив будет хранить их как даты, а ваша переменная birthday
является строкой, поэтому в этом случае вам следует переключить birthday As Date