I have a VBA code for selecting multiple values at once from the drop-down list in a cell of an excel sheet. I want to integrate this code with my java code which allows us to select only value at a time from the drop-down list in a cell of an excel sheet using Apache Poi, so that after integration I should finally be able to select multiple values at once from the drop-down list in a cell of an excel sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$C$2" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
Java code for selecting only value at a time from the drop-down list in a cell of an excel sheet using Apache Poi
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet)sheet);
String[] arrayList = allowedValues.toArray(new String[allowedValues.size()]);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(arrayList);
CellRangeAddressList addressList = new CellRangeAddressList(row.getRowNum() + 1, 10000, cell.getColumnIndex(), cell.getColumnIndex());
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
validation.setShowErrorBox(true);
validation.createErrorBox("ERROR MESSAGE:Invalid Data", "Please provide valid data in the drop down list.");
sheet.addValidationData(validation);