Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2369|回复: 3

[已解决]紧急求助!

[复制链接]
发表于 2016-10-30 10:51 | 显示全部楼层 |阅读模式
本帖最后由 huchuanxing 于 2016-10-30 16:03 编辑

工作表中添加以下VBA代码后,在单元格输入内容后即会自动锁定,我想可否把它修改一下,让工作簿中的其它工作表也能在输入内容后自动锁定?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    With Target.Range("a1")
        If .Value <> "" Then
            PW = InputBox("修改内容请输入密码:")
            If PW <> "123" Then
                Cells(1, 1).Select
            Else
                Exit Sub
            End If
        End If
    End With
End Sub


最佳答案
2016-10-30 12:28
这样修改一下可能更严谨一些
  1. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  2.     On Error Resume Next
  3.     Application.EnableEvents = False
  4.     With Target.Range("a1")
  5.         If .Value <> "" Then
  6.             PW = InputBox("修改内容请输入密码:")
  7.             If PW <> "123" Then
  8.                 Cells(Cells(Rows.Count, .Column).End(3).Row + 1, .Column).Select
  9.             End If
  10.         End If
  11.     End With
  12.     Application.EnableEvents = True
  13. End Sub
复制代码
发表于 2016-10-30 12:04 | 显示全部楼层
以下代码放到 ThisWorkbook 模块中
  1. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  2.     On Error Resume Next
  3.     Application.EnableEvents = False
  4.     With Target.Range("a1")
  5.         If .Value <> "" Then
  6.             PW = InputBox("修改内容请输入密码:")
  7.             If PW <> "123" Then
  8.                 Cells(1, 1).Select
  9.             End If
  10.         End If
  11.     End With
  12.     Application.EnableEvents = True
  13. End Sub
复制代码
回复

使用道具 举报

发表于 2016-10-30 12:28 | 显示全部楼层    本楼为最佳答案   
这样修改一下可能更严谨一些
  1. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  2.     On Error Resume Next
  3.     Application.EnableEvents = False
  4.     With Target.Range("a1")
  5.         If .Value <> "" Then
  6.             PW = InputBox("修改内容请输入密码:")
  7.             If PW <> "123" Then
  8.                 Cells(Cells(Rows.Count, .Column).End(3).Row + 1, .Column).Select
  9.             End If
  10.         End If
  11.     End With
  12.     Application.EnableEvents = True
  13. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-10-30 15:56 | 显示全部楼层
雪舞子 发表于 2016-10-30 12:28
这样修改一下可能更严谨一些

非常感谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-6 03:38 , Processed in 0.262930 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表