使用InputBox将用户input作为文本string作为公式中的variables返回

我想采取用户input,并在公式中插入它。

input框将有一个帐户名称,月份,types的字段。

这个variables需要返回sumifs公式sumifscolumn E是这个帐户column c是这个月, column d是这个types。

我看了,但我发现所有涉及numbers inputsless得多的text string作为sumifs公式中的variables返回。

公式例如:

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

如果你想在一个表单上有三个input框,你将不得不build立一个表单。 如果你能用三个单独的input表单来生活,你可以这样做。

 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") 

您将要对input的值进行一些validation。

然后你可以用它们来build立你的公式

 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,因为我不知道你的目标单元格。