我有一份excel表格,用于员工数据录入工作。 excel表包含员工的详细信息。 我希望表单通过上一个和下一个按钮浏览每个记录,同时在表单中显示内容。
我已经为此编写了代码,但它无法正常工作。它有时会在到达记录的开头和结尾时提供无效的记录详细信息。帮帮我
Private Sub UserForm_Initialize()
frmEmpDetails.ComboGender.AddItem "Male", 0
frmEmpDetails.ComboGender.AddItem "Female", 1
counter = ActiveSheet.UsedRange.Rows.Count
temp_counter = counter
lblTmpCount.Caption = temp_counter
End Sub
Private Sub btnNext_Click()
status = 1
lblTmpCount.Caption = temp_counter
If (temp_counter >= counter) Then
MsgBox "Reached end"
Else
temp_counter = temp_counter + 1
txtID.Text = Cells(temp_counter, 1).Value
txtName.Text = Cells(temp_counter, 2).Value
txtDOB.Text = Cells(temp_counter, 3).Value
ComboGender.Value = Cells(temp_counter, 4).Value
txtAboutEmp.Text = Cells(temp_counter, 5).Value
lblTmpCount.Caption = temp_counter
End If
End Sub
Private Sub btnPrev_Click()
status = 1
lblTmpCount.Caption = temp_counter
If (temp_counter < 2) Then
MsgBox "Reached beginning"
Else
txtID.Text = Cells(temp_counter, 1).Value
txtName.Text = Cells(temp_counter, 2).Value
txtDOB.Text = Cells(temp_counter, 3).Value
ComboGender.Value = Cells(temp_counter, 4).Value
txtAboutEmp.Text = Cells(temp_counter, 5).Value
temp_counter = temp_counter - 1
End If
End Sub
答案 0 :(得分:0)
一旦控件到达文件的结尾/开头,您就可以禁用相应的按钮。
答案 1 :(得分:0)
我建议您使用子程序来获取和放置数据,然后使用导航按钮更改行。测试你的GetData和PutData例程,以确保它们完成对它们的期望。
以下是我的一个项目中使用类似控件和文本框和单元格引用的示例。在我的例子中,我在第4行开始了数据。大多数变量是在激活表单时声明的。
Private Sub cmdFirst_Click()
R = 4
Call GetData
End Sub
Private Sub cmdPrev_Click()
If R = 4 Then
MsgBox ("Already on First Record")
Else
R = R - 1
Call GetData
End If
End Sub
Private Sub cmdNext_Click()
lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).row
If R = lastRow Then
MsgBox ("Already on the last record.")
Else
R = R + 1
Call GetData
End If
End Sub
Private Sub cmdLast_Click()
R = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).row
Call GetData
End Sub
Private Sub cmdNew_Click()
R = (Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).row) + 1
Call SetID
Call ClearData
txtID = newID
End Sub
Private Function SetID()
Dim rng As Range
Set rng = Sheets("Sheet1").Range("a4", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).row
newID = Application.WorksheetFunction.Max(rng) + 1
End Function
Private Sub GetData()
txtID.Text = Sheets("Sheet1").Cells(R, 1).Value
txtName.Text = Sheets("Sheet1").Cells(R, 2).Value
txtDOB.Text = Sheets("Sheet1").Cells(R, 3).Value
ComboGender.Value = Sheets("Sheet1").Cells(R, 4).Value
txtAboutEmp.Text = Sheets("Sheet1").Cells(R, 5).Value
End Sub
Private Sub ClearData()
txtName.Text = ""
txtDOB.Text = ""
ComboGender.Value = ""
txtAboutEmp.Text = ""
End Sub
Private Sub PutData()
Sheets("Sheet1").Cells(R, 1).Value = txtID.Text
Sheets("Sheet1").Cells(R, 2).Value = txtName.Text
Sheets("Sheet1").Cells(R, 3).Value = txtDOB.Text
Sheets("Sheet1").Cells(R, 4).Value = ComboGender.Value
Sheets("Sheet1").Cells(R, 5).Value = txtAboutEmp.Text
End Sub