'How do I save changes made to data within a Userform back to the same row where the data came from?
I have found code to search a table and load cell values into the form. I need to be able to make changes and save the form data back to the table to update the row.
This modified code works great to open the form, find the record and load the data.
' Written by Philip Treacy
' https://www.myonlinetraininghub.com/searching-for-data-with-user-form
'
Private Sub CommandButton1_Click()
Dim RecordRow As Long
Dim RecordRange As Range
' Turn off default error handling so Excel does not display
' an error if the record number is not found
On Error Resume Next
' Find the row in the table that the record is in
RecordRow = Application.Match(CLng(TextBox1.Value), Range("JobSheet[W/O]"), 0)
' Set RecordRange to the first cell in the found record
Set RecordRange = Range("JobSheet").Cells(1, 1).Offset(RecordRow - 1, 0)
' If an erro has occured i.e the record number was not found
If Err.Number <> 0 Then
ErrorLabel.Visible = True
On Error GoTo 0
Exit Sub
End If
' Turn default error handling back on (Let Excel handle errors from now on)
On Error GoTo 0
' If the code gets to here the record number was found
' Hide the error message 'Not Found'
ErrorLabel.Visible = False
' and populate the form fields with the record's data
TextBox4.Value = RecordRange(1, 1).Offset(0, 6).Value
TextBox5.Value = RecordRange(1, 1).Offset(0, 5).Value
TextBox2.Value = RecordRange(1, 1).Offset(0, 15).Value
TextBox3.Value = RecordRange(1, 1).Offset(0, 16).Value
CheckBoxBell.Value = RecordRange(1, 1).Offset(0, 17).Value
CheckBoxGas.Value = RecordRange(1, 1).Offset(0, 18).Value
CheckBoxHydro.Value = RecordRange(1, 1).Offset(0, 19).Value
CheckBoxWater.Value = RecordRange(1, 1).Offset(0, 20).Value
CheckBoxCable.Value = RecordRange(1, 1).Offset(0, 21).Value
CheckBoxOther1.Value = RecordRange(1, 1).Offset(0, 22).Value
CheckBoxOther2.Value = RecordRange(1, 1).Offset(0, 23).Value
CheckBoxOther3.Value = RecordRange(1, 1).Offset(0, 24).Value
End Sub
I have been searching and for code to reinsert the data back into the row but have not been able to find a solution.
This is the User form:
I want to click the update button and have the data saved into the same row. I know it is recommended to post code you have tried but I have tried so many things that there is no way I could post it and have it make any sense.
Solution 1:[1]
With the help of @cybernetic.nomad, I was able to create another sub that recorded the changes. In case someone ends up here, I wanted to post the code for the solution.
Private Sub UpdateRecord()
Dim RecordRow As Long
Dim RecordRange As Range
' Find the row in the table that the record is in
RecordRow = Application.Match(CLng(TextBoxWO.Value), Range("JobSheet[W/O]"), 0)
' Set RecordRange to the first cell in the found record
Set RecordRange = Range("JobSheet").Cells(1, 1).Offset(RecordRow - 1, 0)
RecordRange(1, 1).Offset(0, 5).Value = TextBoxComp.Value
RecordRange(1, 1).Offset(0, 6).Value = TextBoxHold.Value
RecordRange(1, 1).Offset(0, 8).Value = TextBoxDays.Value
RecordRange(1, 1).Offset(0, 10).Value = CheckBoxLocate.Value
RecordRange(1, 1).Offset(0, 15).Value = TextBoxFirst.Value
RecordRange(1, 1).Offset(0, 16).Value = TextBoxOveride.Value
RecordRange(1, 1).Offset(0, 17).Value = CheckBoxBell.Value
RecordRange(1, 1).Offset(0, 18).Value = CheckBoxGas.Value
RecordRange(1, 1).Offset(0, 19).Value = CheckBoxHydro.Value
RecordRange(1, 1).Offset(0, 20).Value = CheckBoxWater.Value
RecordRange(1, 1).Offset(0, 21).Value = CheckBoxCable.Value
RecordRange(1, 1).Offset(0, 22).Value = CheckBoxOther1.Value
RecordRange(1, 1).Offset(0, 23).Value = CheckBoxOther2.Value
RecordRange(1, 1).Offset(0, 24).Value = CheckBoxOther3.Value
End Sub
It works perfectly!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DryBSMT |

