如何将单元格的内容显示在msgbox的消息中

时间:2013-05-27 03:41:43

标签: excel-vba cell msgbox vba excel

我正在尝试开发一个MsgBox来显示来自单元格引用的MsgBox中的问题。

所以在下面的示例中,Msg“请输入公顷数”,我想从Worksheet1 cell A1说出来。

Sub ComplainceQuestion()
    On Error Resume Next
    Dim num As Double
    Dim Save
    num = Application.InputBox(prompt:="Please Enter The Number Of Hectares", Type:=1)
        MsgBox Format(num * 2.47105, "#,##0.00") & " Is the Number Of Acre's."
        Save = MsgBox("Do you want to paste the result in a cell?", vbYesNo)
        If Save = vbYes Then
            Cell = Application.InputBox("Type In The Cell Reference, for example 'G64'")
            Range(Cell).Value = num * 2.471054
        End If
End Sub

1 个答案:

答案 0 :(得分:3)

在原始代码中,num分配了用户输入的值。要为其分配单元格的值(例如A1),只需将行num = Application.Inputbox ...更改为num = Range(“A1”)。value。修改后的代码:

Sub ComplainceQuestion()
On Error Resume Next
Dim num As Double
Dim Save
    num = Range("A1").Value
    MsgBox Format(num * 2.47105, "#,##0.00") & " Is the Number Of Acre's."
    Save = MsgBox("Do you want to paste the result in a cell?", vbYesNo)
    If Save = vbYes Then
        Cell = Application.InputBox("Type In The Cell Reference, for example 'G64'")
        Range(Cell).Value = num * 2.471054
    End If
    End Sub

编辑:将之前提到的行更改为 num = Application.InputBox(prompt:= Range(“A1”)。Value,Type:= 1)