代码之家  ›  专栏  ›  技术社区  ›  Gangula

更改整个工作簿的所有MSGBox的标题

  •  0
  • Gangula  · 技术社区  · 6 年前

    我创建了一个Excel模板,组织中有多个人在使用它。我在代码中添加了很多MSGBox,用于错误或通知用户。我根据发现的新问题不断修改代码,并添加更多的msgboxes。所有这些MSGBox的标题都是“Microsoft Excel”

    我知道我们可以通过将第三个参数添加到 msgbox 方法但我要找的是更改 所有的msgboxes (现有和新增)将参数(用户名/工作簿名)添加到msgbox的标题中。

    2 回复  |  直到 6 年前
        1
  •  1
  •   barrowc    6 年前

    如果不想在每个调用中更改title参数 MsgBox 然后可以创建一个名为 消息框 隐藏内置函数。自定义函数只是将其所有参数传递给内置函数 消息框 功能,但它取代了 title 改为使用自定义标题的参数。

    Function MsgBox(prompt As String, Optional buttons As Long, Optional title As String, _
        Optional helpfile As String, Optional context As Long) As Integer
    
    ' Pass through the parameters to the built-in MsgBox function
    ' but replace the title with our own version
    ' Use the return value from the built-in function as our return value
    MsgBox = VBA.MsgBox(prompt, buttons, "Custom Title", helpfile, context)
    
    End Function
    

    这是因为隐藏的概念:如果我们没有完全限定我们试图调用的函数的名称,那么在本地范围内使用该名称的函数将被调用,而不是使用相同名称的内置函数。如果完全限定名称,我们仍然可以使用内置函数- VBA.MsgBox -像这样:

    ' Call the custom function
    MsgBox "Hello world!"
    
    ' Call the built-in function
    VBA.MsgBox "Hello world!"
    

    我不认为这个答案是解决这个问题的特别好的方法。将所有通话改为 消息框 包含title参数肯定比创建这样的自定义函数更可取


    编辑:我们可以通过检查是否为我们的函数提供了一个标题来稍微改善这一点。要做到这一点,我们可以使用 IsMissing 但这需要 标题 要成为变体而不是字符串:

    Function MsgBox(prompt As String, Optional buttons As Long, Optional title As Variant, _
        Optional helpfile As String, Optional context As Long) As Integer
    
    ' Pass through the parameters to the built-in MsgBox function
    ' Use the return value from the built-in function as our return value
    
    If IsMissing(title) Then
        ' If no title has been supplied then use our custom title
        MsgBox = VBA.MsgBox(prompt, buttons, "Custom Title", helpfile, context)
    Else
        ' Otherwise use the title supplied as a parameter
        MsgBox = VBA.MsgBox(prompt, buttons, title, helpfile, context)
    End If
    
    End Function
    

    我们可以这样测试我们的新版本是否正常工作:

    ' Call the custom function
    MsgBox "Hello world!"
    
    ' Call the custom function with a title
    MsgBox "Hello world!", vbOKOnly, "My Title"
    
    ' Call the built-in function
    VBA.MsgBox "Hello world!"
    
        2
  •  0
  •   FaneDuru    6 年前

    请尝试下一种方法:

    1. 创建一个 Public 我们可以这么说, msgTitle 在标准模块顶部(声明区域):
        Public msgTitle as String
    
    1. 按以下方式初始化变量: 在里面 Workbook_Open() 活动:
        Private Sub Workbook_Open()
            msgTitle = Application.UserName & "_" & ThisWorkbook.Name
        End Sub
    

    2之二:如果变量因未知原因失去其值,在某些事件中(工作表更改、SelectionChange等),变量将以以下方式重新初始化:

        If msgTitle = "" Then msgTitle = Application.UserName & "_" & ThisWorkbook.Name
    
    1. 创建所有必要的 MsgBox 这样:
       MsgBox "Whatever necessary to be announced", vbCritical, msgTitle
    
        3
  •  0
  •   BDra    6 年前

    只需将msgbox title声明为公共变量,就可以在整个项目中使用它:

    Public MsgBoxTitle As String
    
    Sub InitialSub()
    
        MsgBoxTitle = Application.UserName & " - " & ThisWorkbook.Name
        AnotherSub
    
    End Sub
    
    Sub AnotherSub()
    
        MsgBox "This is a test!", vbInformation + vbOKOnly, MsgBoxTitle
    
    End Sub
    
    推荐文章