VBA & Macro Problems

How to Read and Store a RibbonX EditBox Value Using VBA

Muhammad TalhaMuhammad Talha Oct 29, 2026 868 views

Question details

The user needs to capture the input value from a custom RibbonX EditBox and store it in a public VBA variable so it can be accessed by other macros across the project.

How to Read a RibbonX EditBox Value with VBA
Product
Excel / Office VBA
Device & OS
not provided
Scenario
Developing a custom Ribbon UI and VBA macros where user input from a RibbonX EditBox needs to be captured and reused dynamically.
Observed behavior
Needs the correct syntax and structure to declare a global/public variable and populate it with the EditBox text during an application event or callback.
Before you start

Ensure that the Developer tab is enabled in your application ribbon and that you know how to insert a standard module in the Visual Basic Editor.

Solution 1Recommended

Declare and Use a Public Variable in a Standard Module

Store the RibbonX EditBox value in a public variable declared at the top of a standard module so it can be accessed by any procedure in your project.

A public variable is ideal when the value relies on user interaction (like typing into a RibbonX EditBox) and changes dynamically. It retains its value across different macro executions as long as the project is not reset.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic Editor from your spreadsheet.

2
Insert a Standard Module

Right-click on your project in the Project Explorer pane, select 'Insert', and choose 'Module'.

3
Declare the Public Variable

At the very top of the module, type 'Option Explicit' followed by 'Public UserN As String' on the next line.

4
Assign the Value via Callback

Create a RibbonX onChange callback subroutine (e.g., Sub EditBox_OnChange(control As IRibbonControl, text As String)) and assign the 'text' argument to your 'UserN' variable.

Declare and Use a Public Variable in a Standard Module
Public Variable vs. Public Const: Use a 'Public Const' (e.g., Public Const UserN As String = "John") only when the value is hardcoded and never changes. If the value depends on the user's input from the EditBox, you must use a standard public variable.
WPS VBA Support

Write and Execute VBA Code Easily in WPS Office

WPS Office includes a robust VBA editor, allowing you to create custom macros, define public variables, and automate your spreadsheet workflow seamlessly without needing expensive software.

  1. 1. Download WPS Office: Download and install WPS Office, then open WPS Spreadsheet.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon menu.
  3. 3. Launch the VBA Editor: Click on the 'Visual Basic' icon or press Alt + F11 to launch the VBA Editor.
  4. 4. Write Your Macros: Insert a new module and define your public variables to handle custom inputs effortlessly.
Full support for standard VBA code, userforms, and standard modulesHighly compatible with Microsoft Excel macro-enabled workbooks (.xlsm and .xlam)Lightweight architecture for fast execution of custom scriptsFree to download with a familiar user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why is my public VBA variable unexpectedly losing its value?

Public variables can lose their value (reset to empty) if the VBA project experiences an unhandled run-time error, if you press the 'Reset' (Stop) button in the VBA Editor, or if you edit the code while it is paused. To prevent this, ensure robust error handling is in place.

How do I trigger the VBA macro when the RibbonX EditBox changes?

In your custom RibbonX XML file, add the 'onChange' attribute to your <editBox> tag (e.g., onChange="MyMacro"). Then, in your VBA module, ensure 'MyMacro' matches the required signature: Sub MyMacro(control As IRibbonControl, text As String).

Can I use a Public Const to store the EditBox value?

No. A Constant (Public Const) is evaluated at compile time and cannot be altered during code execution. Since an EditBox value is generated dynamically by user input, you must use a standard Public variable.