Excel VBA:限制Excel工作表中的编辑区域

发布于 2024-10-08 01:18:56 字数 672 浏览 9 评论 0原文

我想限制用户在 Ms Excel 工作表中编辑范围。

Scenario:

  | A | B | C | D | E | F | G | H | I | J | ... IV
-------------------------------------------
1 |   |   |   |   |   |   |   |   |   |   |
-------------------------------------------
2 |   |   |   |   |   |   |   |   |   |   |
-------------------------------------------
3 |   |   |   |   |   |   |   |   |   |   |
-------------------------------------------
4 |   |   |   |   |   |   |   |   |   |   |
-------------------------------------------
...
65536

在上面的电子表格中,用户应该有权编辑 Column AColumn H 范围。 Column I to IV 用户不应允许编辑任何文本或任何内容。行数没有限制。

谢谢 :)

I want to restrict the users to edit range in Ms Excel sheet.

Scenario:

  | A | B | C | D | E | F | G | H | I | J | ... IV
-------------------------------------------
1 |   |   |   |   |   |   |   |   |   |   |
-------------------------------------------
2 |   |   |   |   |   |   |   |   |   |   |
-------------------------------------------
3 |   |   |   |   |   |   |   |   |   |   |
-------------------------------------------
4 |   |   |   |   |   |   |   |   |   |   |
-------------------------------------------
...
65536

In the above spreadsheet user should have access to edit the range Column A to Column H. Column I to IV users should not allow to edit any text or anything. No restriction on number of rows.

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苏璃陌 2024-10-15 01:18:56

要以编程方式执行此操作,请在模块中尝试此操作(并对其进行调整以满足您的需求):

Sub ProtectAToH()
Dim ws as Worksheet
For each ws In ActiveWorkbook.Worksheets
  ws.Columns("A:H").Locked = False
  ws.Protect Contents:=True, Password:="myPassword"
Next ws
End Sub

To do this programatically, try this in a module (and adapt it to suit your needs):

Sub ProtectAToH()
Dim ws as Worksheet
For each ws In ActiveWorkbook.Worksheets
  ws.Columns("A:H").Locked = False
  ws.Protect Contents:=True, Password:="myPassword"
Next ws
End Sub
夜光 2024-10-15 01:18:56

分三步

1) 选择整个工作表。格式->锁定单元格->删除所有单元格的锁定。 (默认情况下,所有单元格最初都“锁定”)

2) 选择所需的锁定列。格式->锁定单元格->应用锁定(这是声明性的,您没有锁定任何内容,只是声明下一步要锁定的内容)

3)格式->应用锁定保护工作表。 (这会触发真正的保护)

你就完成了。

华泰

In three steps

1) Select the whole sheet. Format->Lock Cells->Remove locking for all cells. (All cells are initially "locked" by default)

2) Select your desired locking columns. Format->Lock Cells->Apply Locking (This is declarative, you are not locking nothing, just declaring what you will lock in the next step)

3) Format-> Protect Worksheet. (This trigger the real protection)

You are done.

HTH

海的爱人是光 2024-10-15 01:18:56

sasfrog 和 belisarius 提案的一些替代方案(只是为了丰富您的选择):

a)您也可以隐藏列 K:IV,并保护工作表以防止取消隐藏
b) 使用“工具”、“保护”、“允许用户编辑范围”选项,将范围 $a:$h 定义为属于Everyone 组的用户无需密码即可编辑,然后保护您的工作表。我喜欢那个。

不要忘记,任何涉及 Excel 内置保护的解决方案都会阻止您的用户插入/删除行。

c) 使用 VBA(不会阻止删除/插入行):

Private Sub Worksheet_Change(ByVal Target As Range)
  If Not (Intersect(Target, Range("$h:$iv")) Is Nothing) Then
    Target.Value = ""  'or something else'
  End If
End Sub

Some alternatives to sasfrog and belisarius proposals (just to enrich your options):

a) you may also just HIDE columns K:IV, and protect the worksheet to prevent unhiding
b) Using the Tools, Protection, "Allow users to edit ranges" option, define range $a:$h as editable without password for users belonging to Everyone group, then protect your sheet. I like that one.

Don't forget that any solution involving Excel built in protection will prevent your users from inserting/deleting rows.

c) Using VBA (would not prevent deleting/inserting rows):

Private Sub Worksheet_Change(ByVal Target As Range)
  If Not (Intersect(Target, Range("$h:$iv")) Is Nothing) Then
    Target.Value = ""  'or something else'
  End If
End Sub
梦幻的心爱 2024-10-15 01:18:56
ActiveSheet.ScrollArea = "$A:$H"

将限制用户可以选择的单元格。

ActiveSheet.ScrollArea = "$A:$H"

will restrict what cells the user can select.

又爬满兰若 2024-10-15 01:18:56

应修改 iDevlop 的选项 c) 以防止无限循环:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
        If Not Target.Cells.Count > 1 Then                  'Prevent Error on Row Insert or Delete
            If Not Target.Value = "" Then Target.Value = "" 'Prevent infinite loop
        End If
    End If
End Sub

这仍然允许用户将多单元格复制粘贴到指定的相交范围中。还没有弄清楚这一点。

iDevlop's option c) should be modified to prevent an infinite loop:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("A1:G25")) Is Nothing Then
        If Not Target.Cells.Count > 1 Then                  'Prevent Error on Row Insert or Delete
            If Not Target.Value = "" Then Target.Value = "" 'Prevent infinite loop
        End If
    End If
End Sub

This will still allow a user to make a multi-cell copy paste into your specified Intersect range. Havent figured that one out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文