How to get text and a variable in a messagebox

前端 未结 5 1121
滥情空心
滥情空心 2021-02-07 15:11

I just need to know how to have plain text and a variable in a messagebox.

For example:

I can do this: MsgBox(variable)

And I can do this: <

相关标签:
5条回答
  • 2021-02-07 15:37

    I kind of run into the same issue. I wanted my message box to display the message and the vendorcontractexpiration. This is what I did:

    Dim ab As String
    Dim cd As String
    
    ab = "THE CONTRACT FOR THIS VENDOR WILL EXPIRE ON "
    cd = VendorContractExpiration
    
    
    If InvoiceDate >= VendorContractExpiration - 120 And InvoiceDate < VendorContractExpiration Then
    
    MsgBox [ab] & [cd], vbCritical, "WARNING"
    
    End If
    
    0 讨论(0)
  • 2021-02-07 15:43

    Why not use:

    Dim msg as String = String.Format("Variable = {0}", variable)
    

    More info on String.Format

    0 讨论(0)
  • 2021-02-07 15:45
    MsgBox("Variable {0} " , variable)
    
    0 讨论(0)
  • 2021-02-07 15:49

    I wanto to display the count of rows in the excel sheet after the filter option has been applied.

    So I declared the count of last rows as a variable that can be added to the Msgbox

    Sub lastrowcall()
    Dim hm As Worksheet
    Dim dm As Worksheet
    Set dm = ActiveWorkbook.Sheets("datecopy")
    Set hm = ActiveWorkbook.Sheets("Home")
    Dim lngStart As String, lngEnd As String
    lngStart = hm.Range("E23").Value
    lngEnd = hm.Range("E25").Value
    Dim last_row As String
    last_row = dm.Cells(Rows.Count, 1).End(xlUp).Row
    
    MsgBox ("Number of test results between the selected dates " + lngStart + " 
    and " + lngEnd + " are " + last_row + ". Please Select Yes to continue 
    Analysis")
    
    
    End Sub
    
    0 讨论(0)
  • 2021-02-07 15:57

    As has been suggested, using the string.format method is nice and simple and very readable.

    In vb.net the " + " is used for addition and the " & " is used for string concatenation.

    In your example:

    MsgBox("Variable = " + variable)
    

    becomes:

    MsgBox("Variable = " & variable)
    

    I may have been a bit quick answering this as it appears these operators can both be used for concatenation, but recommended use is the "&", source http://msdn.microsoft.com/en-us/library/te2585xw(v=VS.100).aspx

    maybe call

    variable.ToString()
    

    update:

    Use string interpolation (vs2015 onwards I believe):

    MsgBox($"Variable = {variable}")
    
    0 讨论(0)
提交回复
热议问题