vba 编译错误:外部程序无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18462390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:59:29  来源:igfitidea点击:

Compile error: invalid outside procedure

excelexcel-vbavba

提问by Atiq Samtia

I m trying to make a excel vba based calculator witch post the out put result to a specific cell in excel sheet but when i am trying to call the user form an error occurred Compile error: invalid outside procedure my code of macro is as under

我正在尝试制作一个基于 excel vba 的计算器,将输出结果发布到 excel 表中的特定单元格,但是当我尝试调用用户表单时,发生了错误编译错误:外部程序无效,我的宏代码如下

    'Declare the global variables to be used throughout the form
Dim mfirst As Single
Dim msecond As Single
Dim manswer As Single
' Declare the global variables for the operators: Add,Sub,Mul and DIV
Dim mbutton As Integer
Dim ws As Worksheet
Set ws = Worksheet("entry")
ws.Range("b4").Value = manswer
'Change the sign of the number from + or - or vice versa
' Depending on its state now they show in txtNUMBER text box
Dim Signstate As Boolean
Private Sub Backspc_Click()
TXTnumber = Len(TXTnumber) - 1
End Sub

Private Sub cmd0_Click()
'Put the value 0 into the txtNUMBER text box
TXTnumber = TXTnumber + "0"
End Sub

Private Sub cmd1_Click()
'Put the value 1 into the txtNUMBER text box
TXTnumber = TXTnumber + "1"
End Sub

Private Sub cmd2_Click()
'Put the value 2 into the txtNUMBER text box
TXTnumber = TXTnumber + "2"
End Sub

Private Sub cmd3_Click()
'Put the value 3 into the txtNUMBER text box
TXTnumber = TXTnumber + "3"
End Sub

Private Sub cmd4_Click()
'Put the value 4 into the txtNUMBER text box
TXTnumber = TXTnumber + "4"
End Sub

Private Sub cmd5_Click()
'Put the value 5 into the txtNUMBER text box
TXTnumber = TXTnumber + "5"
End Sub

Private Sub cmd6_Click()
'Put the value 6 into the txtNUMBER text box
TXTnumber = TXTnumber + "6"
End Sub

Private Sub cmd7_Click()
'Put the value 7 into the txtNUMBER text box
TXTnumber = TXTnumber + "7"
End Sub

Private Sub cmd8_Click()
'Put the value 8 into the txtNUMBER text box
TXTnumber = TXTnumber + "8"
End Sub

Private Sub cmd9_Click()
'Put the value 9 into the txtNUMBER text box
TXTnumber = TXTnumber + "9"
End Sub

Private Sub cmdADD_Click()
'User slected the add button
mbutton = 1
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(TXTnumber)

TXTnumber = ""
End Sub



Private Sub cmdSUBTRACT_Click()
'User slected the minus button
mbutton = 2
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(TXTnumber)

TXTnumber = ""
End Sub

Private Sub cmdMULTIPLY_Click()
'User slected the multiply button
mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(TXTnumber)

TXTnumber = ""
End Sub

Private Sub cmdDIVIDE_Click()
'User slected the Divide button
mbutton = 4
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(TXTnumber)

TXTnumber = ""
End Sub
Private Sub cmdEQUALS_Click()
msecond = Val(TXTnumber)

Select Case mbutton
Case Is = 1
manswer = mfirst + msecond
Case Is = 2
manswer = mfirst - msecond
Case Is = 3
manswer = mfirst * msecond
Case Is = 4
manswer = mfirst / msecond
End Select
TXTnumber = manswer
End Sub
Private Sub cmdDOT_Click()
TXTnumber = TXTnumber + "."
End Sub
Private Sub cmdSIGN_Click()
'Sign state = false on load of form
If TXTnumber = "-" + TXTnumber Then
    MsgBox "error start again"
End If
If Signstate = False Then
TXTnumber = "-" + TXTnumber
Signstate = True
Else
'SignState = True

minusvalue = Val(TXTnumber)
'Value now positive
minusvalue = Val("-1" * minusvalue)
TXTnumber = minusvalue
Signstate = False

End If
End Sub
Private Sub cmdEXIT_Click()
Unload frmCALCULATOR
End Sub

Private Sub cmdCANCEL_Click()
'Remove the values in the txtNUMBER text box
TXTnumber = " "
End Sub

回答by SWa

You can only declare variables and constants outside of a Sub routine/function. As such

您只能在 Sub 例程/函数之外声明变量和常量。像这样

Set ws = Worksheet("entry")
ws.Range("b4").Value = manswer

Needs to be in a sub, maybe the useform initialize routine?

需要在 sub 中,也许是 useform 初始化例程?