微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关于错误'1004'的任何建议:范围类的删除方法失败?简单的删除按钮不起作用

如何解决关于错误'1004'的任何建议:范围类的删除方法失败?简单的删除按钮不起作用

问题:我有一个删除按钮,可以在一种表单上使用,但是当更改相同的代码以反映不同的工作表时,会引发错误“范围类的删除方法失败”

在遵循了在线教程之后,我设法创建了一个功能正常的表单。因此,我决定要在同一工作簿的新工作表上复制类似的表格。

在这种情况下,我创建了一个新模块并复制了代码,并更改了代码以反映工作表的名称。“ paptops”。

我复制并更改了用于遍历列表框条目的函数名称,最初在原始模块上为“ Selected_List”,为此表创建了一个新模块,并将其称为“ Lap_List”函数

奇怪的是,我的“编辑”按钮工作正常。

以下是用于尝试删除代码

Private Sub Btn_Delete_Click()

''''' If no Option Selected displays a Message '''

 If Lap_List = 0 Then

      MsgBox "No row is selected.",vbOKOnly + vbinformation,"Delete"
      Exit Sub
 End If

  '''' If option is Selected peresnt Option Box '''

 Dim imsg As VbMsgBoxResult

imsg = MsgBox("Do you want to delete the selected record?",vbYesNo + vbQuestion,"Confirmation")

If imsg = vbNo Then Exit Sub

   ''''' If yes then delete the selected row from ListBox and cells on sheet ''''

ThisWorkbook.Sheets("Laptops").Rows(Lap_List + 1).Delete '''' Error 1004 but worked fine on another form''''
    
Call Reset

MsgBox "Selected record has been deleted.","Deleted"



End Sub

以及用于引用的功能

Function Lap_List() As Long

 Dim iRow As Long

 Lap_List = 0

   For iRow = 0 To Frm_Laptops.Lstbx_Laptops.ListCount - 1
    
   If Frm_Laptops.Lstbx_Laptops.Selected(iRow) = True Then
    
    Lap_List = iRow + 1
    Exit For

    End If
    
    Next iRow


End Function

我真的很坚持这一点,当我尝试将删除代码更改为range.end(xlup)等的变体时,它也遇到了同样的问题。

有什么建议吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。