Private Sub cmdsub_Click()
UnProtect
'Copy input values to sheet.
Dim lrow As Long
Dim ws As Worksheet
Dim Lvalue As Date
Lvalue = Now
If Me.cbox4.Value = "pending" Then
Set ws = ThisWorkbook.Sheets("Pending")
Else
Set ws = ThisWorkbook.Sheets("Completed")
End If
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
If Me.txt13.Value = "" Then
Me.txt13.SetFocus
MsgBox "'Mandatory", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.cbox1.Value = "" Then
Me.cbox1.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.cbox2.Value = "" Then
Me.cbox2.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.cbox2.Value = "" Then
Me.cbox2.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt1.Value = "" Then
Me.txt1.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt2.Value = "" Then
Me.txt2.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt3.Value = "" Then
Me.txt3.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt4.Value = "" Then
Me.txt4.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt5.Value = "" Then
Me.txt5.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt6.Value = "" Then
Me.txt6.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt7.Value = "" Then
Me.txt7.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.cbox4.Value = "" Then
Me.cbox4.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
End If
With ws
.Cells(lrow, 1).Value = Me.lbl1.Caption
.Cells(lrow, 2).Value = Me.txt13.Value
.Cells(lrow, 3).Value = cbox1.Value
.Cells(lrow, 4).Value = cbox2.Value
.Cells(lrow, 5).Value = cbox3.Value
.Cells(lrow, 6).Value = Me.txt1.Value
.Cells(lrow, 7).Value = Me.txt2.Value
.Cells(lrow, 8).Value = Me.txt3.Value
.Cells(lrow, 9).Value = Me.txt4.Value
.Cells(lrow, 10).Value = Me.txt5.Value
.Cells(lrow, 11).Value = Me.txt6.Value
.Cells(lrow, 12).Value = Me.txt7.Value
.Cells(lrow, 13).Value = cbox4.Value
.Cells(lrow, 23).Value = Me.txt15.Value
.Cells(lrow, 24).Value = "Unknown"
End With
'Clear input controls.
Me.lbl1.Caption = Now()
cbox1.Value = ""
cbox2.Value = ""
cbox3.Value = ""
Me.txt1.Value = ""
Me.txt2.Value = ""
Me.txt3.Value = ""
Me.txt4.Value = ""
Me.txt5.Value = ""
Me.txt6.Value = ""
Me.txt7.Value = ""
Me.txt13.Value = ""
Me.txt15.Value = ""
cbox4.Value = ""
Cmb1.Value = ""
MoveAndDelete
cbox1.Visible = False
cbox2.Visible = False
cbox3.Visible = False
txt1.Visible = False
txt2.Visible = False
txt3.Visible = False
txt4.Visible = False
txt5.Visible = False
txt6.Visible = False
txt7.Visible = False
cbox4.Visible = False
txt13.Visible = False
Label1.Visible = False
Label2.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label8.Visible = False
Label9.Visible = False
Label10.Visible = False
Label11.Visible = False
Label12.Visible = False
txt15.Visible = False
Label21.Visible = False
cmdsub.Visible = False
cmdnxt.Visible = False
abc
bcd
Protect
Application.DisplayAlerts = False
End Sub
Sub UnProtect()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
wSheet.UnProtect Password:="Passworld"
Next wSheet
End Sub
Sub Protect()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
wSheet.Protect Password:="Passworld"
Next wSheet
End Sub
当我运行上面的代码时,它给了我一个错误:
运行时错误9.下标超出范围。
我无法调试它。我尝试了几种可能但没有用。此外,我有这个保护和取消保护表,当我使用这个宏时,我还有其他工作表也会受到保护。
答案 0 :(得分:0)
检查lrow值设置(ws.Cells(Rows.Count,1).End(xlUp).Offset(1,0).Row)偏移功能中的0可能导致您的问题,将其删除不是必需的。