我似乎无法弄清楚为什么这会引发错误,而且只是第一次通过。当为每个用户创建用户登录费用和收入表时,登录用户也可以访问。它分别从Master Expense Page和Master Income Page复制。在注销时,调用sub来删除这些添加的页面,可以在下面找到:
注意: onclick 注销程序调用一个,而不是另一个。
Public Sub removeExpensePages()
Set wb = ThisWorkbook
For Each ws In wb.Worksheets
On Error Resume Next
If ws.Name Like "*" & "Expense Page" & "*" Then
If ws.Name <> "Master Expense Page" Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
End If
On Error GoTo 0
Next
End Sub
Public Sub removeIncomePages()
Set wb = ThisWorkbook
For Each ws In wb.Worksheets
On Error Resume Next
If ws.Name Like "*" & "Income Page" & "*" Then
If ws.Name <> "Master Income Page" Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
End If
On Error GoTo 0
Next
End Sub
答案 0 :(得分:1)
您的代码似乎对我有用。但是,既然我已经对它进行了测试并做了一些改进,我会发布它们:
Option Explicit
是必不可少的,我立即注意到wb
和ws
变量未声明wb
变量并非真的必要,只需使用ThisWorkbook
On Error Resume Next
只是隐藏了这些问题,它无法帮助您解决问题。Option Explicit
Public Sub removeAllPages()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "*Expense Page*" Or ws.Name Like "*Income Page*" Then
If ws.Name <> "Master Expense Page" And ws.Name <> "Master Income Page" Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
End If
Next ws
End Sub
在:
后:
答案 1 :(得分:0)
将工作表的索引从最后一个循环到第一个
此外,您可以使用单个子接受“主”表格名称
最后交换两张支票是最好的逻辑
Option Explicit
Public Sub RemovePages(masterName As String)
Dim iWs As Long
Dim wsName As String
With ThisWorkbook
For iWs = ThisWorkbook.Sheets.Count to 1 Step -1
With .Sheets(iWs)
If .Name <> masterName Then
If .Name Like "*" & masterName & "*" Then
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
End If
End If
End With
Next
End With
End Sub
这样你的onclick sub可以通过两个“Master”名称来调用它
Sub onclick()
RemovePages "Expense Page"
RemovePages "Master Income Page"
End Sub
答案 2 :(得分:0)
所以,我不确定解决方案是什么,但它与主表单上的代码中的某些内容有关。也许正在引用的对象在ws.delete上不再可用?我已经重新处理了所有代码,并且不再收到运行时错误。感谢所有帮助过我的人!!!