Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 390
Default Easy way to test range as single cell

I'm trying to come up with an easy way in VBA to know if
the selected range is one single cell only -- and from a
restricted range of cells. I suspect this is trivially
easy to do, but the how of it escapes me at this moment.
Ideas gladly solicited.

dman
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 69
Default Easy way to test range as single cell

On 9 Dec, 17:45, Dallman Ross <dman@localhost. wrote:
I'm trying to come up with an easy way in VBA to know if
the selected range is one single cell only -- and from a
restricted range of cells. I suspect this is trivially
easy to do, but the how of it escapes me at this moment.
Ideas gladly solicited.

dman


If the user has already selected the range then it would be:

if Selection.Cells.Count 1 then
Msgbox "More than one cell is selected"
end if

James

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Easy way to test range as single cell

You might try

' ensure a Range is selected
If TypeOf Selection Is Excel.Range Then
If Selection.Cells.Count = 1 Then
' one cell selected
If Not Application.Intersect(Selection, Range("A1:A10")) Is Nothing
Then
' range is within A1:A10
' do something
Else
' not in A1:A10
End If
Else
' more than one cell selected
End If
Else
' selection is not a range (perhaps a Shape or something)
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Dallman Ross" <dman@localhost. wrote in message
...
I'm trying to come up with an easy way in VBA to know if
the selected range is one single cell only -- and from a
restricted range of cells. I suspect this is trivially
easy to do, but the how of it escapes me at this moment.
Ideas gladly solicited.

dman


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Easy way to test range as single cell

Hi

Try this:

Option Explicit

Dim isect As Variant
Dim rRange As Range

Sub test()
Set rRange = Range("A1:C5")
If Selection.Rows.Count * Selection.Columns.Count 1 Then MsgBox "Multiple
cells selected!"

Set isect = Application.Intersect(Selection, rRange)
If isect Is Nothing Then
MsgBox "Ranges do not intersect"
Else
isect.Select
End If

End Sub

"Dallman Ross" <dman@localhost. skrev i en meddelelse
...
I'm trying to come up with an easy way in VBA to know if
the selected range is one single cell only -- and from a
restricted range of cells. I suspect this is trivially
easy to do, but the how of it escapes me at this moment.
Ideas gladly solicited.

dman



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 390
Default Easy way to test range as single cell

Thank you, Chip, this looks very good indeed. Per's similar
approach was also interesting to see. Thanks to you both!

dman

----------------------
In , Chip
Pearson spake thusly:

You might try

' ensure a Range is selected
If TypeOf Selection Is Excel.Range Then
If Selection.Cells.Count = 1 Then
' one cell selected
If Not Application.Intersect(Selection, Range("A1:A10")) Is Nothing
Then
' range is within A1:A10
' do something
Else
' not in A1:A10
End If
Else
' more than one cell selected
End If
Else
' selection is not a range (perhaps a Shape or something)
End If


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
Test to see if selected range is single row? davegb Excel Programming 6 March 19th 07 09:18 PM
Test if CELL is in RANGE Vaughan Excel Worksheet Functions 12 November 19th 06 02:48 PM
Formula that will test text conditions in a single cell Prohock Excel Worksheet Functions 10 April 4th 06 10:21 PM
Range.Find returns cell outside of range when range set to single cell Frank Jones Excel Programming 12 June 10th 04 04:22 AM
easy way to test if a Named Range exists Andrew Bauer Excel Programming 4 July 10th 03 07:32 PM


All times are GMT +1. The time now is 11:44 AM.

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"