Excel VBA: Validate Range entered in cell.

A user would like to enter ranges in an Excel workbook. What VBA would I use that would label in column C whether the ranges he/she entered are valid or not.

EXAMPLE:

Row       Column:B            Column:C
  1          A1:B200                  Valid
  2          BC10: DD10           Valid
  3          A:B200                    Invalid
  4          C200:A1                  Invalid

The code should say row 3 is an invalid range because it missing the row reference after the "A".
The code should say Row 4 is an invalid range because column "C" is before column "A".
ouestqueAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
You don't need VBA for this. You can have

=IF(ISREF(INDIRECT(B2)),"Valid","Invalid")
0
Rgonzo1971Commented:
HI

pls try
Sub macro()

For Each c In Range(Range("B1"), Range("B" & Rows.Count).End(xlUp))
    If Evaluate("=ISREF(" & c.Text & ")") Then
        c.Offset(, 1) = "Valid"
    Else
        c.Offset(, 1) = "Invalid"
    End If
Next
End Sub

Open in new window

As a side remark (C200:A1) is a valid range if not conventional

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Saqib Husain, SyedEngineerCommented:
BTW, row 2 in your example is Invalid because there is a space within.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rgonzo1971Commented:
If you want in the conventional order then try
Sub macro()

For Each c In Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
    On Error Resume Next
    strRngAddress = Range(c.Text).Address(0, 0)
    On Error GoTo 0
    If UCase(c) = strRngAddress And Evaluate("=ISREF(" & c.Text & ")") Then
        c.Offset(, 1) = "Valid"
    Else
        c.Offset(, 1) = "Invalid"
    End If
Next
End Sub

Open in new window

0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Or you may give this User Defined Function...

Function IsRangeValid(Str As String) As String
Dim Rng As Range

On Error Resume Next
Set Rng = Range(Str)
On Error GoTo 0
If Not Rng Is Nothing Then
    If LCase(Left(Rng.Address(0, 0), 1)) = LCase(Left(Str, 1)) Then
        IsRangeValid = "Valid"
    Else
        IsRangeValid = "Invalid"
    End If
Else
    IsRangeValid = "Invalid"
End If
End Function

Open in new window

and then try it on the sheet like this...
In C1
=IsRangeValid(B1)

Open in new window

0
Rgonzo1971Commented:
@ Neeraj the condition at line 8 gives a valid

for "az10:ab10" as well for "ab100:ab10" both invalid according to question
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
@Rgozo

Yes, you are right. I only compared the first letter. Thanks for pointing that out. :)

You may convert your sub-routine to UDF so that OP can use it on the sheet.
0
Rgonzo1971Commented:
my macro as UDF
Function fIsRangeValid(Str As String) As String
    On Error Resume Next
    strRngAddress = Range(Str).Address(0, 0)
    On Error GoTo 0
    If UCase(Str) = strRngAddress And Evaluate("=ISREF(" & Str & ")") Then
        fIsRangeValid = "Valid"
    Else
        fIsRangeValid = "Invalid"
    End If
End Function

Open in new window

0
ouestqueAuthor Commented:
Great Answers! Thank you everyone!
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome! Glad we could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.