ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Data Validation (https://www.excelbanter.com/excel-programming/351345-data-validation.html)

cvach

Data Validation
 
Can you perform data validation for the worksheets within a work book?
For example, if I type in a cell a worksheet name, can excel tell me if
the name I typed in matches a worksheet name in the workbook? If so
How?


Dave Peterson

Data Validation
 
If you had that list of worksheet names somewhere, you could use that in the
data|validation rules.

Or you could use a macro (worksheet_change event) to do the validation, too.

This checks to see if there is a worksheet with the name you typed in A1.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

Dim wks As Worksheet

If Target.Cells.Count 1 Then Exit Sub
If Intersect(Target, Me.Range("a1")) Is Nothing Then
Exit Sub
End If

Set wks = Nothing
On Error Resume Next
Set wks = Me.Parent.Worksheets(CStr(Target.Value))
On Error GoTo 0

On Error GoTo errHandler:

If wks Is Nothing Then
'invalid name
MsgBox "Not valid"
With Application
.EnableEvents = False
.Undo 'set it back
End With
End If

errHandler:
Application.EnableEvents = True

End Sub

Rightclick on the worksheet tab that should have this behavior. Select view
code and paste that code into the code window.

Then back to excel to test it out.

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

If you want to read more about these kinds of events:

Chip Pearson's site:
http://www.cpearson.com/excel/events.htm

David McRitchie's site:
http://www.mvps.org/dmcritchie/excel/event.htm

cvach wrote:

Can you perform data validation for the worksheets within a work book?
For example, if I type in a cell a worksheet name, can excel tell me if
the name I typed in matches a worksheet name in the workbook? If so
How?


--

Dave Peterson


All times are GMT +1. The time now is 05:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com