Replace data in secondary workbook if duplicate found
I have a workbook that has the user transfer their data to a second workbook every hour. Each record is a row of around 20 data points; in the last column I have code for the record, something like 42314-01 through 42304-24, where the suffix is each hour of the day, the first five digits are the numerations of the date. Each time the user submits their data, the code checks to make sure the record isn't already recorded in the second workbook, to prevent duplication.
Right now, if the code detects a duplicate, the user can choose to not transfer it, or they may choose to transfer it anyway; there may have been an error in the initial transaction. What I want to do is overwrite the data if there is a duplicate, but I'm not sure how to do this.
This is the code I'm using to detect the duplication:
PHP Code:
Dim R As Range, S As Range 'CHECK DUPLICATION - R is the value to look for from this workbook, S is the range to look in target workbook
Set R = Range("S" & ActiveCell.row)
Set S = WkShtData.Columns("S").Find(R.Value, LookIn:=xlValues, LookAt:=xlWhole)
Set Wkbk = Workbooks.Open(location & fileName, , , , , 1234) 'Location & name of target workbook, defined in GlobalVar
Set WkShtData = Wkbk.Sheets("A1") 'Name of worksheet in target workbook to use
If Not S Is Nothing Then
If MsgBox("This hour appears to have been already recorded. Continue with data transfer anyway?", vbYesNo, "User Input") = vbNo Then
'Save data workbook and close
errorType = "Duplicate value detected - User did not continue"
Call WriteErrors(errorType) 'keeps track of errors that occur
Wkbk.Save
Wkbk.Close
Exit Sub
Else
errorType = "Duplicate value detected - User Continued with transfer"
Call WriteData 'Sub that transfers data from this Workbook to data collection Workbook
End If
Else
End If
The WriteData sub looks for the first unused row in the target worksheet, and writes the data there, but I'm not sure how to replace a record. I use something like this to find the first unused row:
So now, I need to take a row that has been found to already have the code I'm using to search with, and replace it with the new record. Any ideas on how to do this? I have 20 worksheets that use similar code, each with different data points. We're trying to not have to manually clean up the data every day. Thanks in advance.
Bookmarks