直接在单元格A1上打印宏的输出,而不是在msgbox上显示

时间:2018-02-16 09:57:22

标签: excel vba

我想在Excel工作表中打印以下输出,而不是在msg框中显示。

我写了下面的代码但是我无法在excel表中打印sCommand的输出。 我想在单元格A1上打印输出。 提前谢谢。

sCommand = "Call " & s1 & "." & s2 & "(" & "'" & INPUT_DATE & "'" & "," & "'" &EXIT_DATE & "'" & "," & "STATUS " & ")" & ";"
MsgBox (sCommand)

2 个答案:

答案 0 :(得分:1)

与Gadziu相同,但不使用.Range使用.Cells(1,1):

sCommand = "Call " & s1 & "." & s2 & "(" & "'" & INPUT_DATE & "'" & "," & "'" &EXIT_DATE & "'" & "," & "STATUS " & ")" & ";"

ActiveSheet.Range("A1").Value = sCommand

您还应该声明您实际想要写入哪个工作表,因为ActiveSheet将采用当前选择/活动的工作表,因此可以更好地定义为:

Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
Dim s1 As String, s2 As String, sCommand As String

s1 = InputBox("Enter schema name")
s2 = InputBox("Enter procedure name")
Input_date = Format(Date, "dd/mm/yyyy") 'get today's date or: Input_date = InputBox("Enter Input Date")
Exit_Date = Format(Date, "dd/mm/yyyy") 'get today's date or: Exit_Date = InputBox("Enter Exit Date")
Status = InputBox("Status")

sCommand = "Call " & s1 & "." & s2 & "(" & "'" & Input_date & "'" & "," & "'" & Exit_Date & "'" & "," & Status & ")" & ";"
'concatenate your variables into a string

'ws.Cells(1, 1).Value = sCommand 'the Cells(1, 1) refer to Cells(row_number, column_number)
ws.Range("A1").Value = sCommand
End Sub

答案 1 :(得分:0)

sCommand = "Call " & s1 & "." & s2 & "(" & "'" & INPUT_DATE & "'" & "," & "'" &EXIT_DATE & "'" & "," & "STATUS " & ")" & ";"

ActiveSheet.Cells(1,1).Value = sCommand
相关问题