我有一个表单,目前允许从VBA中的表单输入到我的Excel电子表格中。当我使用Previous按钮或下一个按钮时,它会删除从先前输入到表单上的内容的所有内容。有没有办法点击上一个,看到输入的上一个数据,允许编辑但是当你再次选择上一个按钮时改为字段?
Public nCurrentRow As Long
Private Sub Next_Command_Click()
Do
nCurrentRow = nCurrentRow + 1
TraverseData (nCurrentRow)
Loop Until C_C_L.Cells(nCurrentRow, 1).Value = "" Or C_C_L.Cells(nCurrentRow, 1).Value = Me.PI_Text.Value
End Sub
Private Sub Previous_Command_Click()
Do
nCurrentRow = nCurrentRow - 1
TraverseData (nCurrentRow)
Loop Until nCurrentRow = 1 Or C_C_L.Cells(nCurrentRow, 1).Value = Me.PI_Text.Value
End Sub
还有办法跳过前3行(标题),以便新数据不会覆盖我的标题吗?
答案 0 :(得分:0)
您应该添加另一个按钮AddRecord_Command
,它会将当前行的设置带到下一个空行。这样,Previous_Command
将移回一条记录,Next_Command
将不会移动到最后一条记录。
Option Explicit
Private NC_C_L As Worksheet
Private nCurrentRow As Long
Private firstRow As Long
Private Sub UserForm_Initialize()
Set NC_C_L = Worksheets("Sheet1")
firstRow = 4
ReadData
End Sub
Private Sub AddRecord_Command_Click()
Dim lastRow As Long
lastRow = NC_C_L.Range("A" & NC_C_L.Rows.Count).End(xlUp).Row
WriteData
nCurrentRow = lastRow + 1
End Sub
Private Sub Next_Command_Click()
Dim lastRow As Long
lastRow = NC_C_L.Range("A" & NC_C_L.Rows.Count).End(xlUp).Row
If nCurrentRow < lastRow Then
WriteData
nCurrentRow = nCurrentRow + 1
ReadData
End If
End Sub
Private Sub Previous_Command_Click()
If nCurrentRow > firstRow Then
WriteData
nCurrentRow = nCurrentRow - 1
ReadData
End If
End Sub
Private Sub ReadData()
With NC_C_L
Me.A_Text.Value = .Cells(nCurrentRow, 1)
Me.B_Box = .Cells(nCurrentRow, 2)
Me.C_Combo.Value = .Cells(nCurrentRow, 3)
Me.C_Combo.Value = .Cells(nCurrentRow, 4)
Me.F_Combo.Value = .Cells(nCurrentRow, 5)
Me.H_Combo.Value = .Cells(nCurrentRow, 6)
Me.I_Combo.Value = .Cells(nCurrentRow, 7)
Me.J_Text.Value = .Cells(nCurrentRow, 8)
Me.K_Text.Value = .Cells(nCurrentRow, 9)
Me.Comments1_Text.Value = .Cells(nCurrentRow, 10)
Me.Comments2_Text.Value = .Cells(nCurrentRow, 11)
Me.Comments3_Text.Value = .Cells(nCurrentRow, 12)
Me.PhoneNumber_Text.Value = .Cells(nCurrentRow, 13)
Me.Address1_Text.Value = .Cells(nCurrentRow, 14)
Me.Address2_Text.Value = .Cells(nCurrentRow, 15)
Me.City_Text.Value = .Cells(nCurrentRow, 16)
Me.State_Combo.Value = .Cells(nCurrentRow, 17)
Me.Zip_Text.Value = .Cells(nCurrentRow, 18)
Me.EMail_Text.Value = .Cells(nCurrentRow, 19)
Me.P_Name_Text.Value = .Cells(nCurrentRow, 20)
Me.P_PhoneNumber_Text.Value = .Cells(nCurrentRow, 21)
Me.P_Address_Text.Value = .Cells(nCurrentRow, 22)
End With
End Sub
Private Sub WriteData()
With NC_C_L
.Cells(nCurrentRow, 1) = Me.A_Text.Value
.Cells(nCurrentRow, 2) = Me.B_Box
.Cells(nCurrentRow, 3) = Me.C_Combo.Value
.Cells(nCurrentRow, 4) = Me.C_Combo.Value
.Cells(nCurrentRow, 5) = Me.F_Combo.Value
.Cells(nCurrentRow, 6) = Me.H_Combo.Value
.Cells(nCurrentRow, 7) = Me.I_Combo.Value
.Cells(nCurrentRow, 8) = Me.J_Text.Value
.Cells(nCurrentRow, 9) = Me.K_Text.Value
.Cells(nCurrentRow, 10) = Me.Comments1_Text.Value
.Cells(nCurrentRow, 11) = Me.Comments2_Text.Value
.Cells(nCurrentRow, 12) = Me.Comments3_Text.Value
.Cells(nCurrentRow, 13) = Me.PhoneNumber_Text.Value
.Cells(nCurrentRow, 14) = Me.Address1_Text.Value
.Cells(nCurrentRow, 15) = Me.Address2_Text.Value
.Cells(nCurrentRow, 16) = Me.City_Text.Value
.Cells(nCurrentRow, 17) = Me.State_Combo.Value
.Cells(nCurrentRow, 18) = Me.Zip_Text.Value
.Cells(nCurrentRow, 19) = Me.EMail_Text.Value
.Cells(nCurrentRow, 20) = Me.P_Name_Text.Value
.Cells(nCurrentRow, 21) = Me.P_PhoneNumber_Text.Value
.Cells(nCurrentRow, 22) = Me.P_Address_Text.Value
End With
End Sub