简体   繁体   中英

What will happen when a cell is double clicked in excel?

What will happen when a cell is double clicked in excel?

I want to know this because i have to control entering data in cells in my excel viewer in this case that the user is not allowed to enter any thing in a cell unless he double clicked on it.

I used the following code to protect the active sheet from entering data without double clicking:

myActiveWorksheet.Protect("password", Type.Missing, true, true, Type.Missing, true,
 Type.Missing,
 Type.Missing,
 Type.Missing,
 Type.Missing,
 Type.Missing,
 Type.Missing,
 Type.Missing,
 false, false, true);

It works fine and do what exactly i want.

But the problem occurs just when the user double clicking on a cell.I have wrote the following code in SheetBeforeDoubleClick event handler of the ActiveWorkbook to unprotect the sheet so the user could enter his data in the cell :

myActiveWorksheet.Unprotect("password");
target.Locked = false;

and also the code below in SheetChange event handler to protect it again :

target.Locked = true;
myActiveWorksheet.Protect("password", Type.Missing, true, true, Type.Missing, true,
    Type.Missing,
    Type.Missing,
    Type.Missing,
    Type.Missing,
    Type.Missing,
    Type.Missing,
    Type.Missing,
    false, false, true);

However the second protection code(I mean the one in SheetChange event handler) does not works fine so the user could type in each cells with just one click on it.

I have 2 guesses about the reason:

1)Something happens in double clicking process with excel that causes the sheet will be unprotected.

2)The target parameter of SheetChange event handler does not point to the changed cell.

Ey Val...

I found my solution. I should use

myActiveWorksheet.Cells.Locked = true;

instead of

target.Locked = true;

in SheetChange event handler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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