Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a program that can create a workbook. Inside a workbook is the Sheet1 and Sheet2. Sheet1 has dropdownlist control created programmatically using vb.net. it's working fine when this line of code is like this:Formula1:="=$A$1:$E$1" but when I change it to Formula1:="=Sheet2$A$1:$E$1" it returns to an error.

This is the error showed:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.VisualBasic.dll
Additional information: Exception from HRESULT: 0x800A03EC


Please help fix this kind of error. Thanks

What I have tried:

My code:


VB
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim xlApp As New Excel.Application
        xlApp.Visible = True
        Dim xlWorkbooks As Excel.Workbooks = xlApp.Workbooks
        Dim xlWorkbook As Excel.Workbook = xlWorkbooks.Add
        Dim xlWorksheet As Excel.Worksheet = CType(xlWorkbook.Worksheets(1), Excel.Worksheet)

       


        Dim xlRng As Excel.Range = xlWorksheet.Cells(5, 5)

        xlRng.Select()  
        With xlApp.Selection.Validation   
            .Add(Type:=Excel.XlDVType.xlValidateList, _
             AlertStyle:=Excel.XlDVAlertStyle.xlValidAlertStop, _
            Operator:=Excel.XlFormatConditionOperator.xlBetween, _
             Formula1:="=Sheet2$A$1:$E$1")
            .IgnoreBlank = True
            .InCellDropdown = True
        End With
    

        With (xlWorkbook) 
            .Worksheets.Add(After:=.Worksheets(1))
            .Sheets("Sheet2").Name = "Sheet2"
        End With
        With xlWorkbook.Worksheets("Sheet2") 
            .Cells(1, 1) = "Jose"
            .Cells(1, 2) = "Juan"
            .Cells(1, 3) = "acronym"
            .Cells(1, 4) = "sector"
            .Cells(1, 5) = "type_acronym"

            .Cells(2, 2) = "(Select Agency Name)"
            .Cells(3, 1) = "2198"
            .Cells(3, 2) = "ABRA STATE INSTITUTE OF SCIENCE AND TECHNOLOGY"
            .Cells(3, 3) = "ASIST"
            .Cells(3, 4) = "State Universities and Colleges"
            .Cells(3, 5) = "Proper"
        End With


        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRng)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorksheet)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbooks)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
    End Sub
Posted
Updated 9-May-19 20:24pm

1 solution

Quote:
How to create dropdownlist with source from another sheet in excel using VB.NET

Since Interop is used to remote control Excel, it have Excel limitations, and Excel (at least on hold versions) do not allow what you want to do (at least directly).
Excel do not allow droplists with an explicit reference to another WorkSheet.
The workaround is to define a name for the range of list and use it as drop formula.
it look like:
VB
xlApp.ActiveWorkbook.Names.Add Name:="MyList", RefersTo:="=Sheet2$A$1:$E$1"
With xlApp.Selection.Validation
    .Add(Type:=Excel.XlDVType.xlValidateList, _
     AlertStyle:=Excel.XlDVAlertStyle.xlValidAlertStop, _
    Operator:=Excel.XlFormatConditionOperator.xlBetween, _
     Formula1:="=MyList")
    .IgnoreBlank = True
    .InCellDropdown = True
End With
 
Share this answer
 
Comments
kyrons 15-May-19 0:58am    
Thanks...solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900