Note: The other languages of the website are Google-translated. Back to English

كيفية تمكين أو تعطيل الزر على أساس قيمة الخلية في إكسيل؟

في هذه المقالة ، سأتحدث عن بعض الطرق لتمكين أو تعطيل الزر استنادًا إلى قيمة الخلية في ورقة عمل Excel.

تمكين أو تعطيل زرين استنادًا إلى بيانات عمودين برمز VBA

قم بتمكين زر أو تعطيله بناءً على قيمتين من الخلايا مع رمز VBA

تمكين أو تعطيل زرين استنادًا إلى بيانات عمودين برمز VBA

على سبيل المثال ، لدي زري أمر في ورقة العمل ، إذا كان العمود A يحتوي على بيانات ، فسيتم تمكين الزر 1 ، ويتم تعطيل الزر 2 ؛ إذا كان العمود C يحتوي على بيانات ، فسيتم تمكين الزر 2 ، ويتم تعطيل الزر 1. لحل هذه المهمة ، قد يساعدك رمز VBA التالي.

1. انقر بزر الماوس الأيمن فوق الورقة التي تحتوي على الأزرار ، ثم اختر عرض الرمز من قائمة السياق ، انظر لقطة الشاشة:

2. في ميكروسوفت فيسوال باسيك للتطبيقات نافذة ، يرجى نسخ ولصق الكود أدناه في الوحدة الفارغة:

رمز فبا: تمكين أو تعطيل زرين بناء على عمودين:

Private Sub Worksheet_Change(ByVal Target As Range)
    CommandButton1.Enabled = False
    CommandButton2.Enabled = False
    If Application.WorksheetFunction.CountA(Range("C:C")) > 0 Then
        CommandButton2.Enabled = True
    End If
    If Application.WorksheetFunction.CountA(Range("A:A")) > 0 Then
        CommandButton1.Enabled = True
    End If
End Sub

Note: In the above code, CommandButton1 and CommandButton2 are the buttons names, A:A and C:C are the two columns that you use to enable or disable two buttons, please change them to your need.

3. Then save and close the code window, now, you will get the following result as you need:

(1.) If type data in Column A, the button 1 is enabled and button 2 is disabled, see screenshot:

(2.) If type data in Column C, the button 2 is enabled and button 1 is disabled, see screenshot:

(3.) Both Column A and Column C have data, two buttons are all enabled, see screenshot:

(4.) Two buttons are all disabled if there is no data in Column A and Column C, see screenshot:

Enable or disable a button based on two cell values with VBA code

If you want to enable or disable a command button based on two cell values, such as, when value in A1 is greater than value in B1, the button is enable, else, the button is disable. The following VBA code also can do you a favor.

1. Right click the sheet that contain the button, and then choose View Code from the context menu, in the Microsoft Visual Basic for Applications window, please copy and paste the below code into the blank module, see screenshot:

VBA code: Enable or disable a button based on two cell values:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TargetAdd As String
    Dim xTemp As Double
    On Error GoTo ExitSub
    TargetAdd = Target.Address
    CommandButton1.Enabled = False
    If (TargetAdd = Range("A1").Address) Or (TargetAdd = Range("B1").Address) Then
        If Range("A1") > Range("B1") Then
            CommandButton1.Enabled = True
        End If
    End If
End Sub

Note: In the above code, A1 and B1 are the cell values that you use, CommanButton1 is the button name, change them to your own.

2. Then save and close the code window, now, if the cell value in A1 is greater than B1, the button is enabled, else, if the cell value in A1 is less than B1, the button is disabled, see screenshot:

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office / Excel 2007-2021 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (1)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I receive a "Expected Function or variable" error when I incorporate this code - could you show the code you set up for the two buttons? May help me solve the error.
There are no comments posted here yet
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations

Follow Us

Copyright © 2009 - | All rights reserved. Powered by ExtendOffice. | Sitemap
Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.
Protected by Sectigo SSL