跳到主要內容

如何防止在Excel中使用下拉列表複製和粘貼單元格?

對於具有數據驗證下拉列表的單元格,當從另一個單元格複製內容並將其粘貼到該單元格時,該下拉列表將消失並替換為新的粘貼內容。 在某些情況下,您想防止將其他內容複製和粘貼到帶有下拉列表的單元格上,您將如何處理? 在本文中,您將獲得處理此問題的VBA方法。

防止在Excel中使用下拉列表複製和粘貼單元格


防止在Excel中使用下拉列表複製和粘貼單元格

1.使用要防止複制和粘貼的下拉列表單元格打開工作表。

2。 按 其他 + F11 同時打開 Microsoft Visual Basic應用程序 窗口。

3。 在裡面 Microsoft Visual Basic應用程序 窗口,在左窗格中雙擊當前工作表名稱,然後將下面的VBA代碼複製並粘貼到代碼編輯器中。 看截圖:

VBA代碼:使用下拉列表防止在單元格上複製和粘貼

Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 20200903
    Dim xValue As String
    Dim xCheck1 As String
    Dim xCheck2 As String
    Dim xRg As Range
    Dim xArrCheck1() As String
    Dim xArrCheck2() As String
    Dim xArrValue()
    Dim xCount, xJ As Integer
    Dim xBol As Boolean
'    If Target.Count > 1 Then
'        Exit Sub
'        End If
    xCount = Target.Count
    ReDim xArrCheck1(1 To xCount)
    ReDim xArrCheck2(1 To xCount)
    ReDim xArrValue(1 To xCount)
    Application.EnableEvents = False
    On Error Resume Next
    xJ = 1
    For Each xRg In Target
        xArrValue(xJ) = xRg.Value
         xArrCheck1(xJ) = xRg.Validation.InCellDropdown
        xJ = xJ + 1
    Next

    Application.Undo

    xJ = 1
    For Each xRg In Target
        xArrCheck2(xJ) = xRg.Validation.InCellDropdown
        xJ = xJ + 1
    Next

    xBol = False
    For xJ = 1 To xCount
        If xArrCheck2(xJ) <> xArrCheck1(xJ) Then
            xBol = True
            Exit For
        End If
    Next

    If xBol Then
       MsgBox "The selected cells containg data validation drop-down lists, no pasting allowed."
    Else
        xJ = 1
        For Each xRg In Target
            xRg.Value = xArrValue(xJ)
            xJ = xJ + 1
        Next

    End If

    Application.EnableEvents = True
End Sub

4。 按 其他 + Q 退出鍵 Microsoft Visual Basic應用程序 窗口。

5.當您將內容粘貼到帶下拉列表的單元格時,將彈出一個對話框,提醒您不允許粘貼。 看截圖:

備註:此VBA僅適用於單單元格粘貼。 如果您複製並粘貼到多個單元格中,它將無法正常工作。

使用Excel中的複選框輕鬆創建下拉列表:

帶有復選框的下拉列表 的效用 Excel的Kutools 可以幫助您根據需要在指定範圍,當前工作表,當前工作簿或所有打開的工作簿中輕鬆創建帶有復選框的下拉列表。
立即下載Kutools for Excel的全功能30天免費試用版!


相關文章:

如何在Excel中使用多個複選框創建下拉列表?
許多Excel用戶傾向於創建帶有多個複選框的下拉列表,以便每次都從列表中選擇多個項目。 實際上,您無法使用數據驗證功能創建帶有多個複選框的列表。 在本教程中,我們將向您展示兩種在Excel中創建帶有多個複選框的下拉列表的方法。 本教程提供了解決問題的方法。

在Excel中從另一個工作簿創建下拉列表
在工作簿中的工作表之間創建數據驗證下拉列表非常容易。 但是,如果數據驗證所需的列表數據位於另一個工作簿中,您將怎麼辦? 在本教程中,您將詳細了解如何從Excel中的另一個工作簿創建拖放列表。

在Excel中創建可搜索的下拉列表
對於具有眾多價值的下拉列表,找到合適的價值並非易事。 以前,我們已經介紹了一種在下拉框中輸入第一個字母時自動完成下拉列表的方法。 除了自動完成功能之外,您還可以使下拉列表可搜索,以提高在下拉列表中查找適當值時的工作效率。 為了使下拉列表可搜索,請嘗試本教程中的方法。

在Excel下拉列表中選擇值時自動填充其他單元格
假設您已經根據單元格區域B8:B14中的值創建了一個下拉列表。 在下拉列表中選擇任何值時,都希望在選定單元格中自動填充單元格範圍C8:C14中的相應值。 為了解決該問題,本教程中的方法將對您有所幫助。

下拉列表的更多教程...

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (12)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I works BUT after you put the code you cannot UNDO anything. Not a single step back is allowed.
This comment was minimized by the moderator on the site
No, I did not figure it out. I didn't try to resolve after a while.
This comment was minimized by the moderator on the site
did you manage to resolve the issue of copying and pasting into multiple cells? Thanks in advance!
This comment was minimized by the moderator on the site
Hi Dilip, are you able to fix the issue of allowing data using paste values
This comment was minimized by the moderator on the site
Hi Dilip, are you able to fix the issue of allowing data using paste values..i am also facing the same issue..help me on this please
This comment was minimized by the moderator on the site
Thank you very much !! works fine for me !!! copying into a single cell is restricted. but if i copy a list of cells and paste.., the paste is accepted. let me see if i can tune this up ..thanks anyways
This comment was minimized by the moderator on the site
Jepson, did you manage to resolve the issue of copying and pasting into multiple cells? Thanks in advance!
This comment was minimized by the moderator on the site
Private Sub Worksheet_Change(ByVal Target As Range) Dim xValue As String Dim xCheck1 As String Dim xCheck2 As String If Target.Count > 1 Then Exit Sub End If Application.EnableEvents = False xValue = Target.Value On Error Resume Next xCheck1 = Target.Validation.InCellDropdown On Error GoTo 0 Application.Undo On Error Resume Next xCheck2 = Target.Validation.InCellDropdown On Error GoTo 0 If xCheck1 = xCheck2 Then Target = xValue Else MsgBox "No pasting allowed!" End If Application.EnableEvents = True End Sub This Macro not work when copy from different workbook or worksheet then so please tell me how to avoid these things also
This comment was minimized by the moderator on the site
Hi, above mention vba code is not run.kindly resolve.
This comment was minimized by the moderator on the site
Hi, While I running this macro I got this debug error "Application.Undo". Pls resolve this ASAP
This comment was minimized by the moderator on the site
This works but when I paste value this is allowing data which is not from the drop down list. Any way to restrict any value which is not from the list.
This comment was minimized by the moderator on the site
Hi Dilip, Did you able to resolve the issue, i too having similar issue. I have to allow copy,paste only for drop down values in the data validation cell other i have to restrict. Can you share your solution. Thanks Rao
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations