How to Find Text in Word Tables and Insert Footnote References Using VBA Macro
Question details
The user wants to automatically search for specific text strings (like fruit names) within Word tables and insert cross-references to corresponding footnotes using a VBA macro.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document formatting by finding matched text inside table columns and dynamically linking them to existing footnotes.
- Observed behavior
- The user requires functional VBA code to automate the manual process of checking the first column of document tables and inserting specific cross-references when a target word is found.
Ensure that your word processor supports VBA macros and that you have already created the necessary footnotes (e.g., Footnote 1, 2, and 3) in your document before running the code.
Use a VBA Macro to Automate Table Search and Cross-Referencing
Apply a custom VBA script to loop through document tables, find specific keywords in the first column, and instantly insert footnote cross-references next to them.
The following macro searches the first column of every table in your document. When it detects specific text (e.g., 'Orange', 'Strawberry', or 'Blueberry'), it calls a secondary subroutine to insert a cross-reference linked to footnote 1, 2, or 3.
Press the ALT + F11 keys on your keyboard to open the Visual Basic for Applications (VBA) editor in your word processor.
In the VBA editor, click on 'Insert' in the top menu bar, and select 'Module' to create a blank code window.
Copy and paste the following code into the module window: Sub Demo() Application.ScreenUpdating = False Dim Tbl As Table, Rng As Range Dim r As Long With ActiveDocument For Each Tbl In .Tables For r = 1 To Tbl.Rows.Count Set Rng = Tbl.Cell(r, 1).Range Rng.End = Rng.End - 1 If InStr(Rng.Text, "Orange") > 0 Then Call InsertXRef(Rng.Duplicate, 1) If InStr(Rng.Text, "Strawberry") > 0 Then Call InsertXRef(Rng.Duplicate, 2) If InStr(Rng.Text, "Blueberry") > 0 Then Call InsertXRef(Rng.Duplicate, 3) Next r Next Tbl End With Application.ScreenUpdating = True End Sub Sub InsertXRef(Rng As Range, i As Long) With Rng .Collapse wdCollapseEnd .InsertCrossReference ReferenceType:=wdRefTypeFootnote, ReferenceKind:=wdFootnoteNumberFormatted, ReferenceItem:=i, InsertAsHyperlink:=True End With End Sub
Place your cursor inside the 'Sub Demo()' code block and press F5, or click the 'Run' button in the toolbar. The macro will process all tables and add the cross-references.

Run VBA Macros Seamlessly in WPS Writer
WPS Office offers robust support for VBA macros, allowing you to easily run advanced scripts like finding table text and inserting footnotes to streamline your workflow.
- 1. Open Your Document: Launch WPS Writer and open the document containing the tables and footnotes you want to process.
- 2. Access the VBA Editor: Navigate to the 'Developer' tab and click on 'Macros', or simply press the ALT + F11 keyboard shortcut.
- 3. Insert and Run Code: Insert a new module, paste your table searching and cross-reference code, and click 'Run' to automate your task instantly.

Frequently Asked Questions
How do I modify the VBA code to search for different words?
You can change the search words by locating the `InStr(Rng.Text, "Orange")` line in the VBA code and replacing "Orange" with your desired keyword. Repeat this step for any other words you wish to target.
Why am I getting a runtime error when I run the macro?
This commonly occurs if the footnote number referenced in the macro (e.g., footnote 1, 2, or 3) does not exist in your document. You must create the corresponding footnotes before executing the code.
Does this macro search every column in the table?
No, this specific macro is designed to search only the first column of every table. This is defined by the snippet `Tbl.Cell(r, 1)`. To search other columns, you would need to modify the column index or add a nested loop to iterate through all columns.
Can I use this VBA macro in WPS Office?
Yes, WPS Office supports VBA macros. However, you need to ensure you are using a version of WPS Office that includes the VBA module enabled, which typically allows you to write, edit, and run scripts just like in Microsoft Word.




