Userform Textbox saving to table

GedSalter

Board Regular
Joined
Apr 24, 2019
Messages
80
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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What is doing the date stamping ?
Is it a field for each cell updated or are you relying on Excel tracking / audit features ?
 
Upvote 0
Try :
VBA Code:
If ws.Cells(i, "B") <> txtFirstname.Value Then ws.Cells(i, "B") = txtFirstname.Value
do the same thing to other textboxes.
 
Upvote 0
Solution
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
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,630
Messages
6,120,634
Members
448,973
Latest member
ChristineC

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