在表格中查找一行并从用户表单中插入值

时间:2019-09-20 21:52:22

标签: excel vba

对不起,我对此进行了很多次令人困惑的编辑,所以我将重新开始。我写了两个不同的功能都在工作,而香港专业教育学院试图结合这两个来获得此功能,但我有麻烦。我想找到一个具有匹配名称的行,然后在该行的5列中插入值。

现在我在此行收到错误消息“对象不支持此属性或方法”:

.Cells(LastRow,5).Value = TextBox1.Value

Private Sub OKButton_Click()
    If ComboBox1.Value = "" Then
        MsgBox "Please Select a member of staff", , "Error"
        Exit Sub
    End If
    If CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False And CheckBox5.Value = False Then
        'do nothing
    Else
        Dim Counter As Integer, EmployeeName As String, LastRow As Long
        Call SmoothCodeStart
        EmployeeName = ComboBox1.Value
        With Sheets("Timetable").ListObjects("TblTimetable")
        LastRow = .range.Rows.Count
            For Counter = LastRow To 1 Step -1
            If .DataBodyRange.Cells(Counter, .ListColumns("Name and Surname").Index) = EmployeeName Then
                .Cells(LastRow, 4).Value = TextBox1.Value
                .Cells(LastRow, 5).Value = TextBox2.Value
                .Cells(LastRow, 6).Value = TextBox3.Value
                .Cells(LastRow, 7).Value = TextBox4.Value
                .Cells(LastRow, 8).Value = TextBox5.Value
            End If
            Next Counter
        End With
    End If
    Call SmoothCodeEnd
    Unload Me
End Sub

好吧,我从头开始,这段代码找到了正确的行并将其删除,现在我不是要删除它,而是希望它将文本框1:5的值添加到4:8列

Private Sub OKButton_Click()
    If Me.ComboBox1.Value = "" Then
        MsgBox "Please Select a member of staff", , "Error"
        Exit Sub
    End If
    If CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False And CheckBox5.Value = False Then
        'do nothing
    Else
        Dim Counter As Integer, EmployeeName As String, LastRow As Long

        EmployeeName = ComboBox1.Value
        With Sheets("Timetable").ListObjects("TblTimetable")
            LastRow = .range.Rows.Count
            For Counter = LastRow To 1 Step -1
            If .DataBodyRange.Cells(Counter, .ListColumns("Name and Surname").Index) = EmployeeName Then
                .ListRows(Counter).Delete
            End If
            Next Counter
        End With
    End If
    Unload Me
End Sub

我尝试了以下方法:

.Cells(LastRow, 4).Value = TextBox1.Value

改用它:

 .Cells(Counter, 4).Value = TextBox1.Value

我已经尝试过了:

With .ListRows(Counter)
   .Columns(, 4).Value = TextBox1.Value
End With

新的工作代码非常感谢@tim williams的帮助,它可以工作,但是在关闭用户窗体之前,我按ok按钮后有3秒钟的暂停,任何人都对为什么有任何想法?

Private Sub OKButton_Click()
    If Me.ComboBox1.Value = "" Then
        MsgBox "Please Select a member of staff", , "Error"
        Exit Sub
    End If
    If CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False And CheckBox5.Value = False Then
        'do nothing
    Else
        Dim EmployeeName As String, f As range, tbl As ListObject, Counter As Integer, LastRow As Long, listcolumns As range
        EmployeeName = ComboBox1.Value
        With Sheets("Timetable").ListObjects("TblTimetable")
            LastRow = .range.Rows.Count
            For Counter = LastRow To 1 Step -1
            Set f = .DataBodyRange.Cells(Counter, .listcolumns("Name and Surname").Index)
            If f = EmployeeName Then
                With f.EntireRow
                    .Cells(5).Value = TextBox1.Value
                    .Cells(6).Value = TextBox2.Value
                    .Cells(7).Value = TextBox3.Value
                    .Cells(8).Value = TextBox4.Value
                    .Cells(9).Value = TextBox5.Value
                End With
            End If
            Next Counter
        End With
    End If
    Unload Me
End Sub

2 个答案:

答案 0 :(得分:0)

以下是您可以使用的方法的概述:

    Dim EmployeeName As String, f As Range, tbl As ListObject

    EmployeeName = "Joe Brown"

    Set tbl = Sheets("Timetable").ListObjects("TblTimetable")

    'find the row
    Set f = tbl.ListColumns("Name and Surname").DataBodyRange.Find( _
                                    what:=EmployeeName, lookat:=xlWhole)

    'if found a row, update it
    If Not f Is Nothing Then
        With f.EntireRow
            .Cells(4).Value = "Value_1"
            .Cells(5).Value = "Value_2"
            'etc etc
        End With
    End If

答案 1 :(得分:0)

感谢@tim williams帮助我实现此功能

Private Sub OKButton_Click()
    If Me.ComboBox1.Value = "" Then
        MsgBox "Please Select a member of staff", , "Error"
        Exit Sub
    End If
    If CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False And CheckBox4.Value = False And CheckBox5.Value = False Then
        'do nothing
    Else
        Dim EmployeeName As String, f As range, tbl As ListObject, Counter As Integer, LastRow As Long, listcolumns As range
        EmployeeName = ComboBox1.Value
        With Sheets("Timetable").ListObjects("TblTimetable")
            LastRow = .range.Rows.Count
            For Counter = LastRow To 1 Step -1
            Set f = .DataBodyRange.Cells(Counter, .listcolumns("Name and Surname").Index)
            If f = EmployeeName Then
                With f.EntireRow
                    .Cells(5).Value = TextBox1.Value
                    .Cells(6).Value = TextBox2.Value
                    .Cells(7).Value = TextBox3.Value
                    .Cells(8).Value = TextBox4.Value
                    .Cells(9).Value = TextBox5.Value
                End With
            End If
            Next Counter
        End With
    End If
    Unload Me
End Sub