عناصر تحكم VBA في Excel: النماذج و ActiveX

⚡ ملخص ذكي

VBA Controls in Excel are the interactive elements, such as buttons, check boxes, and text boxes, that build a graphical user interface. This page explains Form controls, ActiveX controls, how the two families differ, and a step-by-step ActiveX command button example.

  • 🧩 GUI Controls: Buttons, combo boxes, check boxes, and labels form the interface a user interacts with on a worksheet.
  • 🗂️ ضوابط النموذج: Simple controls that run a single assigned macro and work on both Windows and Mac Excel.
  • ⚙️ عناصر تحكم ActiveX: Richer controls with many properties and events, coded in VBA, available on Windows Excel only.
  • 🔧 المتطلبات المسبقة: Enable the Developer ribbon and save the file as a macro-enabled workbook before adding controls.
  • ▶ ️ ActiveX Example: A command button prints the string Guru99 VBA Tutorial into cell range A1 to A10 on click.
  • 🖱️ الأحداث: ActiveX controls respond to events such as Click, Change, and GotFocus through dedicated subroutines.

VBA Controls in Excel

عناصر تحكم VBA: التحكم في نموذج VBA وعناصر تحكم ActiveX في Excel

عناصر تحكم نموذج VBA/واجهة المستخدم الرسومية في Excel

GUI هو اختصار لـ Graphical User Interface (واجهة المستخدم الرسومية). واجهة المستخدم الرسومية هي جزء البرنامج الذي يتفاعل معه المستخدم. تتكون واجهة المستخدم الرسومية من عناصر تحكم. يمكن استخدام عناصر تحكم VBA في Excel في نموذج. يوضح الجدول التالي بعض عناصر تحكم VBA الأكثر استخدامًا في واجهة المستخدم الرسومية.

S / N السيطرة الوصف
1 أمر تستخدم لتنفيذ التعليمات البرمجية
2 كومبو Box تستخدم لتقديم قائمة منسدلة للمستخدمين
3 تحقق Box تُستخدم للقيم الصحيحة أو الخاطئة. يمكن أن تحتوي مربعات الاختيار المتعددة على قيم صحيحة في نفس الوقت.
4 قائمة Box تستخدم لتقديم قائمة بسيطة للمستخدمين
5 نص Box يستخدم لقبول إدخال المستخدم
6 شريط التمرير يستخدم لتوفير شريط التمرير لأغراض التنقل.
7 زر الدوران يُستخدم لزيادة أو تقليل القيم الرقمية من خلال النقر على الأسهم لأعلى أو لأسفل التي تظهر في زر الدوران
8 زر الخيارات تستخدم للقيم الحقيقية أو الخاطئة. يمكن أن يكون لزر خيار واحد فقط قيمة حقيقية في المرة الواحدة.
9 تُشير يستخدم لعرض نص ثابت للمستخدم
10 صورة يستخدم لعرض الصور
11 Togglزر البريد الزر الذي تم الضغط عليه وحالات عدم الضغط عليه.

في عنصر تحكم VBA GUI،

  1. انقر فوق علامة التبويب المطور
  2. انقر على زر إدراج القائمة المنسدلة

سوف تحصل على اللوحة المنسدلة التالية

عناصر تحكم نموذج VBA/واجهة المستخدم الرسومية في Excel

إذا قمت بتمرير الماوس فوق عناصر تحكم نموذج VBA، فسيظهر اسم عنصر التحكم كما هو موضح أدناه

عناصر تحكم نموذج VBA/واجهة المستخدم الرسومية في Excel

إضافة عناصر تحكم واجهة المستخدم الرسومية إلى جدول بيانات

سنقوم الآن بإضافة زر أمر إلى المصنف الخاص بنا، لذلك يتعين علينا القيام بذلك

  • انقر على القائمة المنسدلة "إدراج".
  • انقر على زر الأمر (تحكم ActiveX) كما هو موضح في الصورة أعلاه
  • اسحب وارسم الزر الموجود على الورقة

إضافة عناصر تحكم واجهة المستخدم الرسومية إلى جدول البيانات

ضبط خصائص التحكم في واجهة المستخدم الرسومية

سنقوم الآن بتعيين التسمية التوضيحية للزر على شيء أكثر وصفًا

ضبط خصائص التحكم في واجهة المستخدم الرسومية

  1. انقر بزر الماوس الأيمن على زر المساواة الذي أنشأناه للتو
  2. حدد قائمة الخصائص
  3. سوف تحصل على نافذة الخصائص التالية

ضبط خصائص التحكم في واجهة المستخدم الرسومية

  • قم بتعيين خاصية الاسم إلى btnButton. يتم استخدام خاصية الاسم لتعريف عنصر التحكم في إطار التعليمات البرمجية. btn هي البادئة للزر.
  • قم بتعيين خاصية التسمية التوضيحية إلى Click Me. النص الموجود في خاصية التسمية التوضيحية هو ما سيراه المستخدمون على الزر.

أغلق النافذة عند الانتهاء.

سوف تحصل على النتائج التالية

ضبط خصائص التحكم في واجهة المستخدم الرسومية

Difference Between Form Controls and ActiveX Controls

Excel offers two families of controls, and choosing the right one keeps a project simple. Form controls are built into Excel and run a single assigned macro, while ActiveX controls are programmable objects that respond to many events through VBA code. The table below compares the two.

الميزات شكل عناصر التحكم عناصر تحكم ActiveX
اقامة Simple; assign one macro to the control Requires VBA event code to respond
عقارات Limited set of options Extensive, including font, colour, and behaviour
الفعاليات Single action on click Many events such as Click, Change, and GotFocus
التوافق يعمل على Windows and Mac Excel يعمل على Windows إكسل فقط
أفضل مناسبة ل Quick, simple worksheet tasks Interactive and dynamic user forms

In short, use a Form control when a single macro is enough, and choose an ActiveX control when the interface needs rich properties or must react to several user events.

بشرط اساسي

Before building the ActiveX example, prepare the workbook so the controls are available and the macros can be saved.

ضبط Microsoft Excel
كما رأينا في البرنامج التعليمي السابق، تأكد من أن الشريط الخاص بك سيحتوي على أيقونة "المطور" كما هو موضح هنا.

بشرط اساسي

الآن، أعد تسمية الورقة 1 إلى "VBA Operator” as shown in screen-shot below. Save the workbook in an Excel Macro-Enabled Workbook (*.xlsm) file extension. (see the image below)

بشرط اساسي

بعد ذلك قم بالضغط على زر حفظ.

كيفية استخدام عنصر تحكم ActiveX في VBA

With the workbook ready, we can add a working ActiveX button. In this section of ActiveX control Excel, we will see how to incorporate ‘commandclick’ button in VBA and execute a program using the button.

الخطوة 1) في هذه الخطوة، انقر فوق خيار "زر الإدراج" من عنصر التحكم Active X. ثم حدد خيار زر الأمر منه.

استخدم عنصر تحكم ActiveX في VBA

الخطوة 2) لإدراج "clickcommandbutton1" اسحب مؤشر الماوس إلى ورقة Excel.

استخدم عنصر تحكم ActiveX في VBA

الخطوة 3) ثم انقر بزر الماوس الأيمن على زر الأمر وحدد الخيار "عرض". Code".

استخدم عنصر تحكم ActiveX في VBA

الخطوة 4) تأكد من أنك على الورقة الصحيحة. سيتم فتح محرر التعليمات البرمجية. ادخل رمزك.

  • في الكود، حددنا النطاق من A1 إلى A10.
  • يحدد هذا النطاق رقم الخلية في ورقة Excel من "A1 إلى A10".
  • بعد ذلك، نُعرّف السلسلة "Guru"99 برنامج تعليمي في VBA" في الكود
  • لذا، عند النقر على زر "الأمر" في برنامج إكسل، يجب أن يطبع "Guru"99 VBA Tutorial" في نطاق الخلايا "A1 إلى A10".

استخدم عنصر تحكم ActiveX في VBA

الخطوة 5) في الخطوة التالية، احفظ ملف التعليمات البرمجية ثم اخرج من المحرر. للعودة إلى ملف Excel، انقر فوق أيقونة ورقة Excel استخدم عنصر تحكم ActiveX في VBA في أقصى اليسار.

استخدم عنصر تحكم ActiveX في VBA

الخطوة 6) في ورقة Excel، سترى علامة التبويب "وضع التصميم" قيد التشغيل. تأكد من أنه "متوقف" وإلا فلن يعمل الرمز الخاص بك.

استخدم عنصر تحكم ActiveX في VBA

الخطوة 7) عند إيقاف وضع التصميم، لن يكون هناك تمييز باللون الأخضر عليه. الآن يمكنك النقر على زر الأمر.

استخدم عنصر تحكم ActiveX في VBA

الخطوة 8) انقر على "CommandButton1". سيتم طباعة "Guru"99 VBA Tutorial" في نطاق الخلايا "A1 إلى A10".

استخدم عنصر تحكم ActiveX في VBA

قم بتنزيل Excel الذي يحتوي على الكود أعلاه

قم بتنزيل ملف Excel أعلاه Code

Common ActiveX Control Events in VBA

An ActiveX control is powerful because it reacts to events, which are actions the user performs on the control. Each event runs its own subroutine, so the same button can behave differently depending on what the user does. The most common events are listed below.

  • انقر – runs the code when the user clicks the control, the event used in the example above.
  • التغيير – runs when the value of a control changes, such as typing in a text box or picking a combo box item.
  • GotFocus / LostFocus – runs when the control gains or loses focus, useful for validating input.
  • KeyPress عنصر – runs when the user presses a key while the control is active.
  • تحريك الماوس – runs when the mouse pointer moves over the control.
  • DblClick – runs when the user double-clicks the control.

To add code for any event, switch on Design Mode, right-click the control, choose View Code, and select the event from the drop-down list at the top of the code window.

الأسئلة الشائعة

A Form control runs a single macro you assign to it and works on Windows and Mac. An ActiveX control is programmed in VBA, exposes many properties and events, and is available on Windows Excel only.

The Insert menu for Form and ActiveX controls lives on the Developer ribbon, which is hidden by default. Enable it from File, Options, Customize Ribbon, then tick Developer, so the controls and the VBA editor become available.

The button is likely still in Design Mode, shown by a green highlight on the Developer tab. Turn Design Mode off so the control runs its event code. The workbook must also be saved as a macro-enabled .xlsm file.

Yes. AI assistants such as Copilot write the Click or Change subroutine from a plain description, for example clear the range when the button is pressed. The developer pastes it into the control’s code window and tests it.

Yes. Describe the input you need and an AI assistant suggests a suitable control, such as a combo box for a fixed list or a check box for a yes or no value, and explains how to set its key properties.

تلخيص هذه التدوينة بـ: