允许用户在VBA的Excel表格中编辑范围
我有一个工作簿,其中有不同的用户名称(用户login)名称不同的工作表。 现在我已经添加了一个代码,为新用户添加新工作簿。 我想授予同一个用户编辑一定范围的代码。 如果我解锁某些单元格,所有用户将能够编辑此用户表单中的范围。 如何通过vba获得“允许用户编辑范围”的function
Sub add_new_user() newUser = ActiveWorkbook.Sheets("Admin").newUserTextBox.Value struserdn = GetUserFullName(newUser) If struserdn <> "Error" Then answer = MsgBox("Do you want to add the following user?" & vbNewLine & vbNewLine & struserdn & vbNewLine & newUser & "@abcd.com", vbYesNo + vbQuestion, "New User") If answer = vbYes Then Dim newUSheet As Worksheet Worksheets("Example_sheet").Copy Before:=Worksheets("Example_sheet") Set newUSheet = ActiveSheet newUSheet.Name = newUser newUSheet.Unprotect "123" 'need some code here to allow the newUser to edit ranges A4:F10000 newUSheet.protect "123" newUSheet.Rows("A1").Value = struserdn Else Exit Sub End If Else MsgBox "User name not found!", , "Error" Exit Sub End If End Sub
这段代码循环遍历工作簿中的所有工作表,并解锁那些与windows用户名相同的工作表。
编辑:添加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
使用Range("A1:B1").Locked = False
可以在保护模式下将范围标记为解锁。
如果你现在使用类似的东西
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
为了保护片材,则先前的解锁范围可以由用户编辑,并且片材的任何其他范围不能。
如果你的意思是说用户A只能编辑表单A,用户B只能编辑表单B,这是不可能的。