2014-12-02 59 views
0

我有一個用於員工數據錄入工作的excel表單。 Excel表格包含員工的詳細信息。 我希望表單可以通過上一個和下一個按鈕瀏覽每個記錄,並在表單中顯示內容。使用上一個和下一個按鈕的行導航excel表格vba

我已經爲此寫了代碼,但它不能正常工作。有時會在記錄的開始和結束時提供無效的記錄詳細信息。幫助我

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

我建議你使用子程序獲取和放置數據,然後使用導航按鈕來改變行。測試你的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 
相關問題