XGuarden

Private Function GetValidRange(ByVal validation As Excel.Validation, ByVal myrange As Excel.Range) As Boolean

How to check if the data is ok, with the parameter validation

Return "true or false"

End Function

Private Function GetValidText(ByVal validation As Excel.Validation, ByVal mystring As string) As Boolean

How to check if the data is ok, with the parameter validation

Return "true or false"

End Function

I found how add validation, remove, but i cant found how check if my data is valid, thank.



Re: Visual Studio Tools for Office Excel.Validation in vb.net, check if data is valid

Andrew Whitechapel - MSFT

This is not a VSTO-specific question. General Excel programming questions should be posted to the appropriate Excel programming forum.

It is also not completely clear why you would want to use the Validation object in this way. For information on the Validation object, you can simply read the Excel VBA documentation. To do this, run Excel, hit Alt-F11 to get the VBA editor, then go to Help to get the Excel development documentation.

Anyway, here is some sample code (in a Windows Forms app) that implements a GetValidRange method according to your question:

Public Class Form1

Dim cell As Excel.Range

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim isValid As Boolean
isValid = GetValidRange(cell.Validation, cell)
MessageBox.Show(isValid.ToString())
End Sub

Private Function GetValidRange(ByVal validation As Excel.Validation, ByVal myrange As Excel.Range) As Boolean
Return validation.Value
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim xl As Excel.Application
Dim book As Excel.Workbook
Dim sheet As Excel.Worksheet

Try
xl = New Excel.Application
xl.Visible = True
book = xl.Workbooks.Add
sheet = xl.ActiveSheet
cell = sheet.Range("a1")

With cell.Validation
.Add(Type:=Excel.XlDVType.xlValidateWholeNumber, _
AlertStyle:=Excel.XlDVAlertStyle.xlValidAlertInformation, _
Operator:=Excel.XlFormatConditionOperator.xlBetween, _
Formula1:="5", Formula2:="10")
.InputTitle = "Integers"
.ErrorTitle = "Integers"
.InputMessage = "Enter an integer from five to ten"
.ErrorMessage = "You must enter a number from five to ten"
End With

Catch ex As Exception
End Try

End Sub
End Class

If you have any further general Excel programming questions, please post them to this forum:

http://msdn.microsoft.com/newsgroups/default.aspx dg=microsoft.public.excel.programming&lang=en&cr=US