在我发布的代码中,我使用了一个名为" ACDS Test"的复选框。并且无论何时检查它都会创建一个工作表,然后当它被取消选中时,它会调用upper函数并删除工作表。
我正在尝试添加一个消息框,它基本上像故障安全一样工作,以确保他们想要删除页面。如果他们说他们不想删除页面,那么我希望复选框保持检查状态。
出于某种原因,当我尝试传递该值以确保复选框保持选中状态并且无法弄清楚原因时,我收到此错误消息。
错误出现在线上: Sub ACDSTest_Click(CorrectValue As Integer) 具体错误是:"编译错误:程序声明与具有相同名称的事件或程序的描述不匹配"。
非常感谢任何帮助!如果需要进一步澄清,请随时询问!
Sub DeleteWorksheet(NameSheet As String)
Dim Ans As Long
Dim t As String
Dim CorrectValue As Integer
Dim i As Long, k As Long
k = Sheets.Count
Ans = MsgBox("Would you like to take this test off of the form?", vbYesNo)
Select Case Ans
Case vbYes
'Code reads through each page and finds one with corresponding name to string t
'Once it finds the correct page, it deletes it
For i = k To 1 Step -1
t = Sheets(i).Name
If t = NameSheet Then
Sheets(i).Delete
End If
Next i
CorrectValue = 0
Case vbNo
CorrectValue = 1
End Select
End Sub
Sub ACDSTest_Click(CorrectValue As Integer)
Dim NameSheet As String
Dim NameValue As String
NameSheet = "ACDS"
NameValue = "ACDS Test"
If ACDSTest.Value = True Then
CreateWorksheet (NameSheet), (NameValue)
Worksheets("Sheet1").Activate
Else
DeleteWorksheet (NameSheet)
If CorrectValue = 1 Then
ActiveSheet.Shapes("ACDS Test").ControlFormat.Value = 1
End If
End If
End Sub
答案 0 :(得分:0)
这里的问题是,CorrectValue
中定义的DeleteWorksheet
变量在上下文中不存在
变量在ACDSTest_Click
子例程的上下文中不存在。这是因为子程序或函数中定义的变量是这些函数的本地变量。要更正此问题,我会将DeleteWorksheet
转换为如下所示的函数。
此外,触发Private Sub ACDSTest_Click()
的事件无法处理将值传递给该函数,因此将其更改为Sub ACDSTest_Click(CorrectValue As Integer)
会导致错误。
Function DeleteWorksheet(ByVal SheetName As String) As Boolean
On Error GoTo SheetDNE
SheetName = Sheets(SheetName).Name 'Check if sheet exists w/o other objects
On Error GoTo 0
Select Case MsgBox("Would you like to take this test off of the form?", vbYesNo)
Case vbYes
Application.DisplayAlerts = False
Sheets(SheetName).Delete
Application.DisplayAlerts = True
DeleteWorksheet = True
Case Else: DeleteWorksheet = False
End Select
Exit Function 'Exit The Function w/o error
SheetDNE: 'Sheet Does Not Exist
MsgBox "The indicated sheet, " & SheetName & ", does not exist", vbOKOnly
End Function
并且
Private Sub ACDSTest_Click()
Dim NameSheet As String
Dim NameValue As String
NameSheet = "ACDS"
NameValue = "ACDS Test"
If ACDSTest.Value = True Then
CreateWorksheet (NameSheet), (NameValue)
Worksheets("Sheet1").Activate
Else
If Not DeleteWorksheet(NameSheet) Then _
ActiveSheet.Shapes("ACDS Test").ControlFormat.Value = 1
End If
End Sub