2013-10-04 34 views
2

這裏是我的子():VBA - Excel 2010中的局部變量沒有初始化在我的功能

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 

兩者都是理智的工作簿。 MsgBox(MyPrompt)返回空白。 謝謝你的幫助。

回答

2

爲什麼你應該使用Option explicit

你看到任何區別的經典案例? ;)

Public Function PopMessage(ByVal Mypromt As String) As String

MsgBox (MyPrompt)

INTERESTING READ (Point 2)

這正好說明了你所面臨的同樣的問題。

+1

好,我知道了!我一直在尋找更困難的事情。這個錯誤是學習更多關於調試藝術的機會。現在Option Explicit將成爲我的第一行代碼! – gabx

+2

除了'OPTION EXPLICIT'外,還避免使用'ON ERROR RESUME NEXT'(OERN)語句,它基本上意味着「我不在乎我的代碼中出現了什麼錯誤,只是執行下一行」。有時候不可能避免OERN聲明。在這種情況下,通過在想要結束OERN效應的行上添加語句ON ERROR GOTO 0,將其範圍限制爲可能的最少行數。 –

+0

@PradeepKumar:好點:) –

0

嗯....一個錯字可以使你頭痛

我同意這是一個很好的習慣,始終聲明變量