简体   繁体   English

excel vba:将表格单元格设置为只读

[英]excel vba: set a table cell to read only

Is it possible to set a specific cell in a table to 'Read-Only'? 是否可以将表中的特定单元格设置为“只读”? Or, set a column in the table to 'Read-Only'? 或者,将表中的列设置为“只读”? This is the idea of what I am trying to do: 这是我想要做的事情的想法:

Sub TableInsertRow()
    Dim oLo As ListObject
    Dim oNewRow As ListRow

    Set oLo = ActiveSheet.ListObjects("Table1")

    oLo.TableStyle = "tablestylelight3"
   'insert below

    Set oNewRow = oLo.ListRows.Add(AlwaysInsert:=True)

    oNewRow.Range.Cells(1, 1).Value = "Value For New cell"
    oNewRow.Range.Cells(1, 5).ReadOnly = True
    oNewRow.Range.Cells(1, 6).ReadOnly = True
    oNewRow.Range.Cells(1, 7).ReadOnly = True

End Sub

The last 3 lines are clearly not proper syntax, rather the idea I am trying to accomplish. 最后3行显然不是正确的语法,而是我想要完成的想法。 Or, if the table's columns can be set to read only, that will work as well. 或者,如果表的列可以设置为只读,那么也可以。

Rich 丰富

You can use this basic script to make some cells range ReadOnly : 您可以使用此基本脚本使一些单元格范围为ReadOnly

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(ActiveCell, Range("A2:B4")) Is Nothing Then 'We make A2:B4 range readonly
Range("A1").Select 'when user selects some cell from the range above
MsgBox ("These cells are Readonly,So you can not Change") 'He will receive this massage
End If
End Sub

Just open VB editor ( ALT + F11 ) choose sheet you want to make a read only cells on - double-click, paste this code. 只需打开VB编辑器( ALT + F11 ),选择要打开只读单元格的工作表 - 双击,粘贴此代码。

Taken long time ago from here 从很久以前就从这里开始了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM