簡體   English   中英

excel 數據驗證 同一單元格內多選,逗號分隔

[英]excel data validation multiple selection within the same celle separated by comma

我有一個 excel 電子表格,我需要從列表中插入數據驗證,到目前為止這不是問題,但我需要能夠 select 多個條目而不會覆蓋以前的正常數據驗證,因此最終結果將是這樣的:

列表 數據驗證結果
芒果 蘋果、芒果、像素
IPhone 像素,蘋果
像素
蘋果 蘋果、芒果
芒果 蘋果、芒果、像素
IPhone 像素,蘋果
像素

我在網上找到了一個 VBA 代碼插入到我的電子表格中,以獲取多項選擇而不重復:

Private Sub Worksheet_Change(ByVal Target As Range)
'UpdatebyExtendoffice20180510
    Dim I As Integer
    Dim xRgVal As Range
    Dim xStrNew As String
    Dim xStrOld As String
    Dim xFlag As Boolean
    Dim xArr
    On Error Resume Next
    Set xRgVal = Cells.SpecialCells(xlCellTypeAllValidation)
    If (Target.Count > 1) Or (xRgVal Is Nothing) Then Exit Sub
    If Intersect(Target, xRgVal) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    xFlag = True
    xStrNew = " " & Target.Value & ","
    Application.Undo
    xStrOld = Target.Value
    If InStr(1, xStrOld, xStrNew) = 0 Then
        xStrNew = xStrNew & xStrOld & ""
    Else
        xStrNew = xStrOld
    End If
    Target.Value = xStrNew
    Application.EnableEvents = True
End Sub

它有點工作,但我有兩個問題:

  1. 我可以從我的數據中選擇 select 但結果是這樣
列表 數據驗證結果
芒果 蘋果,芒果,像素,

最后的逗號

  1. 如果選擇錯誤,我將無法刪除或清空該字段,我需要在該單元格上使用 Erase all function 然后使用下拉菜單 function 從目前尚未完成的空單元格中重新擴展數據驗證字段

我對 VBA 不熟悉,因此不勝感激。

我主要使用 R 和 SQL 這是我需要為我辦公室的另一個人執行的任務,該人將使用此電子表格並且需要以最低的難度使用此 ZC1C425268E68385D1AB5074C17A94F1。

有什么建議么?

我已經修改了代碼以添加空格和逗號,僅當它實際上需要將 2 個字符串連接在一起時。 所以第一個值沒有附加逗號,直到第二個值也被選中。

我還對其進行了修改以允許清除單元格。 現在按 Delete 將正確地允許用戶清除單元格。

Private Sub Worksheet_Change(ByVal Target As Range)
'UpdatebyExtendoffice20180510
    Dim I As Integer
    Dim xRgVal As Range
    Dim xStrNew As String
    Dim xStrOld As String
    Dim xFlag As Boolean
    Dim xArr
    On Error Resume Next
    Set xRgVal = Cells.SpecialCells(xlCellTypeAllValidation)
    If (Target.Count > 1) Or (xRgVal Is Nothing) Then Exit Sub
    If Intersect(Target, xRgVal) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    xFlag = True
    xStrNew = Target.Value
    Application.Undo
    xStrOld = Target.Value
    If xStrNew <> "" Then
        If InStr(1, xStrOld, xStrNew) = 0 Then
            xStrNew = xStrNew & IIf(xStrOld <> "", ", " & xStrOld, "")
        Else
            xStrNew = xStrOld
        End If
    End If
    Target.Value = xStrNew
    Application.EnableEvents = True
End Sub

我留下了它,以防它被用於未復制到這篇文章的代碼中,但xArr & I已聲明但未使用。 xFlag已聲明並設置為True ,但未在任何表達式中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM