Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VBA to prompt user to select cells (possibly on different sheet) [duplicate]

I'm working in Excel on a VBA project, and want part of my macro to prompt the user to select a range of cells*, which the macro can later do stuff with.

*The type of prompt you get when creating a chart, or using a GUI to insert a function

e.g. here: Screengrab showing required functionality - the Excel 'Sum' Function Arguments pop-up

and here: Screengrab showing required functionality - the Excel Function Arguments pop-up

I'm therefor looking for something along the lines of

Sub MyMacro()
    MsgBox "Please select data range"

    ' allow user to select range (as images above)
    CreateFunctionArgumentsPrompt()    

    'do stuff with user selected range of cells
    ...
End Sub

Is it possible to access built-in Excel functionality to perform what I refer to as: CreateFunctionArgumentsPrompt()

Note: this is similar to SO question excel vba get range of user selected range by mouse but differs in that

  1. I want to use the built in GUI functionality of Excel as displayed above
  2. I need to be able to select and refer to a range on a sheet other than the active worksheet
like image 604
Jonny Avatar asked Apr 02 '14 12:04

Jonny


People also ask

How do you copy a range of cells from one sheet to another in Excel VBA?

To copy a cell or a range of cells to another worksheet you need to use the VBA's “Copy” method. In this method, you need to define the range or the cell using the range object that you wish to copy and then define another worksheet along with the range where you want to paste it.

How do I find duplicates in Excel VBA?

Find and Highlight Duplicates in a Column Using Excel VBA ❶ First of all, press ALT + F11 to open the VBA editor. ❷ Then go to Insert >> Module. ❸ Copy the following VBA code. ❹ Paste and Save the code in the VBA editor.

Can an Excel macro pull data from another workbook?

Notes. This macro allows you to get data from another workbook, or put data into it, or do anything with that workbook. The code is a template that allows you to simply access another Excel file.


1 Answers

This isn't using the built in that you showed above, but does allow you to select a range of cells following an income prompt:

Sub RangeSelectionPrompt()
    Dim rng As Range
    Set rng = Application.InputBox("Select a range", "Obtain Range Object", Type:=8)

    MsgBox "The cells selected were " & rng.Address
End Sub

This is based on the answer given in this MrExcel answer.

Here is how it looks in use:

enter image description here

like image 143
ChrisProsser Avatar answered Sep 21 '22 03:09

ChrisProsser