I'm writing a xlsx file directly from vb.net
I have a problem when I try to add a cell custom validation formula with this istruction:
HTML Code:
Sub AddValidationFormula(byval range as range)
dim validationFormula as string = "=mod(C2,F2)=0"
range.Validation.Add(XlDVType.xlValidateCustom, XlDVAlertStyle.xlValidAlertStop, validationFormula)
End Sub
The validationFormula above indicated is an semplified formula that I have try to used to solve my problem.
When I run my code, it crashes with "exception from HRESULT: 0x800A03EC".
I have also tryed the italian Fornula ("=RESTO") but without result.
If I write the formula directly in the Excel file, I don't have any problem.
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
I have tryed using also the semicolon, but I have the same error.
I also tryed a formula using values instead of cells, but the error is always the same
I'm in dubt if I need to specify the formula (only for validation) in italian (RESTO) or english (MOD).
Using the formula without the final "=0" in a single cell formula, I don't have any problem (in this case I have used the italian formula and the FormulaLocal property)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Dim xlApp As New Microsoft.Office.Interop.Excel.Application()
Dim xlWb As Microsoft.Office.Interop.Excel.Workbook
xlWb = xlApp.Workbooks.Open("D:\TestFolder\vbexcel.xlsx")
Dim xlSt As Microsoft.Office.Interop.Excel.Worksheet = CType(xlWb.Worksheets("Tabelle3"), Worksheet)
With xlSt
.Range("G22").FormulaLocal = "=REST(C2;F2)=0"
End With
xlWb.Save()
xlApp.Quit()
xlApp = Nothing
Catch g As Exception
MessageBox.Show(g.ToString)
End Try
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
with the code that you propose, you set a formula in a single cell (in the example, in the cell G22).
I need to add a validation formula (range.validation.add) to this cell, like the Data Validation inserted from Excel using menu data --> data validation.
In another cell, i Have set a data validation data, using the XlDVType.xlValidateTextLength and I don't have any problem.