Userform Textbox saving to table

GedSalter

Board Regular
Joined
Apr 24, 2019
Messages
64
Hi all.

I have a userform which imports data from a table. At the moment when a textbox is changed it Saves A:LLthe textbox data back to respective cells. What I need is that the save will only overwrite changed data back into the table and not overwrite data which hasnt changed.

here is my code so far

VBA Code:
Private Sub cmdAddData_Click()

Dim i As Long, LastRow As Long, ws As Worksheet

Set ws = Sheets("Sheet2")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow


If Me.ComboBox1.Value = ws.Cells(i, "A") Then

ws.Cells(i, "B") = txtFirstname.Value
ws.Cells(i, "C") = TxtSurname.Value
ws.Cells(i, "D") = TxtDOB.Value
ws.Cells(i, "E") = Gender.Value
ws.Cells(i, "F") = Membership.Value

ws.Cells(i, "H") = MemberSince.Value

ws.Cells(i, "G") = FEESnowpaid.Value

ws.Cells(i, "I") = PaidDate.Value


End If

Next i

End Sub
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Alex Blakenburg

Well-known Member
Joined
Feb 23, 2021
Messages
2,285
Office Version
  1. 365
Platform
  1. Windows
The question is somewhat confusing.
and not overwrite data which hasnt changed.
If you are "overwriting" data that hasn't changed, you won't know, so why does it matter ?
Changed in comparison to what ?
 

Alex Blakenburg

Well-known Member
Joined
Feb 23, 2021
Messages
2,285
Office Version
  1. 365
Platform
  1. Windows
What is doing the date stamping ?
Is it a field for each cell updated or are you relying on Excel tracking / audit features ?
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
3,560
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Try :
VBA Code:
If ws.Cells(i, "B") <> txtFirstname.Value Then ws.Cells(i, "B") = txtFirstname.Value
do the same thing to other textboxes.
 
Solution

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,932
Office Version
  1. 2019
Platform
  1. Windows
Hi,
untested but see if this update to your code will do what you want

VBA Code:
Private Sub cmdAddData_Click()
    Dim m           As Variant, Entry As Variant
    Dim Search      As Variant
    Dim c           As Long
    Dim ws          As Worksheet
    
    Set ws = Sheets("Sheet2")
    Search = Me.ComboBox1.Value
    
    If Len(Search) = 0 Then Exit Sub
    If IsNumeric(Search) Then Search = Val(Search)
    
    m = Application.Match(Search, ws.Columns(1), 0)
    
    If Not IsError(m) Then
        
        For c = 1 To 8
            Entry = Me.Controls(Choose(c, "txtFirstname", "TxtSurname", "TxtDOB", _
                                            "Gender", "Membership", "MemberSince", _
                                            "FEESnowpaid", "PaidDate")).Value
            
            If IsDate(Entry) Then Entry = DateValue(Entry)
            
            With ws.Cells(CLng(m), c + 1)
                If .Value <> Entry Then .Value = Entry
            End With
            
        Next c
    End If
    
End Sub

Dave
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
3,560
Office Version
  1. 365
Platform
  1. Windows
You're welcome, glad to help & thanks for the feedback.:)
 

Forum statistics

Threads
1,148,179
Messages
5,745,201
Members
423,932
Latest member
pablo2

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
Top