引用单元格内容的消息框
Message Box referring to cell contents
我无法获得 msgbox 的正确语法。我想让盒子说:
You have indicated that"
"employee name" (a range reference to a cell the worksheet)
has worked for "hours" (a range reference to a cell the worksheet) doing "job" (a range reference to a cell the worksheet)
Is this information correct?
这是我的(略有缩短):
Public confirmation_yes_no()
Dim yesornoanswertomessagebox As String
Dim questiontomessagebox As String
questiontomessagebox = "You have indicated that" & worksheets("dept 1 input").range("g12"),"worked at" & worksheets("dept 1 input").range("g16"), "for" & worksheets("dept 1 input").range("g16"), vbinformation, "Are you sure that this data is correct?"
yesornoanswertomessagebox = MsgBox(questiontomessagebox, vbYesNo, "FlightPlan for Profits PRO")
If yesornoanswertomessagebox = vbNo Then
MsgBox "Return to Data Input to correct error(s)", 0, "FlightPlan for Profits PRO"
Else
MsgBox "Great! Press Enter", 0, "FlightPlan for Profits PRO"
End If
End Sub
当然,我假设这是可能的。
您好,您错过了“&”符号。所以我给你更正一下。
questiontomessagebox = ("You have indicated that " & Worksheets("dept 1 input").Range("g12") & " ,worked at " _
& Worksheets("dept 1 input").Range("g16") & " for " & Worksheets("dept 1 input").Range("g16")) & Chr(32) & _
vbInformation & vbNewLine & " Are you sure that this data is correct?"
你的代码有几个问题,
- 你的sub开头那行,
Public confirmation_yes_no()
,它是什么,它是一个sub,函数还是什么,现在这样写是一个全局变量声明。
- 当像字符串一样将元素组合成一个时,请始终使用
&
但一定要手动在其周围放置空格,否则无法识别。 &var1
<> & var1
- 在以后使用的变量中设置参数时要小心,绝对不要设置两次。
- 如果您经常使用限定符,例如
Worksheets("dept 1 input")
,请考虑使用如下所示的 With
语句,这样您就不必在 With
语句中键入位和结束。请注意,要使用 with 语句,请在代码前写 .
。
.Range(...
指向由 [=16 设置的 sheet =] 语句。
Range(...
指向 sheet,Excel 认为它是活动的。
- 将变量与文本组合时,请注意变量很可能没有前导空格和尾随空格,您必须在字符串位中对此进行补偿。
- 为了提高可读性,您可以在代码中添加一个
_
以指示它在下一行继续,而不是有一个非常非常长的行。
- 您可以直接在
If
语句中使用消息框。
更正代码
Public Sub confirmation_yes_no()
Dim questiontomessagebox As String
With ThisWorkbook.Worksheets("dept 1 input")
questiontomessagebox = "You have indicated that " & .Range("G12") & " worked at " _
& .Range("G16") & " for " & .Range("G16") & "." _
& vbCr & vbCr _
& "Are you sure that this data is correct?"
End With
If MsgBox(questiontomessagebox, vbYesNo, "FlightPlan for Profits PRO") = vbNo Then
MsgBox "Return to Data Input to correct error(s)", 0, "FlightPlan for Profits PRO"
Else
MsgBox "Great! Press Enter", 0, "FlightPlan for Profits PRO"
End If
End Sub
我无法获得 msgbox 的正确语法。我想让盒子说:
You have indicated that" "employee name" (a range reference to a cell the worksheet) has worked for "hours" (a range reference to a cell the worksheet) doing "job" (a range reference to a cell the worksheet)
Is this information correct?
这是我的(略有缩短):
Public confirmation_yes_no()
Dim yesornoanswertomessagebox As String
Dim questiontomessagebox As String
questiontomessagebox = "You have indicated that" & worksheets("dept 1 input").range("g12"),"worked at" & worksheets("dept 1 input").range("g16"), "for" & worksheets("dept 1 input").range("g16"), vbinformation, "Are you sure that this data is correct?"
yesornoanswertomessagebox = MsgBox(questiontomessagebox, vbYesNo, "FlightPlan for Profits PRO")
If yesornoanswertomessagebox = vbNo Then
MsgBox "Return to Data Input to correct error(s)", 0, "FlightPlan for Profits PRO"
Else
MsgBox "Great! Press Enter", 0, "FlightPlan for Profits PRO"
End If
End Sub
当然,我假设这是可能的。
您好,您错过了“&”符号。所以我给你更正一下。
questiontomessagebox = ("You have indicated that " & Worksheets("dept 1 input").Range("g12") & " ,worked at " _
& Worksheets("dept 1 input").Range("g16") & " for " & Worksheets("dept 1 input").Range("g16")) & Chr(32) & _
vbInformation & vbNewLine & " Are you sure that this data is correct?"
你的代码有几个问题,
- 你的sub开头那行,
Public confirmation_yes_no()
,它是什么,它是一个sub,函数还是什么,现在这样写是一个全局变量声明。 - 当像字符串一样将元素组合成一个时,请始终使用
&
但一定要手动在其周围放置空格,否则无法识别。&var1
<>& var1
- 在以后使用的变量中设置参数时要小心,绝对不要设置两次。
- 如果您经常使用限定符,例如
Worksheets("dept 1 input")
,请考虑使用如下所示的With
语句,这样您就不必在With
语句中键入位和结束。请注意,要使用 with 语句,请在代码前写.
。.Range(...
指向由 [=16 设置的 sheet =] 语句。Range(...
指向 sheet,Excel 认为它是活动的。 - 将变量与文本组合时,请注意变量很可能没有前导空格和尾随空格,您必须在字符串位中对此进行补偿。
- 为了提高可读性,您可以在代码中添加一个
_
以指示它在下一行继续,而不是有一个非常非常长的行。 - 您可以直接在
If
语句中使用消息框。
更正代码
Public Sub confirmation_yes_no()
Dim questiontomessagebox As String
With ThisWorkbook.Worksheets("dept 1 input")
questiontomessagebox = "You have indicated that " & .Range("G12") & " worked at " _
& .Range("G16") & " for " & .Range("G16") & "." _
& vbCr & vbCr _
& "Are you sure that this data is correct?"
End With
If MsgBox(questiontomessagebox, vbYesNo, "FlightPlan for Profits PRO") = vbNo Then
MsgBox "Return to Data Input to correct error(s)", 0, "FlightPlan for Profits PRO"
Else
MsgBox "Great! Press Enter", 0, "FlightPlan for Profits PRO"
End If
End Sub