在列表框填充单元格后修改单元格中的数据

时间:2016-08-31 16:37:55

标签: excel listbox

我双击单元格时会打开一个列表框。当我在列表框中选择多个项目并点击好时,它会将这些项目放在单元格中。但是如果我想要之后转到单元格(选择单元格并点击F2)并修改条目,它将添加我的修改,但也复制列表框之前放入的内容。我只想要修改。如何停止复制?

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lType As Long
Dim strList As String
Application.EnableEvents = False

On Error Resume Next
   lType = Target.Validation.Type
On Error GoTo exitHandler

If lType = 3 Then
   Cancel = True
   strList = Target.Validation.Formula1
   strList = Right(strList, Len(strList) - 1)
  strDVList = strList
  frmDVList.Show
End If

exitHandler:
  Application.EnableEvents = True

End Sub
----------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strSep As String
strSep = "; "
  Application.EnableEvents = False
On Error Resume Next
If Target.Count > 1 Then GoTo exitHandler

Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler

If rngDV Is Nothing Then GoTo exitHandler

If Intersect(Target, rngDV) Is Nothing Then
Else

  newVal = Target.Value
 Application.Undo
 oldVal = Target.Value
 Target.Value = newVal
  If newVal = "" Then
  Else
        If oldVal = "" Then
           Target.Value = newVal
        Else
           Target.Value = oldVal & strSep & newVal
        End If
   End If

End If

exitHandler:
  Application.EnableEvents = True
End Sub

0 个答案:

没有答案