Declaring variable as Range or Variant

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The user brings up a userform which contains a RefEdit box.

The RefEdit box is populated either by selecting a range on the worksheet or typing a value into it.

The variable assigned to the contents of RefEdit is called ChosenCells.

How should ChosenCells be declared? As a Range or Variant?

If I declare it as a range but the user types in a value, it'll crash.

If I declare as a Variant but the user selects a range on the worksheet, the program won't recognise it.

Thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
What is the user supposed to be doing with the RefEdit? Presumably selecting a range, so you should declare as range and use error handling as necessary.
 
Upvote 0
What is the user supposed to be doing with the RefEdit? Presumably selecting a range, so you should declare as range and use error handling as necessary.
Yes, the user is meant to be selecting a range (could be just a single cell) but some have asked to be able to type in a value instead.

I tried error handling such as:

Code:
If TypeName(ChosenCells) = "Range" Then

and

If TypeName(Range(ChosenCells) = "Range" Then

but neither worked.
 
Upvote 0
Code:
On Error Resume Next
Set ChosenCells = Range(refeditcontrol)
On Error Goto 0
If ChosenCells is Nothing then
' it was a value
end if
 
Upvote 0
Solution

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top