How to Find Partial Text Within Cell Contents in VBA
Question details
The user needs to search for a specific substring within the contents of cells in a specific row using VBA, even when the target string is part of a longer text.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Writing a VBA macro to locate specific data embedded within larger cell strings across a specific row or range.
- Observed behavior
- The user wants to ensure the search successfully returns cells containing the partial match rather than requiring an exact full-cell match.
Ensure that the Developer tab is enabled in your spreadsheet application and that your workbook is saved in a macro-enabled format (like .xlsm) before running the VBA code.
Use Range.Find with LookAt:=xlPart
Use the Range.Find method and specify the LookAt parameter as xlPart to find substrings within cell values.
By default, the Find method might look for an exact match depending on previous search settings. Explicitly setting LookAt:=xlPart ensures that VBA searches for the specified string anywhere within the cell's contents.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in your spreadsheet software.
Click 'Insert' > 'Module' from the top menu to create a blank script window.
Enter your script. Use the code: Set foundCell = Rows(4).Find(What:="WK4", LookIn:=xlValues, LookAt:=xlPart). This specifically targets Row 4 and searches for 'WK4' within any longer text.
Always check if a match was found to prevent errors by adding: If Not foundCell Is Nothing Then MsgBox "Found at " & foundCell.Address.

Write and Run VBA Macros Seamlessly in WPS Spreadsheet
WPS Office provides robust support for VBA macros, allowing you to run your existing Excel VBA scripts, including Range.Find functions, without modifying the code.
- 1. Open your macro-enabled workbook: Launch WPS Spreadsheet and open your .xlsm or .xls file containing the VBA code.
- 2. Access the Developer tools: Navigate to the 'Developer' tab on the ribbon menu and click on 'Visual Basic' or 'Macros'.
- 3. Run the script: Execute your macro directly within the WPS VBA editor to find your target text instantly.

Frequently Asked Questions
Why does my Range.Find return Nothing even when the text is there?
This often happens if the LookAt parameter was previously set to xlWhole (exact match) in another macro or during a manual search. Always explicitly define LookAt:=xlPart in your VBA code to ensure partial matches are found.
How can I search for text case-insensitively using Range.Find?
You can control case sensitivity by setting the MatchCase parameter to False within your Range.Find method (e.g., MatchCase:=False). Set it to True if you need an exact case match.
Can I use Range.Find to search across multiple sheets?
Range.Find only works on a single worksheet at a time. To search multiple sheets, you need to write a For Each loop in VBA to iterate through all the Worksheets in your Workbook and apply the Find method to each sheet's UsedRange.
How do I find the next occurrence of the text?
After finding the first instance, use the Range.FindNext(After:=foundCell) method in a loop to find subsequent matches. Be sure to save the address of the first match to stop the loop when it cycles back to the beginning.




