Results 1 to 6 of 6

Thread: [RESOLVED] Excel Custon Validation Formula

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    7

    Resolved [RESOLVED] Excel Custon Validation Formula

    Hi All,

    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.

    I have attached the stackTrace error

    Can anyone help me?

    Thanks

    Marcello
    Attached Files Attached Files
    Last edited by Semola; Sep 9th, 2022 at 12:35 AM.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Excel Custon Validation Formula

    add a semicolon in your Formula not a comma

    in Germany it would look like this
    Code:
    =REST(12;2)=0
    or

    Code:
    =REST(-3;2)
    would return 1
    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    7

    Re: Excel Custon Validation Formula

    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)

    Marcello

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Excel Custon Validation Formula

    well I tried this this and it worked

    Code:
     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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    7

    Re: Excel Custon Validation Formula

    Hi ChrisE,

    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.

    Thanks.

    Marcello

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    7

    [RESOLVED]: Excel Custon Validation Formula

    I'm resolved.

    I'm rewrite entire class using the Epplus library.
    With this library, I was able to set the formula for validation.

    Thanks to everyone who tried to help me.

    Marcello

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width