Using InputBox to return user input as a text string as a variable in a formula

烈酒焚心 提交于 2020-01-06 19:26:45

问题


I would like to take user input and insert it in a formula.

The input box would have a field for account name, month, type.

This variable would need to be returned in the sumifs formula saying sumifs, column E is this account column c is this month and column d is this type.

I've looked but all I find involve numbers inputs much less returning the text string as a variable in a sumifs formula.

Formula Ex:

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


回答1:


You will have to build a form if you want to have the three input boxes on one form. If you can live with three separate input forms you can do it like this.

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

You will want to put some validations on the values that are entered.

Then you can use them to build you formula

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

You will need to target the cell you want to write the formula into. I used A2 because I didn't know your target cell.



来源:https://stackoverflow.com/questions/34862502/using-inputbox-to-return-user-input-as-a-text-string-as-a-variable-in-a-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!