我有一个小问题,我知道如何自动化宏但我不知道怎么做。
我的问题:我有一张excel表,其中包含第8行到结尾的数据以及A到AZ列的数据。 现在在Excel工作表中我想自动运行一个宏,当用户更改黄色单元格时,它的列是固定的(“N”)但是行没有固定,因为用户可以根据需要插入行。 我怎么能这样做。
这是我写的宏:
Sub Row_Locker()
Dim locat As String
Dim colstart As String
Dim colend As String
Dim topath As String
ActiveSheet.Protect Password:="mbt"
ActiveSheet.Unprotect
rlocat = ActiveCell.Row
clocat = ActiveCell.Column
colstart = "N"
colend = InputBox("enter the end column name")
topath = colstart & "8" & ":" & colend & rlocat
Cells.Select
' unlock all the cells
Selection.Locked = False
' next, select the cells (or range) that you want to make read only,
' here I used simply A1
Range(topath).Select
' lock those cells
Selection.Locked = True
' now we need to protect the sheet to restrict access to the cells.
' I protected only the contents you can add whatever you want
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, _
Scenarios:=False, AllowInsertingRows:=True
End Sub
即使任何黄色单元格值发生变化,我也希望宏运行。怎么做。行号不固定。 excel表看起来像这样:我指的是N列和108行的黄色单元格
答案 0 :(得分:0)
这是我写的代码:完美地工作(Matt建议)
Private Sub Worksheet_Change(ByVal Target As Range)
'If Target.Count > 1 Then Exit Sub
On Error Resume Next
Application.EnableEvents = False
If Not Intersect(Target, Range("D8:D500")) Is Nothing Then Target.Offset(0, 1).MergeArea.ClearContents
If Target.Row = Range("rowreq").Row Then
Dim locat As String
Dim colstart As String
Dim colend As String
Dim topath As String
ActiveSheet.Protect Password:="mbt"
ActiveSheet.Unprotect
rlocat = ActiveCell.Row
clocat = ActiveCell.Column
colstart = "N"
colend = InputBox("enter the end column name")
topath = colstart & "8" & ":" & colend & rlocat
Cells.Select
' unlock all the cells
Selection.Locked = False
' next, select the cells (or range) that you want to make read only,
' here I used simply A1
Range(topath).Select
' lock those cells
Selection.Locked = True
' now we need to protect the sheet to restrict access to the cells.
' I protected only the contents you can add whatever you want
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowInsertingRows:=True
End If
Application.EnableEvents = True
End Sub