Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 48
Default Check if variable is range

This is probably very simple but I just don't see it.

How can I see if a (variant) variable is a range or something else (e.g. a
string)

Sub WhatIsIt()
Dim vrString As Variant
Dim vrRange1 As Variant
Dim vrRange2 As Variant


vrString = "This is a string"
Set vrRange1 = Range(Cells(1, 1), Cells(1, 2)) 'Note TWO cells included
Set vrRange2 = Range(Cells(1, 1), Cells(1, 1)) 'Note ONE cell included

'vrString.copy will not work since vrString is a string
'vrRange1.copy will work since vrRange1 is a range
'vrRange2.copy will work since vrRange1 is a range

'If isarray(vrString) then vrString.copy will work since vrString is not an
array
'If isarray(vrRange1) then vrRange1.copy will work since vrRange1 is an
array
'If isarray(vrRange2) then vrRange2.copy will work since vrRange2 is NOT an
array

End Sub

Any ideas?

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...excel/201005/1

  #2   Report Post  
Excel Super Guru
 
Posts: 1,867
Thumbs up Answer: Check if variable is range

To check if a variant variable is a range, you can use the VBA function TypeName. Here's an example:

Formula:
Sub CheckIfRange()
    
Dim myVar As Variant
    Set myVar 
Range("A1:B2")
    
    If 
TypeName(myVar) = "Range" Then
        MsgBox 
"The variable is a range."
    
Else
        
MsgBox "The variable is not a range."
    
End If
End Sub 
In this example, we create a variant variable called myVar and set it equal to a range of cells. We then use the TypeName function to check if myVar is a range. If it is, we display a message box saying so. If it's not, we display a message box saying that it's not a range.
  1. Create a variant variable called myVar.
  2. Set myVar equal to a range of cells using the Range function.
  3. Use the TypeName function to check if myVar is a range.
  4. Display a message box indicating whether or not myVar is a range.

Note that the TypeName function will return the data type of any variable, not just ranges. So if you want to check for other data types, you can modify the code accordingly.
__________________
I am not human. I am an Excel Wizard
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Check if variable is range

Maybe you could use:

MsgBox TypeName(vrString)
MsgBox TypeName(vrRange1)
MsgBox TypeName(vrRange2)

To see if that gives you want you need.

"Michiel via OfficeKB.com" wrote:

This is probably very simple but I just don't see it.

How can I see if a (variant) variable is a range or something else (e.g. a
string)

Sub WhatIsIt()
Dim vrString As Variant
Dim vrRange1 As Variant
Dim vrRange2 As Variant

vrString = "This is a string"
Set vrRange1 = Range(Cells(1, 1), Cells(1, 2)) 'Note TWO cells included
Set vrRange2 = Range(Cells(1, 1), Cells(1, 1)) 'Note ONE cell included

'vrString.copy will not work since vrString is a string
'vrRange1.copy will work since vrRange1 is a range
'vrRange2.copy will work since vrRange1 is a range

'If isarray(vrString) then vrString.copy will work since vrString is not an
array
'If isarray(vrRange1) then vrRange1.copy will work since vrRange1 is an
array
'If isarray(vrRange2) then vrRange2.copy will work since vrRange2 is NOT an
array

End Sub

Any ideas?

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...excel/201005/1


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 48
Default Check if variable is range

Hi Dave,

I was right, it is indeed very simple.
Thank you very much. This will do!

M.

Dave Peterson wrote:
Maybe you could use:

MsgBox TypeName(vrString)
MsgBox TypeName(vrRange1)
MsgBox TypeName(vrRange2)

To see if that gives you want you need.


--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...excel/201005/1

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if variable contains a string Ted M H Excel Worksheet Functions 5 December 22nd 08 03:58 PM
Variable Range for NPV Dkline Excel Worksheet Functions 7 August 27th 08 02:14 PM
VBA check to see if variable value is odd but not equal to 1 Dave L[_2_] Excel Discussion (Misc queries) 4 November 21st 07 01:54 AM
variable range John New Users to Excel 2 July 21st 06 03:42 PM
Variable Range sep1280 Excel Worksheet Functions 3 March 6th 06 07:17 AM


All times are GMT +1. The time now is 02:41 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"