Извините, я редактировал это так много раз, что это сбивает с толку, поэтому я просто начну заново.я написал 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