Search code examples
excelvbaif-statementlabelcaption

Find The Row on Label Caption


I have a UserForm with a label ID. I need to find and modify the row only if the first column of the row is equal to the label Id caption of the form.

When I press the button nothing happens to the selected row.

Private Sub CommandButton2_Click()
    Dim I as integer 
    For I = 2 to Worksheets(“sheet1”).Range(“A10000”).end(xlUp).row
        If cells (i, 1) = label1.caption then
            Cells (i, 3).Value  = TextBox1.text
        End if
    Next I
End sub 

Solution

  • Does this work? Assume the button is on a form with the label and textbox?

    If it only occurs once then could exit the loop once found.

    I've added some sheet references and changed the curly quotes for straight ones.

    Private Sub CommandButton2_Click()
    
    Dim I as long
    
    For I = 2 to Worksheets("sheet1").Range("A10000").end(xlUp).row
       If Worksheets("sheet1").cells(i, 1) = label1.caption then
           Worksheets("sheet1").Cells (i, 3).Value  = TextBox1.text
       End if
    Next I 
    
    End sub