0

I've created a VBA Userform in Excel, where the user selects several ranges. Within the user form, I have input validation through a series of If then MsgBox statements.

As part of this, I need to take the inputted range and use it as a variable.

Assuming that the range is Me.ActDurations, I tried to use this:

dim ActDur as range
set ActDur = Me.ActDurations

I've also tried:

set ActDur = Me.ActDurations.Value

And that doesn't work either. What is the proper syntax for this? Using the first type gives me a type mismatch error.

1 Answer 1

3

The .Value property of RefEdit returns a string. To use it as a range, you should use the string as a range name. Sample code below.

Dim address as String
Dim targetRange As Range

address = RefEdit1.Value 'String returned by the selected range using RefEdit.
Set targetRange = Range(address)

'Do some code here.

Modify as necessary for your code. ;)

Sign up to request clarification or add additional context in comments.

3 Comments

Worth noting, RefEdit value will include a sheet name, and may include a workbook name, in the form '[Workbook Name.extn]Worksheet Name'!RangeAddress. If it includes a workbook name, the answer will not work (works fine without workbook name)
@chris: This is something I'm wary of as well, which is why I personally don't go the RefEdit option if I can avoid it (too much maintenance over one simple value, tbh). However, what I provided should point OP to the right direction. Thanks for pointing out such an important note, though! +1
Awesome, works for me! The assignment I'm completing requires me to use RefEdit, normally I'd avoid it like the plague. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.