gpt4 book ai didi

vba - 使用用户表单 'delete' 按钮删除多个工作表上的行

转载 作者:行者123 更新时间:2023-12-04 20:30:08 25 4
gpt4 key购买 nike

我修改了 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

但得到一个

'Next without For' error.



我不明白为什么会发生这种情况,因为我认为这就是我在本节中所做的:
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

任何建议将不胜感激。

(我希望一旦我解决了删除问题,我将能够修改添加新学生时添加行的解决方案!)

最佳答案

添加 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 可能是一个更好的主意。 ,因为它仅循环工作表并避免图表(如果可用)。

关于vba - 使用用户表单 'delete' 按钮删除多个工作表上的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52273660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com