我已经修改了Roy Cox编写的代码(感谢您为我节省了很多时间!),以创建一个用户表单,以便在我正在创建的分析工具中添加,修改和删除用户的详细信息。
在单个工作表上处理用户数据时,它可以完美地工作。我想修改代码,以便在添加或删除用户时,它检查每个工作表并相应地修改添加或删除行。
这是用于删除单张纸上的瞳孔数据的代码:
Private Sub cmbDelete_Click()
Dim msgResponse As String 'confirm delete
Application.ScreenUpdating = False
'get user confirmation
msgResponse = MsgBox("This will delete the selected record. Continue?", _
vbCritical + vbYesNo, "Delete Entry")
Select Case msgResponse 'action dependent on response
Case vbYes
'c has been selected by Find button on UserForm
Set c = ActiveCell
c.EntireRow.Delete 'remove entry by deleting row
'restore form settings
With Me
.cmbAmend.Enabled = False 'prevent accidental use
.cmbDelete.Enabled = False 'prevent accidental use
.cmbAdd.Enabled = True 'restore use
'clear form
Call ClearControls
End With
Case vbNo
Exit Sub 'cancelled
End Select
Application.ScreenUpdating = True
End Sub
我试图修改它,以删除每个工作表上的用户数据,如下所示:
Private Sub cmbDelete_Click()
Dim Sh As Worksheet
Dim msgResponse As String 'confirm delete
Application.ScreenUpdating = False
'get user confirmation
msgResponse = MsgBox("This will delete the selected record. Continue?", _
vbCritical + vbYesNo, "Delete Entry")
Select Case msgResponse 'action dependent on response
Case vbYes
For Each Sh In ThisWorkbook.Sheets
With Sh.UsedRange
'c has been selected by Find button
Set c = ActiveCell
c.EntireRow.Delete 'remove entry by deleting row
Next
'restore form settings
With Me
.cmbAmend.Enabled = False 'prevent accidental use
.cmbDelete.Enabled = False 'prevent accidental use
.cmbAdd.Enabled = True 'restore use
'clear form
Call ClearControls
End With
Case vbNo
Exit Sub 'cancelled
End Select
Application.ScreenUpdating = True
End Sub
但得到
“下一个没有For”错误。
我不明白为什么会这样,因为我认为这是我在本节中所做的:
For Each Sh In ThisWorkbook.Sheets
With Sh.UsedRange
'c has been selected by Find button
Set c = ActiveCell
c.EntireRow.Delete 'remove entry by deleting row
Next
任何建议将不胜感激。
(我希望一旦解决了删除问题,我将能够修改解决方案以在添加新瞳孔时添加行!)
答案 0 :(得分:1)
添加End With
:
For Each Sh In ThisWorkbook.Sheets
With Sh.UsedRange
'c has been selected by Find button
Set c = ActiveCell
c.EntireRow.Delete 'remove entry by deleting row
End With
Next
在Next
之前。
作为最佳实践,使用ThisWorkbook.Worksheets
可能是一个更好的主意,因为它仅循环工作表并避免使用图表(如果有)。