VBA save button to overwrite duplicate row in database

sved

New Member
Joined
Mar 2, 2011
Messages
2
Hello, I have some code that was basically written through help forums. Please be patient with me. I would like to get something working properly. Basically what the workbook does is takes all the information entered on one sheet (CashOut) and flattens it out into one row on another sheet called (CashOutDb). The save button on the CashOut sheet copies the row that was made on CashOutDb and saves that row to another sheet named (DB) that is acting like a database. Right now the code writes to the database and reads from it. What I need is for the code to recognise what line a duplicate value was found in and to overwrite the proper line where it found the duplicate value. Currently the code recognises that there is a duplicate value and when writing over the database it just creates a new entry. Im new to vba but from what I can tell is that the line
Rich (BB code):
intcount = Application.WorksheetFunction.CountIf(ws.[A:A], ws2.[a2])
        If intcount > 0 Then
Is basically checking for any number of duplicates based on comparing it to ws2.[a2]. And when it writes back into the database the lines of code its using just adds a new line to it.
Rich (BB code):
    iRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    ws2.Range("A2").Resize(, 240).Copy
    ws.Cells(iRow + 1, 1).PasteSpecial (xlPasteValues)
Im not sure what I need to do to get it to write back to the proper line it found the duplicate. Here is all of the code for the save button
Rich (BB code):
Private Sub CommandButton1_Click()
 
  Select Case True
         Case Range("n5") = "" Or Range("d5") = "" Or Range("t5") = ""
         MsgBox "You never entered the date. Please use Auto Enter and enter the date.", vbExclamation
      Exit Sub
  End Select
 
    If MsgBox("This will save the data and clear the sheet, do you want to continue?", vbOKCancel) = vbOK Then
 
    Dim iRow As Long
    Dim ws As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
 
    Dim rng As Range
    Dim intcount As Integer
    Dim response As VbMsgBoxResult
 
    Set ws = Worksheets("DB")
    Set ws2 = Worksheets("CashOutDB")
    Set ws3 = Worksheets("CashOut")
 
    ActiveWorkbook.Sheets("CashOutDB").Unprotect " password "
    ActiveWorkbook.Sheets("DB").Unprotect " password "
    ActiveWorkbook.Sheets("CashOut").Unprotect " password "
    ActiveWorkbook.Sheets("CONTROL SHEET").Unprotect " password "
 
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
    intcount = Application.WorksheetFunction.CountIf(ws.[A:A], ws2.[a2])
        If intcount > 0 Then
  'Value already exists in column
      Range("A2").Value = Sheets("CashOutDB").Range("a2").Value
      response = MsgBox("This Information has already been saved: " & vbCrLf & _
      Range("A2").Value & vbCrLf & _
      "overwriting this will overwrite the last entry to the database, do you want to continue?", vbQuestion + vbYesNo)
      If response = vbNo Then Exit Sub
 
 
 
     'overwrite row in database
    Dim lText As String
      On Error Resume Next
      Application.DisplayAlerts = False
          lText = Application.InputBox _
           (Prompt:="Reason for Editing?", _
                  Title:="Reason", Type:=2)
      On Error GoTo 0
<o:p></o:p>
  Application.DisplayAlerts = True
<o:p></o:p>
      If lText = "" Then
      MsgBox ("you never stated your reasoning, the data was not overwritten")
          Exit Sub
      Else
      'just comment
      'where its going / where it came from
      'Range("A2").Value
    iRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    ws.Cells(iRow + 1, 241) = lText
      End If
      'whole range
      'overwrite the data to the database
    iRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    ws2.Range("A2").Resize(, 240).Copy
    ws.Cells(iRow + 1, 1).PasteSpecial (xlPasteValues)
    Range("InputCells").Value = ""
  Else
  'ws = db
  'ws2 = CashOutDb
  'ws3 = CashOut
 
   ActiveWorkbook.Sheets("CashOut").Unprotect " password "
      ActiveWorkbook.Sheets("CashOutDb").Unprotect " password "
      'Value not found in column
     'find first empty row in database
    iRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
     'copy the data to the database
ws2.Range("A2").Resize(, 240).Copy
ws.Cells(iRow + 1, 1).PasteSpecial (xlPasteValues)
<o:p></o:p>
     'clear cells
    Range("InputCells").Value = ""
    ActiveWorkbook.Sheets("CashOut").Protect " password "
    ActiveWorkbook.Sheets("CashOutDb").Protect " password "
    ActiveWorkbook.Sheets("Db").Protect " password "
    ActiveWorkbook.Sheets("CONTROL SHEET").Protect "password"
    ActiveWorkbook.Save
<o:p></o:p>
  End If
End If
<o:p></o:p>
<o:p></o:p>
End Sub
If I could just get it to look at ws.[A:A] and compare it to ws2.[a2] and in that row that it found the duplicate overwrite it!.
I have been reading through forums but am having little luck. Any help would be appreciated. Thanks.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top