获取运行时错误13单击消息框上的“取消”时,会键入mistmatch错误。
我尝试制作以下脚本来处理消息框是否为空,但是在进行错误检查时,单击消息框上的“取消”会将其全部删除。
有什么想法吗?
Private Sub ChangeDebtAmounts_Click()
Dim Debt1 As Integer, Debt2 As Integer, Debt3 As Integer, Debt4 As Integer
Dim D1Range As String, D2Range As String, D3Range As String, D4Range As String
D1Range = ActiveSheet.Range("Y15")
D2Range = ActiveSheet.Range("Y16")
D3Range = ActiveSheet.Range("Y17")
D4Range = ActiveSheet.Range("Y18")
Debt1 = InputBox("Please Enter in the account limit for " & D1Range)
If Debt1 = "" Then
MsgBox ("Setting " & D1Range & " to Zero, No Value Entered")
Else
Range("AA15").Value = Debt1 - Range("S58")
End If
End
End Sub
答案 0 :(得分:4)
类型不匹配的是InputBox
,而不是MsgBox
。要解决此问题,只需将Dim Debt1 As Integer
更改为Dim Debt1 As Variant
。另外,您将MsgBox
用作子元素而不是函数,因此正确的语法应为
MsgBox "Setting " & D1Range & " to Zero, No Value Entered"
而不是
MsgBox ("Setting " & D1Range & " to Zero, No Value Entered")
在这种情况下,括号是没有害处的,但是如果您在将MsgBox
用作子元素时尝试给其附加参数,则会出现语法错误。
答案 1 :(得分:0)
这是您的问题略有不同的看法。在代码中查看我的评论。
(由于注释的缘故,时间更长;可选地,您可以删除任何注释行以及任何其他空白行。)
Private Sub ChangeDebtAmounts_Click()
Dim Debt1, Debt2, Debt3, Debt4 'data type "Variant" is assumed
Dim D1Range As String,D2Range As String,D3Range As String,D4Range As String
'by using a "With" statement, you can use "." instead of "ActiveSheet."
With ActiveSheet
D1Range = .Range("Y15")
D2Range = .Range("Y16")
D3Range = .Range("Y17")
D4Range = .Range("Y18")
'I added a title to the dialog and a default value of zero
Debt1=InputBox("Enter the account limit for " & D1Range, "Limit?" ,0)
'Check user response:
If Debt1 = "" Or Debt1 = 0 Then
'User clicked cancel or entered zero.
MsgBox "Setting " & D1Range & " to Zero, No Value Entered"
'I assume your next step is to set the input value to zero:
Debt1 = 0
Else
'you don't need to specify ".Value" in most cases (it's assumed)
'also:by using the "." we're referring to ActiveSheet again.
.Range("AA15") = Debt1 - .Range("S58")
End If
End With '(the end of "With ActiveSheet")
End Sub
其他一些想法:
似乎您将为每个InputBox
使用不同的变量,但这不是必需的:在这种情况下,您可以重用相同的变量,而不会出现问题。
ActiveSheet
仅指“运行代码时碰巧打开的任何工作表(选项卡)”。最好明确引用一个特定的工作表,以防止将来出现潜在的问题。
例如,如果您的单元格(如Y15
位于工作表Sheet1
上,则可以将ActiveSheet
替换为Sheets("Sheet1")
。
这些方法仅用于演示-如果您已经弄清楚了解决方案,坚持下去,那就没有浪费时间了!这些只是为了说明做同一件事的其他方法。
这很有趣,这是另一种替代方法,它遍历所有4个单元格Y15:Y18
并重复相同的MsgBox
。
我不确定用户输入的其他3个值会发生什么,所以我将这些留为空白。
Private Sub demo_Alternate()
Dim userInput As Variant, arr As Variant, myCell
With Sheets("Sheet1") '<<<<<< change this to actual worksheet name
arr = .Range("Y15:Y18") ' arr(1) to arr(4) are now cell references
For Each myCell In arr
userInput = InputBox("Enter account limit for " & myCell, "Limit?", 0)
If userInput = "" Or userInput = 0 Then 'Cancelled or 0 entered
MsgBox "Setting " & myCell & " to Zero, No Value Entered"
userInput = 0
Else
Select Case Split(myCell.Address, "$")(2)
Case 15 'do what you need to for cell Y15
Range("AA15") = userInput - Range("S58")
Case 16
'do what you need to for cell Y16
Case 17
'do what you need to for cell Y16
Case 18
'do what you need to for cell Y16
End Select
End If
Next myCell 'loop to next cell
End With
End Sub
或,如果所有四个单元格都从S58
获取并放入同一行的AA
列中,例如:
...if your end-goal is the pattern:
AA15 = {Y15 or UserEntry} - S58
AA16 = {Y16 or UserEntry} - S58
AA17 = {Y17 or UserEntry} - S58
AA18 = {Y18 or UserEntry} - S58
...那么类似的事情可能会起作用(甚至更紧凑)。
Private Sub demo_Alternate2()
Dim userInput As Variant, arr As Variant, myCell, rowNum As Long
With Sheets("Sheet1") '<<<<<<<<<<<<< change this to actual worksheet name
arr = .Range("Y15:Y18") ' arr(1) to arr(4) are now cell references
For Each myCell In arr
userInput = InputBox("Enter account limit for " & myCell, "Limit?", 0)
If userInput = "" Or userInput = 0 Then 'Cancelled or 0 entered
MsgBox "Setting " & myCell & " to Zero, No Value Entered"
Else
rowNum = Split(myCell.Address, "$")(2)
Range("AA" & rowNum) = userInput - Range("S58")
End If
Next myCell
End With
End Sub
这里使用的一项值得注意的技术是使用数组(arr
)一次读取多个单元格值,而不是为每个单元格输入单独读取一行。
arr = .Range("Y15:Y18")
...将四个单元格分配给该数组,以便您可以像以下那样引用该数组:
arr(1) = Y15
arr(2) = Y16
arr(3) = Y17
arr(4) = Y18