跳到主要内容

如何防止在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中的复选框轻松创建下拉列表:

带有复选框的下拉列表 实用程序 Kutools for Excel 可以帮助您根据需要在指定范围,当前工作表,当前工作簿或所有打开的工作簿中轻松创建带有复选框的下拉列表。
立即下载 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 Tab 为 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