excel - 在文本框 1 中找到 ID 以查找未将复选框选择应用于该行的行

标签 excel vba

我无法获得以下代码来在 A 列中找到匹配的名称,然后将复选框选择应用于该行的待处理列。

代码

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

最佳答案

您将无法按原样运行代码,因为您缺少 end if
如果您正在搜索 A 列中的文本框值,如果找到,为什么要将其设为文本框的值?

您不能使用 FoundID.Row如果未找到文本框值。

是否仅在找到值时才使用是/否值?

您不需要两个复选框,如果 checkbox1=true 然后"is",否则“否”

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

关于excel - 在文本框 1 中找到 ID 以查找未将复选框选择应用于该行的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467555/

相关文章:

vba - Excel VBA - 通过列重复宏

c# - 如何将 excel ListObject 添加到 C# 中的给定工作表?

vba - Excel VBA - 汇总一列

c# - 替换excel文件一列中的字符

vba - VBA代码外包

VBA 最后一行命令

vba - 如何在没有 xmlns/idQ 的情况下将组添加到(VBA)自定义功能区?

excel - VBA MACRO为选定单元格右侧的一个单元格着色

excel - 通过 VBA 从剪贴板粘贴到 Word 标题的图像既不列为 InlineShape 也不列为 Shape

vb.net - 为什么我的 Excel 工作簿在 Excel 2010 中不可见?