繁体   English   中英

下标超出范围,工作表得到保护

[英]Subscript out of Range and Sheet gets protected

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.下标超出范围。

我无法调试它。 我尝试了几种可能性,但没有用。 另外,我有这个保护和取消保护工作表,当我使用此宏时也会发生其他情况,这也会得到保护。

检查借位值设置(ws.Cells(Rows.Count,1).End(xlUp).Offset(1,0).Row)offset函数中的0可能会导致您的问题,请删除它,因为它不是必需的。

暂无
暂无

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

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