ccravenbartle
asked on
Excel VB.Net Conditional Dropdown Validation problem
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.HorizontalAlign ment = Excel.Constants.xlCenter
With _UnitRange.Cells.Validatio n
.Delete()
.Add(XlDVType.xlValidateLi st, AlertStyle:=XlDVAlertStyle .xlValidAl ertStop, Operator:=XlFormatConditio nOperator. xlBetween, Formula1:="=INDIRECT($B$4" ))
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
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"
Dim _UnitRange As Excel.Range
_UnitRange = osheet.Range("E7:I107")
_UnitRange.HorizontalAlign
With _UnitRange.Cells.Validatio
.Delete()
.Add(XlDVType.xlValidateLi
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
ASKER
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:=xlValidAlertSt op, 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.HorizontalAlign ment = Excel.Constants.xlCenter
With _UnitRange.Cells.Validatio n
.Delete()
.Add(XlDVType.xlValidateLi st, AlertStyle:=XlDVAlertStyle .xlValidAl ertStop, Operator:=XlFormatConditio nOperator. xlBetween, Formula1:="=INDIRECT($B$4) ")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
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:=xlValidAlertSt
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.HorizontalAlign
With _UnitRange.Cells.Validatio
.Delete()
.Add(XlDVType.xlValidateLi
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
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
Others will need to step in.
cheers, teylyn
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.
ASKER
I set B4 to the first of my defined validation values - it is not empty.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your help. I did not have the content of B4 set to a static named range.
If that is your real code, then you are closing the quotes on the Indirect too early. Try this
.Add(XlDVType.xlValidateLi
cheers, teylyn