[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Excel VB.Net Conditional Dropdown Validation problem

Posted on 2010-03-25
7
Medium Priority
?
932 Views
Last Modified: 2012-05-09
In my VB.Net application, I am generating Excel spreadsheets with conditional drop down validation.

Cell B4 is a drop down list of named ranges, for example Europe{UK, France, Germany}, Asia{India, Pakistan, China}, Africa{Kenya, Libya, Uganda}.  The drop down cells from E7 to I107 pick up the values depending on the selection made in B4 so that if B4 is set to Asia then the drop down list for all the cells from E7 to I107 show India, Pakistan, China.

I've tested the functionality by manually creating the validation drop downs in Excel and recording Macros to help me write the VB.Net code.  However, when I execute the VB code the Formula1:="=INDIRECT($B$4") reference  is raising a  general Excel exception error.  If I take out INDIRECT it executes but the dropdowns do not pick up the range of values.


Dim _UnitRange As Excel.Range
        _UnitRange = osheet.Range("E7:I107")
        _UnitRange.HorizontalAlignment = Excel.Constants.xlCenter
        With _UnitRange.Cells.Validation
            .Delete()
            .Add(XlDVType.xlValidateList, AlertStyle:=XlDVAlertStyle.xlValidAlertStop, Operator:=XlFormatConditionOperator.xlBetween, Formula1:="=INDIRECT($B$4"))
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
0
Comment
Question by:ccravenbartle
  • 3
  • 2
  • 2
7 Comments
 
LVL 50
ID: 28618925
Hi ccravenbartle,

If that is your real code, then you are closing the quotes on the Indirect  too early. Try this

            .Add(XlDVType.xlValidateList, AlertStyle:=XlDVAlertStyle.xlValidAlertStop, Operator:=XlFormatConditionOperator.xlBetween, Formula1:="=INDIRECT($B$4)")


cheers, teylyn
0
 

Author Comment

by:ccravenbartle
ID: 28654951
Hi Teylyn

Sorry, I made a typing error when I was editing the question.  The program code is exactly the same as yours.  When I execute it I get "Exception from HRESULT: 0x800A03EC"

For your information, I based my code on this is the Macro I recorded :

Sub Macro1()
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=INDIRECT($B$4)"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub

and this is the actual VB.Net code :

 Dim _UnitRange As Excel.Range
        _UnitRange = osheet.Range("E7:I107")
        _UnitRange.HorizontalAlignment = Excel.Constants.xlCenter
        With _UnitRange.Cells.Validation
            .Delete()
            .Add(XlDVType.xlValidateList, AlertStyle:=XlDVAlertStyle.xlValidAlertStop, Operator:=XlFormatConditionOperator.xlBetween, Formula1:="=INDIRECT($B$4)")
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = ""
            .InputMessage = ""
            .ErrorMessage = ""
            .ShowInput = True
            .ShowError = True
        End With
0
 
LVL 50
ID: 28658699
Sorry, that was the one glaring thing that sprang to mind from an Excel perspective. For me, .NET is a framework I need to install so I can run certain apps on my lappy, but not something I can develop with.

Others will need to step in.

cheers, teylyn
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 28679085
What is in B4 when you add the validation? I suspect that the validation formula is evaluating to an error, which is causing your problem.
0
 

Author Comment

by:ccravenbartle
ID: 29064927
I set B4 to the first of my defined validation values - it is not empty.


0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 29065106
B4 needs to contain either the name of a static named range (can't be dynamic) or an address string. Is that the case?
Also, if your sheet is protected, you will get an error.
0
 

Author Closing Comment

by:ccravenbartle
ID: 31707350
Thank you for your help.  I did not have the content of B4 set to a static named range.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question