如何获取消息框中的文本和变量

发布于 2024-12-23 04:52:00 字数 190 浏览 2 评论 0原文

我只需要知道如何在消息框中包含纯文本和变量。

例如:

我可以这样做: MsgBox(variable)

我可以这样做: MsgBox("Variable = ")

但我不能这样做: MsgBox("变量 = " + 变量)

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: MsgBox("Variable = ")

But I can't do this: MsgBox("Variable = " + variable)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

数理化全能战士 2024-12-30 04:52:00

正如所建议的,使用 string.format 方法很好、简单并且非常易读。

在 vb.net 中,“+”用于加法,“&”用于字符串连接。

在您的示例中:

MsgBox("Variable = " + variable)

变为:

MsgBox("Variable = " & variable)

我可能回答得有点快,因为看起来这些运算符都可以用于连接,但建议使用“&”,来源 http://msdn.microsoft.com/en-us/library/te2585xw(v=VS.100).aspx

可能会调用

variable.ToString()

更新:

使用字符串插值(我相信是从 2015 年开始):

MsgBox($"Variable = {variable}")

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}")
空心↖ 2024-12-30 04:52:00

为什么不使用:

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

有关 String.Format

Why not use:

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

More info on String.Format

说谎友 2024-12-30 04:52:00

我遇到了同样的问题。我希望我的消息框显示消息和供应商合同到期时间。这就是我所做的:

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

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
滥情稳全场 2024-12-30 04:52:00
MsgBox("Variable {0} " , variable)
MsgBox("Variable {0} " , variable)
最冷一天 2024-12-30 04:52:00

我想在应用筛选选项后显示 Excel 工作表中的行数。

所以我将最后一行的计数声明为一个可以添加到 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

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
小姐丶请自重 2024-12-30 04:52:00

我认为最简单的解决方案如下:

iNum1 = 10
iNum2 = 20
iSum = iNum1 + iNum2

MsgBox ("The sum of " & iNum1 & " and " & iNum2 & " is " & iSum)

这将导致 10 和 20 的总和是 30

I think the simplest solution would be as follows :

iNum1 = 10
iNum2 = 20
iSum = iNum1 + iNum2

MsgBox ("The sum of " & iNum1 & " and " & iNum2 & " is " & iSum)

this will result in The sum of 10 and 20 is 30

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文