Update data in sheet

Jakesie

New Member
Joined
Mar 7, 2011
Messages
1
Hi there,

Im new here, and Im working on a excell sheet that I use to generate invoices,, record them in a database, etc. I do not have any VBA knowledge, , so i'm learning here.
My question is this: I have a UserForm that is used to add and view customers. all is working fine, except that I need a code (or info) how to update existing info about users. I have the following code. Is there a way to alter it so it can update existing records instead of add to a new line?

Thanking yo in advance,

Jakes.

Code:
Private Sub CommandButton2_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("CustDATABASE")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
  .End(xlUp).Offset(1, 0).Row

'check for a part number
If Trim(Me.cboID.Value) = "" Then
  Me.cboID.SetFocus
  MsgBox "Please enter a User ID number"
  Exit Sub
End If

  If WorksheetFunction.CountIf(ws.Range("A2", ws.Cells(iRow, 1)), Me.cboID.Value) > 0 Then
        MsgBox "Duplicate Customer ID Found", vbCritical
        Exit Sub
    End If
    
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.cboID.Value
ws.Cells(iRow, 2).Value = Me.txtNAME.Value
ws.Cells(iRow, 3).Value = Me.txtSURNAME.Value
ws.Cells(iRow, 4).Value = Me.txtADRESS.Value
ws.Cells(iRow, 5).Value = Me.txtTOWN.Value
ws.Cells(iRow, 6).Value = Me.txtNUMBER.Value
ws.Cells(iRow, 7).Value = Me.txtEMAIL.Value
ws.Cells(iRow, 8).Value = Me.txtVAT.Value

'clear the data
Me.cboID.Value = ""
Me.txtNAME.Value = ""
Me.txtSURNAME.Value = ""
Me.txtADRESS.Value = ""
Me.txtTOWN.Value = ""
Me.txtNUMBER.Value = ""
Me.txtEMAIL.Value = ""
Me.txtVAT.Value = ""
Me.cboID.SetFocus
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hello and Welcome,

You should be able to replace these lines (which say: if you find a matching ID, stop)

Code:
If WorksheetFunction.CountIf(ws.Range("A2", ws.Cells(iRow, 1)), Me.cboID.value) > 0 Then
    MsgBox "Duplicate Customer ID Found", vbCritical
    Exit Sub
End If

with this code (which says: if you find a matching ID, use that row instead of a new last row).

Code:
Dim c As Range
Set c = ws.Range("A2", ws.Cells(iRow, 1)).Find(What:=Me.cboID.value, _
    LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False)
If Not c Is Nothing Then
    iRow = c.Row
    Set c = Nothing
End If

It's hard for me to test this without having a copy of your workbook. If it doesn't work, let me know and it shouldn't be too hard to debug.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

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