且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解锁和锁定Excel单元格以运行宏

更新时间:2023-10-05 08:22:52

在您之后:

MsgBox (CStr(iRows) + " records have been retrieved from the database!")

放置此代码:

Dim rngCol As Range ' Create a Range object

Set rngCol = ActiveSheet.Range("A:Z") ' Select all columns on sheet
   rngCol.Locked = False              ' Unlock all columns
Set rngCol = ActiveSheet.Range("A:I") ' Now Select columns EmpID - Status
   rngCol.Locked = True               ' Lock only those columns
   ActiveSheet.Protect                ' Protect will now only protect the Locked columns

当您进入需要重写所有内容的过程时:

When you enter the procedure where you need to rewrite everything:

Public Sub RetrieveDBToWorkSheet()

放置以下代码以解锁整个工作表:

Place this code to unlock the whole sheet:

ActiveSheet.Unprotect  ' This will unprotect the whole sheet

您必须记住要先解锁工作表上的所有列.如果您不这样做,那么保护功能将锁定您的所有列(即使您专门锁定"了您想要的范围).这是不直观的,并且使许多excel用户感到困惑.

You have to remember to unlock all the columns on the sheet first. If you don't, then the protect will lock down all of your columns (even though you specifically "locked" the range you want). This is unintuitive and has baffled many excel users.