How to Read and Store a RibbonX EditBox Value Using VBA
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.

- 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.
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.
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.
Press Alt + F11 to open the Visual Basic Editor from your spreadsheet.
Right-click on your project in the Project Explorer pane, select 'Insert', and choose 'Module'.
At the very top of the module, type 'Option Explicit' followed by 'Public UserN As String' on the next line.
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.

Assign Variable Values During Startup Based on User
If your RibbonX or workbook logic depends on the specific user opening the file, assign the public variable during the workbook startup event instead.
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. Download WPS Office: Download and install WPS Office, then open WPS Spreadsheet.
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon menu.
- 3. Launch the VBA Editor: Click on the 'Visual Basic' icon or press Alt + F11 to launch the VBA Editor.
- 4. Write Your Macros: Insert a new module and define your public variables to handle custom inputs effortlessly.

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.




