Userform VBA to update existing row on sheet

Darren Sherwood 1 Reputation point
2022-08-09T19:14:48.74+00:00

Hello. i hope somebody is able to help me, my knowledge of VBA isn't great to begin with and i havnt been able to find the answer online (pr at least one that i can follow) .

i have a spreadsheet with several userforms. one to add, remove and edit existing users on a sheet. the add and delete ones i have been able to do but i need some help with the VBA for the form to edit. so far i have the below which populates a combo box which you can then use to select which person to edit. The code i have pulls through the data from the relevant columns into the userform, but it won't allow me to edit the data on the form and have that written back to the sheet.

Private Sub ComboBox2_Change()

Dim i As Long, LastRow As Long

LastRow = Sheets("Daily Report").Range("AL" & Rows.Count).End(xlUp).Row

For i = 1 To LastRow

If Sheets("Daily Report").Cells(i, "D").Value = (Me.ComboBox2) Or _

Sheets("Daily Report").Cells(i, "D").Value = Val(Me.ComboBox2) Then

Me.TextBox1 = Sheets("Daily Report").Cells(i, "D").Value

Me.TextBox2 = Sheets("Daily Report").Cells(i, "AK").Value

Me.TextBox3 = Sheets("Daily Report").Cells(i, "B").Value

Me.TextBox4 = Sheets("Daily Report").Cells(i, "A").Value

Me.TextBox5 = Sheets("Daily Report").Cells(i, "C").Value

Me.ComboBox1 = Sheets("Daily Report").Cells(i, "AM").Value

Me.TextBox6 = Sheets("Daily Report").Cells(i, "AE").Value

Me.TextBox7 = Sheets("Daily Report").Cells(i, "AF").Value

End If

Next

End Sub

Would someone be able to help me with what i need to add / change to write it back to the sheet?, and if possible help me format so that TextBox 6 and 7 show the time in the right format rather than convert it to text please? i have linked a sample sheet with some dummy data

Thank you in advance for any help

Sample Sheet

Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Oskar Shon 866 Reputation points
    2022-11-09T22:05:30.647+00:00

    1st: if you using loop, then you replace information in your controls. Because textbox can have single text inside.
    but Combo is a collection, then you can using .add to have more then one line.

    2nd: If you want to paste that text from controls to sheet then you should paste, from right to left.

    Sheets("Daily Report").Cells(i, "AF").Value = Me.TextBox7.text   
    

    Regards

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.