使用VBA锁定Excel中的行 [英] Lock rows in Excel using VBA

查看:107
本文介绍了使用VBA锁定Excel中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel表格,其中A至F列由不同的用户填写.完成一行后,另一位用户(控制用户)在G列中输入完成".一旦用户在列G中输入完成",我希望使用VBA脚本锁定整行(从A到G列),以便没有人可以再更改任何该行条目.使用VBA脚本可以做到这一点吗?

I have an Excel sheet with columns A to F to be filled out by different users. Once one row is completed another user (control user) enteres "done" in column G. As soon as the user entered "done" in coulmn G, I want a VBA script to lock the entire row (column A to G) so that no one can change any of that row entries any longer. Is that possible using VBA scripting?

推荐答案

我们必须从所有不受保护的单元格开始 和工作表 Locked

We must start with all cells un-protected and the sheet Locked

在工作表代码区域中输入以下事件宏:

Enter the following Event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim N As Long
    N = Target.Row
    If Intersect(Target, Range("G:G")) Is Nothing Then Exit Sub
    If Target.Text <> "Done" Then Exit Sub
    ActiveSheet.Unprotect
      Range("A" & N & ":G" & N).Locked = True
    ActiveSheet.Protect
End Sub

由于它是工作表代码,因此非常易于安装和自动使用:

Because it is worksheet code, it is very easy to install and automatic to use:

  1. 右键单击Excel窗口底部附近的标签名称
  2. 选择查看代码-这将打开一个VBE窗口
  3. 将内容粘贴并关闭VBE窗口

如果您有任何疑问,请先在试用工作表上尝试一下.

If you have any concerns, first try it on a trial worksheet.

如果保存工作簿,则宏将随其一起保存.如果要在2003年以后使用Excel版本,则必须保存该文件为.xlsm而不是.xlsx

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

要删除宏,请执行以下操作:

To remove the macro:

  1. 如上所述调出VBE窗口
  2. 清除代码
  3. 关闭VBE窗口

要全面了解有关宏的更多信息,请参见:

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

http://msdn.microsoft.com/en-us/library/ee814735(v = office.14).aspx

要了解有关事件宏(工作表代码)的更多信息,请参阅:

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

必须启用宏才能使其正常工作!

这篇关于使用VBA锁定Excel中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆