任何人都知道如何禁用excel单元格中的编辑属性?
我想要的是,如果用户直接点击(而不是双击)该单元格,它会提示一条消息:
您不能编辑内容!
我有这段代码:
Private Sub Worksheet_SelectionChange(ByVal Target As Range, Cancel As Boolean)
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
If Not Intersect(Target, Range("C21:D" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)) Is Nothing Then
Cancel = True
MsgBox "You are not allowed to edit the content!", vbCritical + vbOKOnly
End If
End Sub
但它返回错误说:
答案 0 :(得分:2)
我终于这样做了:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ws As Worksheet
Dim lastRow As Long
Set ws = ThisWorkbook.Worksheets("Sheet1")
lastRow = Cells(Rows.Count, 3).End(xlUp).Row + 1
If Not Intersect(Target, Range("C1:D" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)) Is Nothing Then
Application.ScreenUpdating = False
Set ProtectedCell = Target
MsgBox "Access Denied", vbExclamation, "Access Status"
Cells(lastRow, 3).Select
Application.ScreenUpdating = True
End If
感谢大家的帮助和想法!
答案 1 :(得分:1)
您不需要VBA,这已经是Excel内置的功能。
只需保护表格(有或没有密码);所有“锁定”的单元格将不再可编辑。在“保护”选项卡中使用单元格属性(Ctrl + 1)更改锁定的单元格。
您可以使用Worksheet.Protect
/ Worksheet.Unprotect
方法以编程方式切换工作表保护。
答案 2 :(得分:0)
您需要为SelectionChange定义子
在代码段中尝试使用package stackoverflow;
import java.util.ArrayList;
public class PalindromeWierd {
public static void main(String[] args) {
//palim contains a palindrome word -> madam
ArrayList<Character> palim = new ArrayList<Character>();
palim.add('m');
palim.add('a');
palim.add('d');
palim.add('a');
palim.add('m');
System.out.println("Palim with vowels: ");
for (Character character : palim) {
System.out.print(character);
}
System.out.println();
ArrayList<Character> palimWithoutVowels = new ArrayList<Character>();
for (Character character : palim) {
if (character != 'a' && character != 'e' && character != 'i'
&& character != 'o' && character != 'u') {
palimWithoutVowels.add(character);
}
}
System.out.println("Palim without vowels: ");
for (Character character : palimWithoutVowels) {
System.out.print(character);
}
System.out.println();
}
}
代替SelectionChange
答案 3 :(得分:0)
如果你这样做......
Option Explicit
Public OriginalCell As Range
Public ProtectedCell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set OriginalCell = [A5]
If Not Intersect(Target, [A1:B1]) Is Nothing Then
Application.ScreenUpdating = False
Set ProtectedCell = Target
MsgBox "Access Denied", vbExclamation, "Access Status"
OriginalCell.Select
Application.ScreenUpdating = True
Else
Set OriginalCell = Target
End If
End Sub
单击,双击或选择工作表提示MsgBox
修改强>
Option Explicit
Public OriginalCell As Range
Public ProtectedCell As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set OriginalCell = Range("C22")
If Not Intersect(Target, Range("C21:D" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)) Is Nothing Then
Application.ScreenUpdating = False
Set ProtectedCell = Target
MsgBox "Access Denied", vbExclamation, "Access Status"
OriginalCell.Select
Application.ScreenUpdating = True
Else
Set OriginalCell = Target
End If
End Sub