Поиск строки в таблице и вставка значений из пользовательской формы - PullRequest
0 голосов
/ 21 сентября 2019

Извините, я редактировал это так много раз, что это сбивает с толку, поэтому я просто начну заново.я написал 2 разные функции, обе работают, и я попытался объединить 2, чтобы получить этот функционал, но у меня возникли проблемы.я хочу найти строку с именем совпадения, а затем вставить значения в 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 за это, он работает, но после нажатия кнопки «ОК» перед закрытием пользовательской формы появляется 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 голосов
/ 21 сентября 2019

Спасибо @tim Уильямс за помощь в получении этого функционала

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
0 голосов
/ 21 сентября 2019

Вот схема подхода, который вы можете использовать:

    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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...