Home » excel » Excel Macro UI design: How to let the user select several inputs

Excel Macro UI design: How to let the user select several inputs

Posted by: admin May 14, 2020 Leave a comment


The following question is to a lower extent a coding, but rather a UI design problem:

Suppose I have a button somewhere calling a macro when pressed. Now the macro needs two different inputs — say, two ranges. Then it does something with the inputs and writes the result somewhere. What is the best way to let the user specify these two input ranges?

If it were not two, but only one input, it would be easy: We would let the user select the input and then, after the button being pressed, the macro could look for the selected range and evaluate that. Does anyone have any idea how a user-friendly solution in case of two inputs could look like?

How to&Answers:

For example you can get the user to enter two ranges like these (the default selections of A1:A10 and B1:B10 can be removed)

You would also use error checking before proceeding further (in case user had cancelled)

Sub RangeExample()
Dim rng1 As Range
Dim rng2 As Range
Set rng1 = Application.InputBox("Select first range", "Range 1", Range("A1:A10").Address, , , , , 8)
Set rng2 = Application.InputBox("Select second range", "Range 2", Range("B1:B10").Address, , , , , 8)
End Sub