How to Combine Two VBA Macro Results in One Excel Cell
Question details
The user wants to write the outputs of two separate VBA lookup macros into a single Excel cell without the second result overwriting the first.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running multiple VBA lookups that need to output data into the exact same destination cell.
- Observed behavior
- Currently, the second macro overwrites the data placed in the cell by the first macro, resulting in lost data instead of combined data.
Ensure you have a backup of your current Excel workbook, and open the VBA Editor (ALT + F11) to access your existing macro code.
Combine Lookups into a Single VBA Procedure
Merge the two separate macros into a single loop to capture both variables before writing them to the target cell.
When two separate macros write to the same target cell, the default behavior of Excel is to overwrite the existing value. To retain both results, it is best to combine the operations into a single loop.
By evaluating both lookups simultaneously, you can store their results as variables, concatenate them together with a separator like a slash (/), and then push the final combined string into the cell.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor, and locate the module containing your current macros.
Instead of running two separate loops, write a single For or Do While loop that will process the data for both lookups at the same time.
Inside the loop, declare two variables for your lookups. For example, assign the lookup for the eighth character to variable 'v1' and the lookup for the ninth character to variable 'v2'.
Use conditional If statements to construct the final string. If both v1 and v2 successfully return values, combine them: CombinedResult = v1 & "/" & v2.
Add ElseIf conditions to handle cases where only one lookup succeeds. If only v1 has a value, set CombinedResult = v1. If only v2 has a value, set CombinedResult = v2. This prevents a hanging slash from appearing.
Assign the final concatenated variable to your destination cell (e.g., ActiveSheet.Cells(i, 10).Value = CombinedResult).

Use WPS Spreadsheet for Powerful VBA Macro Execution
WPS Spreadsheet fully supports VBA macros, allowing you to run complex scripts, combine lookup results, and automate workflows with excellent Microsoft Excel compatibility.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click 'Visual Basic', or press ALT + F11.
- 3. Edit Your Code: Paste your newly combined macro code into the standard module within the WPS VBA Editor.
- 4. Run the Combined Macro: Run the macro directly from the editor or ribbon to populate your combined cell results efficiently.

Frequently Asked Questions
Why does my second VBA macro overwrite the first one?
When a macro writes data to a cell using a standard assignment like 'Range("A1").Value = x', it completely replaces the existing content. To keep both, you must either concatenate the new value with the old value, or combine the logic before writing.
Can I concatenate cell values without using VBA macros?
Yes. If your data is already generated in separate columns, you can use built-in Excel formulas like CONCATENATE, TEXTJOIN, or the '&' operator (e.g., '=A1 & "/" & B1') directly in the worksheet.
How do I use a line break instead of a slash to separate the results in VBA?
To separate the two lookup results with a line break inside the cell, use 'vbLf' in your VBA code instead of "/". For example: 'Cell.Value = v1 & vbLf & v2'. Ensure the target cell has 'Wrap Text' enabled so the line break displays properly.
What if I have more than two macro lookups to combine?
You can extend the same combined loop logic. Capture the output of all lookups into separate variables (v1, v2, v3, etc.), use an array or conditional If/Else statements to join only the non-empty values with your chosen delimiter, and write the final string to the cell.




