使用InputBox将用户输入作为文本字符串返回为公式中的变量

时间:2016-01-18 19:26:00

标签: excel vba excel-vba

我想接受用户输入并将其插入公式中。

输入框将包含帐户名称,月份,类型的字段。

此变量需要在sumifs公式中返回sumifscolumn E此帐户column c是本月,column d是此类型

我看过,但我发现所有内容都涉及numbers inputs,而不是将text string作为sumifs公式中的变量返回。

Formula Ex:

=SUMIFS(E2:E100, A2:A100, "Account Name",C2:C100,"Month",D2:D100,"Type")

1 个答案:

答案 0 :(得分:1)

如果要在一个表单上放置三个输入框,则必须构建表单。如果您可以使用三个单独的输入表单,则可以这样做。

Dim AccountName As Variant 
AccountName = InputBox("Please Enter the account name.*", "Account Name")
Dim AccountMonth As Variant 
AccountMonth = InputBox("Please Enter the account name.*", "Account Date")
Dim AccountType As Variant 
AccountType = InputBox("Please Enter the account name.*", "Account Type")

您需要对输入的值进行一些验证。

然后你可以用它们来构建你的公式

Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")

ws.Range("A2").Formula = "=sumifs(E2:E100, A2:A100, " & AccountName & ",C2:C100," & AccountMonth  & ",D2:D100," & AccountMonth & ")"

您需要将要编写公式的单元格定位到。我使用A2是因为我不知道你的目标细胞。