سؤال

I want to edit the data on the cells of a worksheet using a Userform and a couple of TextBoxes and ComboBoxes.

this is the code i use to go through the data on the cells:

Private Sub ComboBox1_Click()
Dim rfound As Range
    On Error Resume Next
    With Sheets("IO-DB")
    Set rfound = .Columns(1).Find(What:=ComboBox1.Value, After:=.Cells(1, 1), LookIn:=xlValues, LookAt:= _
        xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False)
On Error GoTo 0
If rfound Is Nothing Then Exit Sub
Application.Goto rfound, True
TextBox1.Text = rfound.Offset(0, 0)
TextBox9.Text = rfound.Offset(0, 1)
ComboBox7.Text = rfound.Offset(0, 3)
ComboBox8.Text = rfound.Offset(0, 2)
ComboBox5.Text = rfound.Offset(0, 4)
TextBox4.Text = rfound.Offset(0, 5)
TextBox5.Text = rfound.Offset(0, 6)
ComboBox3.Text = rfound.Offset(0, 7)
ComboBox4.Text = rfound.Offset(0, 8)
ComboBox6.Text = rfound.Offset(0, 10)
TextBox7.Text = rfound.Offset(0, 9)

End With
End Sub

this code searches through a row and puts it in each of the TextBox/ComboBox assigned to them. and I used ComboBox1 as a Search Function for my data.

how do i edit the data on the cells using the Userform and the search method I have?

هل كانت مفيدة؟

المحلول

If I have understood well...
You code it's correct to take data... but Using your code you have:
1) You cannot insert directly a name, but you need to populate the combo with all the data before (I have linked to the first column)
2) During The update using:

ActiveCell.Offset(0, 0) = TextBox1.Text
ActiveCell.Offset(0, 1) = TextBox9.Text
ActiveCell.Offset(0, 3) = ComboBox7.Text
ActiveCell.Offset(0, 2) = ComboBox8.Text
...

you can have problem because VBA can update the value of Textbox BEFORE you write in the cells
To solve the point 1) you can use the event AfterUpdate. Work with Click and direct insert
To solve the point 2) you can use this code:

Dim Tmp As String
Tmp = TextBox2.Text
ActiveCell.Offset(0, 1) = tmp

for each cell... The update code you can insert in a Button:

Private Sub CommandButton1_Click()

In the button code you need to use "ActiveCell" instead rfound because this var you don't have, but your code select the cell...

نصائح أخرى

I have modify your code like that. Changed the event:

Private Sub ComboBox1_AfterUpdate()

change the code:

Dim rfound As Range
On Error GoTo NOFIND

Sheets("Sheet1").Columns(1).Select
Selection.Find(What:=ComboBox1.Value, After:=Sheets("Sheet1").Cells(1, 1), LookIn:=xlValues, LookAt:= _
    xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate

TextBox1.Text = ActiveCell.Offset(0, 0)
TextBox2.Text = ActiveCell.Offset(0, 1)

NOFIND:
End Sub

To update che cell add a button Update with this code:

ActiveCell.Offset(0, 0) = TextBox1.Text
ActiveCell.Offset(0, 1) = TextBox2.Text
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top