
插入一个模块:
Public Sub f()
Static a, b
a = 3
b = 4
UserForm1.Show
End Sub
插入一个用户窗体,放一个文本输入框和一个命令按钮,命令按钮的click事件:
Public Sub CommandButton1_Click()
If Not IsNumeric(Trim(TextBox1.Value)) Then
MsgBox "输入一个实数"
Exit Sub
End If
Dim c, d
c = Trim(TextBox1.Value)
d = a + b - c
MsgBox "计算结果是" & d
Unload Me
End Sub
随便输入一个数比如8,得到结果-8,表明用户窗体没有调用模块里的变量值,将模块里的变量用static声明也不行,请问该怎么做?
虽然过程f()是用public定义的,但是巧键Static a, b的作用域只是在f()这个sub中,其他过程是无法访问到的。
如果确孝让巧实需要的话你可以将a,b的声明用public放在通用:
public a,b
Public Sub f()
a = 3
b = 4
UserForm1.Show
End Sub
或者你可以将声明放在窗体中
而滑逗变量的初始化可以放在窗体的Load事件中
private a,b
private sub form1_load()
a=3
b=4
end sub
Public pas As Long
Sub qq()
Dim i, st
st = MsgBox("你确定要显示答案?备态岁需要正确输入密码", 1)
If st = 1 Then
UserForm1.Show
End If
If st + pas = 3315057 Then
For i = 5 To Cells(Rows.Count, 1).End(xlUp).Row
Sheets("Sheet2").Unprotect Password:="3315056"
Cells(i, 5).Formula = "=vlookup(A" & i & ",Sheet2!A:D,4,0)"
Next i
Else
Exit Sub
End If
Sheets("Sheet2"仿睁).Protect Password:="闭肢3315056"
End Sub