excel - vba - Excel 2010。局部变量未在我的函数中初始化

标签 excel vba

这是我的子():

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) 返回空。 谢谢您的帮助。

最佳答案

为什么应该使用Option Explicit的经典案例

你看出什么不同了吗? ;)

公共(public)函数 PopMessage(ByVal Mypromt As String) As String

MsgBox(我的提示)

INTERESTING READ (Point 2)

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

关于excel - vba - Excel 2010。局部变量未在我的函数中初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19176051/

相关文章:

excel - 通过宏查找代码并将其添加到模块中

vba - 打开保存有密码的 Excel 文件进行修改

vba - 使用excel vba从网站下载zip文件(如果还能够从zip文件中提取csv并在excel中打开它,那就更好了)

vba - 为此代码实现粘贴链接

Excel VBE 导出不一致

excel - 在IE窗口中填写表单: Press buttons and fill in text boxes

excel - 删除 Excel 表格中的所有数据行(第一行除外)

excel - 使用excel宏在word中插入分页符

ms-access - DoCmd.TransferSpreadsheet 问题

ms-access - 在 MS Access 中通过 FileDialog 保存时如何过滤报表对象