Bomino
Member
Hello,
I trying to build a little data base in a spreadsheet and updating the records at the same time with the following code:
Assuming this isn't too bad a way of coding this, how do I get back the value of the cells before the change when Resp=VbNo is selected? See commented section on my code.
Any help will be greatly appreciated!
Thanks.
I trying to build a little data base in a spreadsheet and updating the records at the same time with the following code:
Code:
Dim wsdb As Worksheet, wsentry As Worksheet
Dim Lr As Long, myNr As Long
Dim Team As Range, Name As Range, wght As Range
Dim hght As Range, adr As Range, phne As Range
Dim Chck As Long
Dim Resp As Integer
Dim x As Variant
Set wsdb = ThisWorkbook.Sheets("DB")
Set wsentry = ThisWorkbook.Sheets("DataEntry")
Lr = wsdb.Cells(Rows.Count, 1).End(xlUp).Row
myNr = Lr + 1
On Error GoTo Rpt
Chck = WorksheetFunction.CountIfs(wsdb.Range("Team"), wsentry.Range("B3"), wsdb.Range("Name"), wsentry.Range("B4"), wsdb.Range("wght"), wsentry.Range("B5"))
If Chck > 0 Then
Resp = MsgBox("Candidat already registered." & vbNewLine & "Would you like to saves changes?", vbYesNo)
If Resp = vbNo Then Exit Sub ' if this option is selected I would like
'the changed cells (B6,B7 and B8 on wsentry)
' to reurned their previous values
If Resp = vbYes Then
For x = 2 To Lr
If wsdb.Cells(x, 1) = wsentry.Cells(3, 2) And wsdb.Cells(x, 2) = wsentry.Cells(4, 2) And wsdb.Cells(x, 3) = wsentry.Cells(5, 2) Then
With wsdb
.Cells(x, 4) = wsentry.Cells(6, 2)
.Cells(x, 5) = wsentry.Cells(7, 2)
.Cells(x, 6) = wsentry.Cells(8, 2)
End With
End If
Next
End If
End If
Rpt:
With wsdb
.Cells(myNr, 1) = wsentry.Cells(3, 2)
.Cells(myNr, 2) = wsentry.Cells(4, 2)
.Cells(myNr, 3) = wsentry.Cells(5, 2)
.Cells(myNr, 4) = wsentry.Cells(6, 2)
.Cells(myNr, 5) = wsentry.Cells(7, 2)
.Cells(myNr, 6) = wsentry.Cells(8, 2)
.Cells(1, 1).CurrentRegion.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes
End With
End Sub
Any help will be greatly appreciated!
Thanks.