vba - Excel 2010.本地变量未在我的函数中初始化

时间:2013-10-04 07:52:01

标签: excel excel-vba vba

这是我的sub():

Sub SendEmail()

MyPrompt = "want to add a message?"
Mysubject = PopMessage(MyPrompt)

End Sub

这是我的函数():

Public Function PopMessage(ByVal Mypromt As String) As String
'Open a existing userform with a MyPrompt MsgBox and catch user text

FormName = "UserForm1" ' choose here the desired UserForm
Dim Mytext As String
Mytext = ""

On Error Resume Next
MsgBox (MyPrompt) ' only for debuging
Response = MsgBox(Prompt:=MyPrompt, Buttons:=vbYesNo)
    If Response = vbYes Then
    If UserForm1 Is Nothing Then ' we want to check if userform exists
        MsgBox ("Le Formulaire n'existe pas")
        Exit Function
   End If
        Unload UserForm1 ' first we clear the userform
        UserForm1.Show
        Mytext = UserForm1.TextBox1.Text
    End If

End Function

两者都在理智的WorkBook中。 MsgBox(MyPrompt)返回空。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

经典案例,说明您应该使用Option explicit

的原因

你看到有什么区别吗? ;)

Public Function PopMessage(ByVal Mypromt As String) As String

MsgBox (MyPrompt)

INTERESTING READ (Point 2)

这解释了您面临的完全相同的问题。

答案 1 :(得分:0)

嗯....一个错字可能会让你头疼

我同意始终声明变量是一个好习惯