Search code examples
excelvbacomboboxuserform

Check for row with combobox value and use that as a pointer to populate the cell in that active row


I have constructed the code below to find the combobox2 value (if there is one, if not do nothing) in Column A of the worksheet "MFGLR" and then in the same row as the combobox2 value paste the value of the textbox in the column AE. I can not find out how to have it work correctly.

Background: I have a userform that has 2 comboboxes and 1 textbox. I have created 3 scenarios and for 2 of the scenarios I need what I said above to happen.

Sub linepick()

Dim N As Long
Dim i As Long

'Check for a row with combobox2 value and use that as a pointer to populate the textbox in that active row

N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 5 To N

If Cells(i, "A").Value = Combobox2.value Then

'The  = Combobox2.value brings up an error

Cells(i, "A").Rows.Select

End If

Next i

Userform2.Textbox1.text = CStr(Worksheets("MFGLR").Range("AE" & ActiveCell.Row).Value)

End Sub

If I haven't explained enough please let me know. Thanks


Solution

  • I think this does what you want.

    • This code isn't behind the form so you need to add a reference to it (as you did further down).
    • You can also exit the sub once found.
    • You were transferring the value of AE to the textbox so I have turned it round.
    • Make sure you include sheet references consistently.

      Sub linepick()
      
      Dim N As Long
      Dim i As Long
      
      'Check for a row with combobox2 value and use that as a pointer to populate the textbox in that active row
      
      N = Worksheets("MFGLR").Cells(Rows.Count, "A").End(xlUp).Row
      
      For i = 5 To N
          If Worksheets("MFGLR").Cells(i, "A").Value = Userform2.Combobox2.Value Then
              Worksheets("MFGLR").Cells(i, "AE").Value = Userform2.Textbox1.Text
              Exit Sub
          End If
      Next i
      
      End Sub