Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 18
Default Highlight Cell that contains invalid email format

Hi again, I've been working on a spreadsheet that will highlight cells
different colours based on criteria. I've been doing this with VBA...well,
trying :) I've got very very basic skills with VBA and have got stuck trying
to highlight a cell with an invalid email address.

I'd like to be able to check an email address is larger than 7 characters,
contains basic characters only and is in one of these formats:













Where the dots could also be a hyphen.

I've got this code together so far with the count and the character check
working but the email format check (ie:
isn't):

Sub IsEmail()
Dim RangeToCheck As Range, c As Range

Set RangeToCheck = Range("L1:L5000")
For Each c In RangeToCheck

If Len(c.Text) <= 7 Then
c.Interior.Color = vbRed

'This checks that the email is at least 7 characters long

ElseIf c.Text Like "*[!0-9a-z@._+-]*" Then
c.Interior.Color = vbRed

'This checks that the email doesn't contain any 'special' characters

ElseIf Not c.Text Like "*.*" Then
c.Interior.Color = vbRed

but this is as far as I get, I've tried various different things to try and
inderstand the format of the code like:

ElseIf Not c.Text Like "*@*" Then
c.Interior.Color = vbRed

ElseIf c.Text Like "*@*@*" Then
c.Interior.Color = vbRed

but it's getting me nowhere, I can't even see any definition in my results
to carry on. I bought myself a VBA book but I'm still getting to grips with
it. Feel free to comment telling me to read the whole book (several times, as
this is what it'll take I think) and I know I'm being a bit impatient, I can
see what I want to do in my head it's just getting it down that's driving me
mad!:)
Any help or comments would be welcomed.

Best regards,

Gareth
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 834
Default Highlight Cell that contains invalid email format

Sub IsEmail()
Dim RangeToCheck As Range, c As Range
Dim oRegEx As Object
Dim ValidEmail As Boolean

Set RangeToCheck = Range("L1:L5000")
For Each c In RangeToCheck

If Len(c.Text) <= 7 Then
c.Interior.Color = vbRed
Exit Sub
End If

Set oRegEx = CreateObject("VBScript.RegExp")
With oRegEx
.Pattern =
"^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
ValidEmail = .Test(c.Value)
End With
Set oRegEx = Nothing

If Not 7 Then c.Interior.Color = vbRed
Next c

End Sub


--

HTH

Bob

"Gareth_Evans (InterCall EMEA)"
m wrote in message
...
Hi again, I've been working on a spreadsheet that will highlight cells
different colours based on criteria. I've been doing this with VBA...well,
trying :) I've got very very basic skills with VBA and have got stuck
trying
to highlight a cell with an invalid email address.

I'd like to be able to check an email address is larger than 7 characters,
contains basic characters only and is in one of these formats:













Where the dots could also be a hyphen.

I've got this code together so far with the count and the character check
working but the email format check (ie:
isn't):

Sub IsEmail()
Dim RangeToCheck As Range, c As Range

Set RangeToCheck = Range("L1:L5000")
For Each c In RangeToCheck

If Len(c.Text) <= 7 Then
c.Interior.Color = vbRed

'This checks that the email is at least 7 characters long

ElseIf c.Text Like "*[!0-9a-z@._+-]*" Then
c.Interior.Color = vbRed

'This checks that the email doesn't contain any 'special' characters

ElseIf Not c.Text Like "*.*" Then
c.Interior.Color = vbRed

but this is as far as I get, I've tried various different things to try
and
inderstand the format of the code like:

ElseIf Not c.Text Like "*@*" Then
c.Interior.Color = vbRed

ElseIf c.Text Like "*@*@*" Then
c.Interior.Color = vbRed

but it's getting me nowhere, I can't even see any definition in my results
to carry on. I bought myself a VBA book but I'm still getting to grips
with
it. Feel free to comment telling me to read the whole book (several times,
as
this is what it'll take I think) and I know I'm being a bit impatient, I
can
see what I want to do in my head it's just getting it down that's driving
me
mad!:)
Any help or comments would be welcomed.

Best regards,

Gareth



  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 18
Default Highlight Cell that contains invalid email format

Thanks Bob, this is my first experience with RegEx and I'm afraid I'm failing
miserably. I've tried using your code but the cells that are highlighting
confuse me.

When I run the code it highlights cells which contain information but it's
different everytime, I can't see any correlation with the results.

Column L, Rows 1:13



d
..
c
d

@
..
...
..
..
..

The first three cells highlight but the second instances of "D" or
" don't highlight nor do any other cells, just the first
three.

I may not have explained my original request clearly, sorry if I didn't, but
I wanted to highlight cells in column L which aren't in a basic email format
(ie:
etc).
So cells without the @ sign and without at least one dot would be the very
least I'd like to highlight (I know email format's can vary wildly).

Best regards,

Gareth




  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 18
Default Highlight Cell that contains invalid email format

Ok, a slight re-work to my approach. I'm going to simply validate for the
following:

In column L each cell will be highlighted red unless it meets this criteria:

1 @ sign. No more, no less.
Between 1 and 4 dots/full stops
Between 0 and 3 hyphens

I'm going to try using the conditional format option and work record a macro
from this, hopefully I'll make enough sense of the code in relation to my
actions and edit this to suit my needs :)

Will post my results.
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
Conditional Format to Highlight the Active Cell Pawprint Excel Worksheet Functions 1 July 30th 08 09:03 PM
Invalid cell format in Excel MichaelRobert Excel Discussion (Misc queries) 4 November 8th 07 03:08 PM
Excel Auto-Format cell to email format Please Help Me Excel Discussion (Misc queries) 1 March 23rd 06 05:15 PM
How to Sort Invalid Email Addresses? la90292 New Users to Excel 10 February 10th 06 05:36 AM
Conditional Format to highlight blank cell DaveMoore Excel Worksheet Functions 4 June 23rd 05 10:10 AM


All times are GMT +1. The time now is 12:21 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"