انتقل إلى المحتوى الرئيسي

كيفية قفل الخلايا المحددة دون حماية ورقة العمل بأكملها في إكسيل؟

عادة ، تحتاج إلى حماية ورقة العمل بأكملها لتأمين الخلايا من التحرير. هل هناك أي طريقة لقفل الخلايا دون حماية ورقة العمل بأكملها؟ توصي هذه المقالة بطريقة VBA لك.

تأمين الخلايا المحددة دون حماية ورقة العمل بأكملها باستخدام VBA


تأمين الخلايا المحددة دون حماية ورقة العمل بأكملها باستخدام VBA

لنفترض أنك بحاجة إلى قفل الخلية A3 و A5 في ورقة العمل الحالية ، فإن رمز VBA التالي سيساعدك على تحقيق ذلك دون حماية ورقة العمل بأكملها.

1. انقر بزر الماوس الأيمن فوق علامة تبويب الورقة ، وحدد عرض الرمز من قائمة النقر بزر الماوس الأيمن.

2. ثم انسخ والصق رمز فبا أدناه في نافذة التعليمات البرمجية. انظر لقطة الشاشة:

رمز فبا: تأمين الخلايا المحددة دون حماية ورقة العمل بأكملها

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 Then
        If Target.Row = 3 Or Target.Row = 5 Then
            Beep
            Cells(Target.Row, Target.Column).Offset(0, 1).Select
        End If
    End If
End Sub

ملاحظات: في الكود ، عمود 1, الصف = شنومكس و الصف = شنومكس الإشارة إلى أنه سيتم تأمين الخلية A3 و A5 في ورقة العمل الحالية بعد تشغيل الكود. يمكنك تغييرها كما تريد.

3. اضغط على قديم + Q مفاتيح في نفس الوقت لإغلاق ميكروسوفت فيسوال باسيك للتطبيقات نافذة.

تم الآن تأمين الخلية A3 و A5 في ورقة العمل الحالية. إذا حاولت تحديد الخلية A3 أو A5 في ورقة العمل الحالية ، فسيتم نقل المؤشر إلى الخلية المجاورة اليمنى تلقائيًا.


مقالات ذات صلة:

أفضل أدوات إنتاجية المكتب

🤖 مساعد Kutools AI: إحداث ثورة في تحليل البيانات على أساس: التنفيذ الذكي   |  إنشاء التعليمات البرمجية  |  إنشاء صيغ مخصصة  |  تحليل البيانات وإنشاء الرسوم البيانية  |  استدعاء وظائف Kutools...
الميزات الشعبية: البحث عن التكرارات أو تمييزها أو تحديدها   |  حذف الصفوف الفارغة   |  دمج الأعمدة أو الخلايا دون فقدان البيانات   |   جولة بدون صيغة 
سوبر بحث: معايير متعددة VLookup    VLookup ذات القيمة المتعددة  |   VLookup عبر أوراق متعددة   |   بحث غامض ....
قائمة منسدلة متقدمة: إنشاء القائمة المنسدلة بسرعة   |  القائمة المنسدلة التابعة   |  قائمة منسدلة متعددة التحديد ....
مدير العمود: إضافة عدد محدد من الأعمدة  |  نقل الأعمدة  |  تبديل حالة رؤية الأعمدة المخفية  |  مقارنة النطاقات والأعمدة 
الميزات المميزة: التركيز على الشبكة   |  عرض تصميم   |   شريط الفورمولا الكبير    مدير المصنفات والأوراق   |  مكتبة الموارد (النص السيارات)   |  منتقي التاريخ   |  اجمع أوراق العمل   |  تشفير/فك تشفير الخلايا    إرسال رسائل البريد الإلكتروني عن طريق القائمة   |  سوبر تصفية   |   مرشح خاص (تصفية غامق / مائل / يتوسطه خط ...) ...
أفضل 15 مجموعة أدوات12 نص الأدوات (إضافة نص, إزالة الأحرف، ...)   |   +50 رسم الأنواع (مخطط جانت، ...)   |   40+ عملي الصيغ (احسب العمر على أساس تاريخ الميلاد، ...)   |   19 إدخال الأدوات (أدخل رمز الاستجابة السريعة, إدراج صورة من المسار، ...)   |   12 تحويل الأدوات (أرقام إلى كلمات, نتيجة تحويل عملة، ...)   |   7 دمج وتقسيم الأدوات (الجمع بين الصفوف المتقدمة, تقسيم الخلايا، ...)   |   ... و اكثر

عزز مهاراتك في Excel باستخدام Kutools for Excel، واختبر كفاءة لم يسبق لها مثيل. يقدم Kutools for Excel أكثر من 300 ميزة متقدمة لتعزيز الإنتاجية وتوفير الوقت.  انقر هنا للحصول على الميزة التي تحتاجها أكثر...

الوصف


يجلب Office Tab الواجهة المبوبة إلى Office ، ويجعل عملك أسهل بكثير

  • تمكين التحرير والقراءة المبوبة في Word و Excel و PowerPointوالناشر والوصول و Visio والمشروع.
  • فتح وإنشاء مستندات متعددة في علامات تبويب جديدة من نفس النافذة ، بدلاً من النوافذ الجديدة.
  • يزيد من إنتاجيتك بنسبة 50٪ ، ويقلل مئات النقرات بالماوس كل يوم!
Comments (22)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Is there a way for this VBA code to run automatically every time someone opens the file?
This comment was minimized by the moderator on the site
Hi Aaron,After adding the code, please save the workbook as an Excel Macro-Enabled Workbook (click File > Save As > specify a folder for the file > choose Excel Macro-Enabled Workbook from the Save as type drop-down list > Save). After that, every time when you open the file, the code works automatically.
This comment was minimized by the moderator on the site
I have try cannot, Hide formula without protecting.
This comment was minimized by the moderator on the site
Hi,

I have tried to use the code for ranges you have previously posted but it isn't working - can you advise me if I should be combining the code for ranges above or below?


Thanks
This comment was minimized by the moderator on the site
Hello,

I used the code provided to Carlos and it did exactly what I wanted it to. Is there a way to have some ranges offset within the ROW to the right of the protected range (as the "Carlos" code already does), but have other ranges offset within the COLUMN to the cell directly below the protected range? I tried entering the "Carlos" code twice and changing the offset, but I received a variety of errors.

Thank you
This comment was minimized by the moderator on the site
Hi Charlie,
Sorry can't help you with that yet. Thank you for your comment.
This comment was minimized by the moderator on the site
Is there any function to set for example Row 2 cell 13 to 900? Or do i need to manually punch each cell name in the code?
This comment was minimized by the moderator on the site
Bonjour,
Comment faire pour verrouiller de la cellule B8 à B10000?
D'avance merci de votre réponse.
Christophe
This comment was minimized by the moderator on the site
Dear Crystal,

Thank you so much for this! It works perfectly.

I have used the code in Carlos' comment to run the macro automatically as you open the file. I was wondering if there is a way of having an "Undo" button or something like it, that allows you to cancel that code and therefore, unlock those ranges that have been locked.

I want those to be locked most of the time, but I would like to unlock them should I need to change any of the data. The only reason I am not protecting the whole sheet is because, if I do, it deactivates the ability to expand on tables. That in turn, expand on a line chart.

Thanks so much for the help!
This comment was minimized by the moderator on the site
Hi Erin,
You can manually break the code by clicking the Break button in the Microsoft Visual Basic for Applications window to unlock those ranges. And run the code to activate it again. Thanks for your comment.
This comment was minimized by the moderator on the site
Dear Crystal,

Is there anyway that this macro can be run automatically upon opening the file, or upon clicking any cell. I don't want people to have to run the macro manually in order for the required ranges to be protected.

Also, I will need to protect 12 non adjacent ranges. For example: I11:I20 and K11:K20 and M11:20 etc... How do I do this?

Last, and this one might be asking too much, but is it possible to apply the protection to the example ranges that I provided above, BUT then extend the protection down additional rows as new ones are added? In other words, protection would be applied to I11:I20 and K11:K20 and M11:20, but a user would be able to add a new row (row 21), with new data, but once the new row is added, then the protection would apply to I11:I21 and K11:K21 and M11:21. Am I asking for the moon? :-)

Thank you for everything that you've already provided! I can't thank you and other people like you enough for sharing the your knowledge. Amazing!
This comment was minimized by the moderator on the site
Dear Carlos,
The following VBA code can help you solving the problem. Please fill in your ranges in the fourth line of the code and press the Alt + Q keys to return to the worksheet. Then please shift to other worksheet and then go back to current sheet to activate the code. Thank you for your comment.

Dim xRg As Range
Private Sub Worksheet_Activate()
If xRg Is Nothing Then
Set xRg = Union(Range("I10:I20"), Range("K10:K20"), Range("M10:M20"), Range("O10:O20"))
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim I As Integer
Dim xRgNew As Range
Dim xRgLCell As Range
On Error Resume Next
Application.EnableEvents = False
For I = 1 To xRg.Areas.Count
Set xRgLCell = xRg.Areas.Item(I)
Set xRgLCell = xRgLCell(xRgLCell.Count).Offset(1, 0)
If Target.Address = xRgLCell.Address Then
If xRgNew Is Nothing Then
Set xRgNew = Target
Else
Set xRgNew = Union(xRgNew, Target)
End If
End If
Next
Set xRg = Union(xRg, xRgNew)
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo Exitsub
If (Not Intersect(xRg, Target) Is Nothing) And (Target.Count = 1) Then
Target.Offset(0, 1).Select
End If
Exitsub:
End Sub
This comment was minimized by the moderator on the site
Dear Crystal,

You have provided me with a solution (half of it) that I have been struggling for the last few weeks, but I would need some more hints.

How this could be applied to a table range only, not whole worksheet? Thanks in advance.
This comment was minimized by the moderator on the site
Good Day,
As the VBA code provided below, please change the specified range "H:J,4:46" to the table range you only want to lock in the worksheet.
And cells(1,1) should be the cell outside the table range. When clicking on any cell in the table range, the cursor will be moved to that cell automatically.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRg As Range
Dim xRgEx As Range
Dim xRgExEach As Range
On Error Resume Next
Application.ScreenUpdating = False
Set xRg = Range("H:J,4:46") 'Change the row range and column range you will lock without protecting worksheet
Set xRgEx = Application.Intersect(xRg, Target)
If xRgEx Is Nothing Then Exit Sub
Cells(1, 1).Select 'Specify a cell you will shift to after selecting the locked cells
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
Once you have run the VBA to lock those cells, how do you unlock them?
Do you have to have a VBA code to unlock?
This comment was minimized by the moderator on the site
Hi RENICK,

We do not provide the unlocked VBA code, manually break the code then the area can be edited normally.
This comment was minimized by the moderator on the site
how could i lock a range of rows, say 4-46 and columns 8 and 10
This comment was minimized by the moderator on the site
Dear AJ,
If you want to lock a range of specified rows and columns, please try the below VBA script.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRg As Range
Dim xRgEx As Range
Dim xRgExEach As Range
On Error Resume Next
Application.ScreenUpdating = False
Set xRg = Range("H:J,4:46") 'Change the row range and column range you will lock without protecting worksheet
Set xRgEx = Application.Intersect(xRg, Target)
If xRgEx Is Nothing Then Exit Sub
Cells(1, 1).Select 'Specify a cell you will shift to after selecting the locked cells
Application.ScreenUpdating = True
End Sub
This comment was minimized by the moderator on the site
This is a great workaround, especially in shared workbooks where turning protection on/off is unsupported. Thank you so much.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations