更新按钮无法正常运行它只是添加了包含数据的新行而不是更新
Dim currentrow As Long
Private Sub cmdFindNext_Click()
Dim lastrow
Dim evtname As String
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
evtname = TB1.Text
For currentrow = 4 To lastrow
If Cells(currentrow, 1).Text = evtname Then
TB1.Text = Cells(currentrow, 1).Text
ComboB.Text = Cells(currentrow, 4)
startdate.Text = Cells(currentrow, 2)
enddate.Text = Cells(currentrow, 3)
End If
Next currentrow
TB1.SetFocus
End Sub
Private Sub cmdFindPrevious_Click()
Dim lastrow
Dim evtname As String
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
evtname = TB1.Text
For currentrow = lastrow To 4 Step -1
If Cells(currentrow, 1).Text = evtname Then
TB1.Text = Cells(currentrow, 1).Text
ComboB.Text = Cells(currentrow, 4)
startdate.Text = Cells(currentrow, 2)
enddate.Text = Cells(currentrow, 3)
End If
Next currentrow
TB1.SetFocus
End Sub
Private Sub cmdUpdate_Click()
Cells(currentrow - 1, 1).Resize(1, 4).Value = Array(TB1.Text, startdate.Text, enddate.Text, ComboB.Text)
End Sub
我可以对该更新做些什么我添加了文本框,使日期控件变得简单但仍然相同
答案 0 :(得分:1)
试试这个子
Private Sub cmdUpdate_Click()
Dim evtname As String, sname As String, sdate As String, edate As String
evtname = TB1.Text
Cells(currentrow - 1, 1).Value = evtname
sname = ComboB.Text
Cells(currentrow - 1, 4).Value = sname
sdate = startdate.Text
Cells(currentrow - 1, 2).Value = sdate
edate = enddate.Text
Cells(currentrow - 1, 3).Value = edate
End Sub
答案 1 :(得分:0)
试试这个 - 不需要为这个子范围分配变量,你可以用一行代码完成整个过程:
Private Sub cmdUpdate_Click()
Cells(currentrow - 1, 1).Resize(1, 4).Value = Array(TB1.Text, startdate.Text, enddate.Text, ComboB.Text)
End Sub
要获得正确的行而不是最后一行:
您需要在找到条件后退出循环,以便currentrow
保留正确的值。在两个查找子中,退出If
块末尾的循环:
'// existing code here
Exit For '<~~ add this in both subs.
End If
完成后,将currentrow - 1
更改回currentrow
以确保它是正确的行。