Home » excel » excel – Found ID in textbox 1 to find row not applying checkbox choice to that row

excel – Found ID in textbox 1 to find row not applying checkbox choice to that row

Posted by: admin May 14, 2020 Leave a comment

Questions:

I can’t get the following code to find a matching name in column A then to apply the checkbox choice to the pending columns along that row.

Code


Private Sub CommandButton1_Click()
    Dim lrEM As Long
    Dim FoundID As Range
    Set FoundID = Sheets("Employee-Software").Range("A:A").Find(What:=TextBox1.Text, Lookat:=xlWhole)
    If Not FoundID Is Nothing Then
        Sheets("Employee-Software").Cells(FoundID.Row, "A").Value = TextBox1.Text
    Else
        lrEM = Sheets("Employee-Software").Range("A" & Rows.Count).End(xlUp).Row + 1
        Sheets("Employee-Software").Cells(lrEM, "A").Value = TextBox1.Text
    End If
    If CheckBox1.Value = True Then
        Sheets("Employee-Software").Cells(FoundID.Row, "B").Value = "Yes"
        If CheckBox2.Value = True Then
            Sheets("Employee-Software").Cells(FoundID.Row, "B").Value = "No"
        End If
    End Sub
How to&Answers:

You would not be able to run the code as is because you are missing an end if

If you are searching for the textbox value in column A, why would you make it the value of the textbox if found?

You cannot use FoundID.Row if the textbox value is not found.

Did you want to use the Yes/No value only if the value is found?

You don’t need two checkboxes, if checkbox1=true then “yes” else “no”

Private Sub CommandButton1_Click()
    Dim lrEM As Long, x
    Dim FoundID As Range, sh As Worksheet

    Set sh = Sheets("Employee-Software")

    With sh
        x = IIf(CheckBox1 = True, "Yes", "No")
        Set FoundID = .Range("A:A").Find(What:=TextBox1.Text, Lookat:=xlWhole)

        If Not FoundID Is Nothing Then
            'Sheets("Employee-Software").Cells(FoundID.Row, "A").Value = TextBox1.Text

            FoundID.Offset(, 1) = x
        Else

            lrEM = .Range("A" & .Rows.Count).End(xlUp).Row + 1
            .Cells(lrEM, "A").Value = TextBox1.Text
            .Cells(lrEM, "B").Value = x
        End If

    End With
End Sub

Answer:

The only problem i could find was in your last lines in if

If CheckBox1.Value = True Then
    Sheets("Employee-Software").Cells(FoundID.Row, "B").Value = "Yes"
If CheckBox2.Value = True Then
    Sheets("Employee-Software").Cells(FoundID.Row, "B").Value = "No"
End If

It should be

If CheckBox1.Value = True Then
    Sheets("Employee-Software").Cells(FoundID.Row, "B").Value = "Yes"
else CheckBox2.Value = True Then
    Sheets("Employee-Software").Cells(FoundID.Row, "B").Value = "No"
End If