- 帖子
- 4901
- 主題
- 44
- 精華
- 24
- 積分
- 4916
- 點名
- 105
- 作業系統
- Windows 7
- 軟體版本
- Office 20xx
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台北
- 註冊時間
- 2010-4-30
- 最後登錄
- 2025-5-1
               
|
回復 3# bridetobe
試試看是否符合?- Private Sub Worksheet_Change(ByVal Target As Range)
- Dim A As Range
- If Target.Column <> 2 Then Exit Sub
- With 工作表2
- ThisWorkbook.Names.Add "清單", "=OFFSET(" & .Name & "!$A$1,,,COUNTA(" & .Name & "!$A:$A))" '建立動態範圍名稱做為清單
- Set A = .Cells(.Rows.Count, 1).End(xlUp).Offset(1) '清單下一格
- End With
- With Target
- Select Case .Value
- Case "新增"
- newitem = InputBox("輸入新增項目")
- If IsError(Application.Match(newitem, [清單], 0)) Then A.Value = newitem Else MsgBox newitem & "已在清單內": Exit Sub
- With .EntireColumn.Validation
- .Delete
- .Add xlValidateList, , Formula1:="=" & [清單].Address(, , , 1)
- End With
- Target = newitem
- Case "刪除"
- delitem = InputBox("輸入刪除項目")
- Set A = [清單].Find(delitem, lookat:=xlWhole)
- If A Is Nothing Then
- MsgBox delitem & "未在清單內"
- Else
- A.Delete xlShiftUp
- With .EntireColumn.Validation
- .Delete
- .Add xlValidateList, , Formula1:="=" & [清單].Address(, , , 1)
- End With
- End If
- End Select
- End With
- End Sub
複製代碼 |
|