允许用户在VBA中编辑Excel工作表中的范围

时间:2017-03-16 12:08:15

标签: excel excel-vba vba

我有一本工作簿,其中有不同的工作表,其名称为不同用户的用户名(Windows登录名)。现在我添加了一个代码,为新用户添加新工作表。 我想通过创建的新工作表中的代码授予同一用户编辑某些范围的权限。如果我解锁某些单元格,则所有用户都可以编辑此用户工作表中的范围。如何通过vba

获得“允许用户编辑范围”功能
{{1}}

2 个答案:

答案 0 :(得分:1)

这段代码循环遍历工作簿中的所有工作表,并解锁那些与Windows用户名具有相同namne的工作表。

编辑: 在代码中添加了som行。

Dim winUser As String
Dim ws As Worksheet

winUser = Environ("username")

For Each ws In Worksheets

    If ws.Name = winUser Then

        ws.Unprotect     ' You can add a password here

    else

        ws.protect        ' You can add a password here

    End If

Next ws

答案 1 :(得分:0)

使用protected void btnAllocate_Init(object sender, EventArgs e) { DevExpress.Web.ASPxButton btn = (DevExpress.Web.ASPxButton)sender; GridViewDataItemTemplateContainer container = btn.NamingContainer as GridViewDataItemTemplateContainer; // You can remove the if statement, and try to insert a new record. You'll catch an exception, because the DataBinder returns null reference if (container != null && !container.Grid.IsNewRowEditing) btn.Visible = !(bool)DataBinder.Eval(container.DataItem, "PayPlanFlg"); } ,您可以在保护模式下将范围标记为已解锁。

如果您现在使用类似

的内容
Range("A1:B1").Locked = False

保护工作表,然后用户可以编辑以前的解锁范围,而工作表的任何其他范围都不能。

如果您的意思是用户A只能编辑工作表A而用户B只能编辑工作表B,则无法以任何安全的方式进行编辑。