简体   繁体   English

使用上一个和下一个按钮Excel Form VBA进行行导航

[英]Row navigation using previous and next button excel form vba

I have an excel form for employee data entry work. 我有一个用于员工数据输入工作的Excel表格。 The excel sheet contains details of employees. Excel工作表包含员工的详细信息。 I want the form to navigate through each record through previous and next button, also displaying the content in the form. 我希望表单通过上一个和下一个按钮浏览每个记录,并在表单中显示内容。

I already written the code for this but it is not working properly. 我已经为此编写了代码,但是无法正常工作。 It sometimes gives invalid record details while reaching begin and end of the records. 有时在到达记录的开始和结束时会给出无效的记录详细信息。 Help me 帮我

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

一旦控制权到达文件的末尾/开始,您可以禁用相应的按钮。

I suggest you use subroutines to Get and Put data, and then use your navigation buttons to change the row. 建议您使用子例程获取和放置数据,然后使用导航按钮更改行。 Test your GetData and PutData routines to make sure they do what is expected of them. 测试您的GetData和PutData例程,以确保它们执行了预期的操作。

Here is an example from a project of mine using similar controls with your textboxes and cell references. 这是我的一个项目的示例,该项目使用带有文本框和单元格引用的类似控件。 In my example, I started the data on Row 4.. Most of the variables are declared upon activation of the form. 在我的示例中,我从第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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM