Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
mp mp is offline
external usenet poster
 
Posts: 70
Default regex validate rangename

any regex experts here?
trying to validate a string passed in to see if it refers to a valid excel
range name
"a1" or "A1" to "iv65536" or "IV65536"

this clearly isn't it, i'm a regex dunce!
Regex regex = new Regex("[a-zA-Z][a-iA-I][a-vA-V][1-65536]");
this is no good either
Regex regex = new Regex("[a-zA-Zaa-ivAA-IV][1-65536]");

bool b;
b = regex.IsMatch("x1");
Debug.Print(b.ToString());//true

b = regex.IsMatch("1a");
Debug.Print(b.ToString());//false

b = regex.IsMatch("AC65536");
Debug.Print(b.ToString());//true

b = regex.IsMatch("IV65537");
Debug.Print(b.ToString());//false

b = regex.IsMatch("IV65536");
Debug.Print(b.ToString());//true

any tips appreciated
i suppose i could parse the incoming string and separate the alpha and
numeric parts
but with regex that shouldn't be necessary, right?
thanks
mark


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default regex validate rangename

Firstly, the example you posted aren't range names. They're range
address references. -Don't have a regex solution but I do have some VBA
solutions...

For checking if a cell address exists:



For checking if a 'named' range exists:

Function bNameExists(DefinedName As String) As Boolean
' Checks for a name in the active workbook
' Arguments: DefinedName The defined name
' Returns: True if name exists

Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Names(DefinedName)
bNameExists = (Err = 0)
End Function

Function bValidSheet(RangeName As String) As Boolean
' Checks for a local named range on the active sheet
' Arguments: RangeName The defined name of a range
' Returns: True if sRangeName exists

Dim x As Object
On Error Resume Next
Set x = ActiveSheet.Range(RangeName)
bValidSheet = (Err = 0)
End Function

Usage:
If bNameExists("DefinedName") Then
'Do stuff
End If

If bValidSheet("RangeName") Then
'Do stuff
End If

OR use an operator
If Not...

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default regex validate rangename

GS used his keyboard to write :
For checking if a cell address exists:


?? Message sent before I clicked 'Send'!!!

To check if a cell address exists:
Dim x As Range, bRangeExists As Boolean
On Error Resume Next
Set x = ActiveSheet.Range("A1")
bRangeExists = (Err = 0)

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default regex validate rangename

How about a non-RegEx solution...

Function IsRangeAddress(PossibleAddress As String) As Boolean
On Error Resume Next
IsRangeAddress = Range(PossibleAddress).Row
On Error GoTo 0
End Function

Just pass your range address text into the function and it will return True
if that text is a valid range address for the workbook it is being run from.
If you do not want he function housing, you can just embed the three lines
of code directly into your own code at the appropriate location.

Rick Rothstein (MVP - Excel)



"mp" wrote in message ...

any regex experts here?
trying to validate a string passed in to see if it refers to a valid excel
range name
"a1" or "A1" to "iv65536" or "IV65536"

this clearly isn't it, i'm a regex dunce!
Regex regex = new Regex("[a-zA-Z][a-iA-I][a-vA-V][1-65536]");
this is no good either
Regex regex = new Regex("[a-zA-Zaa-ivAA-IV][1-65536]");

bool b;
b = regex.IsMatch("x1");
Debug.Print(b.ToString());//true

b = regex.IsMatch("1a");
Debug.Print(b.ToString());//false

b = regex.IsMatch("AC65536");
Debug.Print(b.ToString());//true

b = regex.IsMatch("IV65537");
Debug.Print(b.ToString());//false

b = regex.IsMatch("IV65536");
Debug.Print(b.ToString());//true

any tips appreciated
i suppose i could parse the incoming string and separate the alpha and
numeric parts
but with regex that shouldn't be necessary, right?
thanks
mark

  #5   Report Post  
Posted to microsoft.public.excel.programming
mp mp is offline
external usenet poster
 
Posts: 70
Default regex validate rangename


"Rick Rothstein" wrote in message
...
How about a non-RegEx solution...

Function IsRangeAddress(PossibleAddress As String) As Boolean
On Error Resume Next
IsRangeAddress = Range(PossibleAddress).Row
On Error GoTo 0
End Function

Just pass your range address text into the function and it will return
True if that text is a valid range address for the workbook it is being
run from. If you do not want he function housing, you can just embed the
three lines of code directly into your own code at the appropriate
location.

Rick Rothstein (MVP - Excel)

yes, easier to let Excel do the validation
thanks Rick and GS
mark





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default regex validate rangename

On Sat, 15 Jan 2011 13:37:14 -0600, "mp" wrote:

any regex experts here?
trying to validate a string passed in to see if it refers to a valid excel
range name
"a1" or "A1" to "iv65536" or "IV65536"


There are easier ways to validate a range reference than using regular expressions, but for what it's worth, the following should work with Excel 2003 and earlier, and also demonstrates a method to validate defined ranges of letters or numbers:

\$?\b([A-Z]|[A-H][A-Z]|I[A-V])\$?([1-9]\d{0,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-6])\b([:\s]\$?\b([A-Z]|[A-H][A-Z]|I[A-V])\$?([1-9]\d{0,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-6])\b)?

And try this for Excel 2007 and later:

\$?(?:\bXF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b([:\s]\$?(?:\bXF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b)?

  #7   Report Post  
Posted to microsoft.public.excel.programming
mp mp is offline
external usenet poster
 
Posts: 70
Default regex validate rangename


"Ron Rosenfeld" wrote in message
...
On Sat, 15 Jan 2011 13:37:14 -0600, "mp" wrote:

any regex experts here?
trying to validate a string passed in to see if it refers to a valid excel
range name
"a1" or "A1" to "iv65536" or "IV65536"


There are easier ways to validate a range reference than using regular
expressions, but for what it's worth, the following should work with Excel
2003 and earlier, and also demonstrates a method to validate defined
ranges of letters or numbers:

\$?\b([A-Z]|[A-H][A-Z]|I[A-V])\$?([1-9]\d{0,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-6])\b([:\s]\$?\b([A-Z]|[A-H][A-Z]|I[A-V])\$?([1-9]\d{0,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-6])\b)?

And try this for Excel 2007 and later:

\$?(?:\bXF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b([:\s]\$?(?:\bXF[A-D]|X[A-E][A-Z]|[A-W][A-Z]{2}|[A-Z]{2}|[A-Z])\$?(?:104857[0-6]|10485[0-6]\d|1048[0-4]\d{2}|104[0-7]\d{3}|10[0-3]\d{4}|[1-9]\d{1,5}|[1-9])d?\b)?


holy cow batman, that's one for the record books!
it will take me a while to digest that!
Thanks
like you say, and others have pointed out elsewhere, probably easier to let
excel throw it's own error if given bad input.
but if i get to where i can decipher your answer it will take me a long
way toward learning more about regex

I would have to Ucase the input I see.
Since i can pass "a1" to excel and it will understand I mean "A1"
where the regex wouldn't...however that would be an easy
and sensible fix for a function that would be doing this validating
Thanks
mark


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default regex validate rangename

On Sun, 16 Jan 2011 00:18:28 -0600, "mp" wrote:

I would have to Ucase the input I see.
Since i can pass "a1" to excel and it will understand I mean "A1"
where the regex wouldn't...however that would be an easy
and sensible fix for a function that would be doing this validating


Actually not. If case insensitivity is a requirement, you can just set to ignore case in your regex object.

In VBA, ignorecase is a property of the regex object, so you just need to set it to True.

e.g. regex.Ignorecase = true

(where regex is the name of your regular expression object)
  #9   Report Post  
Posted to microsoft.public.excel.programming
mp mp is offline
external usenet poster
 
Posts: 70
Default regex validate rangename


"Ron Rosenfeld" wrote in message
...
On Sun, 16 Jan 2011 00:18:28 -0600, "mp" wrote:

I would have to Ucase the input I see.
Since i can pass "a1" to excel and it will understand I mean "A1"
where the regex wouldn't...however that would be an easy
and sensible fix for a function that would be doing this validating


Actually not. If case insensitivity is a requirement, you can just set to
ignore case in your regex object.

In VBA, ignorecase is a property of the regex object, so you just need to
set it to True.

e.g. regex.Ignorecase = true

(where regex is the name of your regular expression object)


oh right, i forgot about that!
Thanks again
mark
ps, i'm on excel 2002
did the max row/col increase with later versions?
I seem to have IV65536 as the lowest rightmost cell.


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 621
Default regex validate rangename

2003 remains same as 2002 regarding rows and columns.

2007 and later moved to 16,384 columns and 1,048,576 rows.


Gord Dibben MS Excel MVP

On Sun, 16 Jan 2011 09:18:38 -0600, "mp" wrote:

oh right, i forgot about that!
Thanks again
mark
ps, i'm on excel 2002
did the max row/col increase with later versions?
I seem to have IV65536 as the lowest rightmost cell.



  #11   Report Post  
Posted to microsoft.public.excel.programming
mp mp is offline
external usenet poster
 
Posts: 70
Default regex validate rangename

Thanks
mark

"Gord Dibben" wrote in message
...
2003 remains same as 2002 regarding rows and columns.

2007 and later moved to 16,384 columns and 1,048,576 rows.


Gord Dibben MS Excel MVP

On Sun, 16 Jan 2011 09:18:38 -0600, "mp" wrote:

oh right, i forgot about that!
Thanks again
mark
ps, i'm on excel 2002
did the max row/col increase with later versions?
I seem to have IV65536 as the lowest rightmost cell.



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
Testing For a RangeName JCS Excel Discussion (Misc queries) 3 August 13th 08 01:08 PM
Confused with RangeName CellREfs Jim May Excel Discussion (Misc queries) 1 October 22nd 06 11:11 PM
union of RangeName Bob Phillips Excel Programming 1 October 20th 06 12:35 PM
Concatenate RangeName for INDEX? CLR Excel Worksheet Functions 5 November 30th 04 07:47 PM
Validate Excel Range with RegEx Fletch[_2_] Excel Programming 1 December 8th 03 03:22 PM


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

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

About Us

"It's about Microsoft Excel"