VBA & Macro Problems

How to Update Excel Text Boxes with a Reusable VBA Macro via Button Clicks

Muhammad TalhaMuhammad Talha Oct 20, 2026 870 views

Question details

The user wants to update multiple Excel text boxes using a single, reusable VBA macro triggered by different buttons, aiming to avoid code duplication for each individual button.

How to Update Excel Text Boxes Using a Reusable VBA Macro and Button Clicks
Product
Excel
Device & OS
not provided
Scenario
Displaying coworker information dynamically in text boxes by clicking different buttons, where each button retrieves specific data from a worksheet.
Observed behavior
Instead of writing separate, duplicated VBA modules for each button, the user seeks a streamlined function that can accept variables and be called by all buttons to place values in the required locations.
Before you start

Ensure the Developer tab is enabled in your Excel ribbon and that your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) so your VBA code executes properly.

Solution 1Recommended

Create a Reusable Parameterized Subroutine

Use a primary VBA procedure that accepts variables (such as row numbers) and assign short macros to your buttons that call this main procedure.

Instead of copying the same code for every button, you can write a central 'helper' procedure that takes a parameter (like the data's row number). Each button is then assigned a tiny macro that simply tells the helper procedure which row to look at. This keeps your code clean and easy to maintain.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications Editor. Right-click on your workbook in the Project Explorer, select 'Insert', and click 'Module'.

2
Write the Main Reusable Macro

In the new module, create a parameterized subroutine. For example: `Sub ShowEmployee(ByVal rowNumber As Long)`. Inside this sub, write the code that assigns cell values from the specified row to your shape text frames using `.Shapes("Rectangle 1").TextFrame2.TextRange.Characters.Text = Cells(rowNumber, 1).Value`.

3
Create Button Click Subroutines

Below your main macro, create a short macro for each button. For example, `Sub Button1_Click()` that contains exactly one line: `ShowEmployee 2`. For button 2, create `Sub Button2_Click()` containing `ShowEmployee 3`.

4
Assign Macros to Buttons

Return to your Excel worksheet. Right-click the first button or shape, select 'Assign Macro', choose `Button1_Click` from the list, and click OK. Repeat this process for the remaining buttons.

Create a Reusable Parameterized Subroutine
Code Efficiency: This approach centralizes your logic in one place. If you ever need to change which text box gets updated, you only need to modify the main 'ShowEmployee' subroutine instead of dozens of individual button scripts.
Advanced Spreadsheet Automation

Use WPS Spreadsheet to Run and Manage Reusable VBA Macros

WPS Office provides robust support for VBA macros in its advanced versions, allowing you to create reusable procedures, assign macros to buttons, and automate text box updates smoothly.

  1. 1. Enable the Developer Tab: Open your workbook in WPS Spreadsheet, go to the Options menu, and ensure the Developer tab is enabled to access macro features.
  2. 2. Access the VBA Editor: Click on the Developer tab and select 'Visual Basic', or press ALT + F11 to launch the integrated VBA Editor.
  3. 3. Insert Your VBA Code: Insert a new module and paste your reusable macro along with the smaller button-click subroutines.
  4. 4. Link Macros to Worksheet Shapes: Insert shapes from the Insert menu, right-click them, and choose 'Assign Macro' to link your centralized scripts to user clicks.
Full compatibility with Microsoft Excel VBA code and .xlsm filesLightweight and fast execution of complex parameterized macrosEasy-to-use Developer tools for seamless macro creation and module management
microsoft office alternative - wps office

Frequently Asked Questions

Why am I getting an 'Object doesn't support this property or method' error when updating the text box?

This typically occurs if the shape name in your VBA code (e.g., 'Rectangle 1') does not perfectly match the actual name of the shape on your worksheet. Select the shape in Excel and check the Name Box (top left, next to the formula bar) to verify its exact spelling and spacing.

Can I use Application.Caller to avoid writing separate button click macros entirely?

Yes. If all your shapes are form control buttons, you can assign a single macro to all of them. Inside that macro, use `Application.Caller` to determine the name of the button that was clicked. You can then use a Select Case statement to assign the correct row data based on the button's name.

Do I need to save my file in a specific format to keep these reusable macros?

Yes, you must save your file as an Excel Macro-Enabled Workbook (.xlsm) or an Excel Binary Workbook (.xlsb). If you save it as a standard .xlsx file, Excel will permanently remove all your VBA code upon saving and closing.