How to Use Excel VBA to Find and Sum First Matching Records
Question details
The user needs an Excel VBA script to identify the first occurrence of unique combinations in columns A, C, and F (specifically where column F contains 'FQQ'), and then output the sum of columns J and K into column G.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating data summation for the first instance of specific group criteria using a VBA macro.
- Observed behavior
- The goal is to loop through rows, check data combinations using COUNTIFS logic, and output either a static sum value or a dynamic Excel formula in column G.
Ensure that you have enabled the Developer tab in your spreadsheet application and saved your workbook as a Macro-Enabled Workbook (.xlsm) before running the VBA code.
Use a VBA Loop with COUNTIFS for Unique Records
This method loops through the dataset starting from row 4, uses COUNTIFS to check for the first occurrence of the target combination, and writes the sum or formula into the specified column.
By leveraging the Application.WorksheetFunction.CountIfs method within a VBA loop, you can accurately identify the very first instance of a unique data combination.
You have the flexibility to either insert the static calculated value or inject a dynamic formula using FormulaR1C1, depending on whether you want the results to update automatically if source data changes.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the editor menu, click 'Insert' and select 'Module' to create a blank script window.
Write a For loop that iterates from row 4 through the last used row in your worksheet.
Inside the loop, check if column F equals 'FQQ'. Then, use WorksheetFunction.CountIfs to count occurrences of the combinations in columns A, C, and F from row 4 down to the current row (i).
When the count equals 1, insert the static sum using `Cells(i, 7).Value = Cells(i, 10).Value + Cells(i, 11).Value`. Alternatively, to insert a dynamic formula, use `Cells(i, 7).FormulaR1C1 = "=RC[3]+RC[4]"`.

Automate Data Tasks Easily with WPS Spreadsheet
WPS Spreadsheet fully supports VBA macros, allowing you to run your Excel scripts natively. You can seamlessly loop through records, perform conditional sums, and insert complex formulas without modifying your existing code.
- 1. Open your Workbook: Launch WPS Spreadsheet and open the data file containing your records.
- 2. Access the Developer Tab: Click on the Developer tab in the top ribbon and select 'Visual Basic' to open the built-in VBA editor.
- 3. Insert the Macro Code: Create a new Module and paste your VBA loop and COUNTIFS code to process the specific FQQ records.
- 4. Run the Script: Press F5 or click the Run button to execute the code and automatically fill Column G with the calculated sums or formulas.

Frequently Asked Questions
Can I apply this VBA script to a different set of columns?
Yes, you can modify the column index numbers in the Cells(row, column) references within your VBA code to target different columns for both your criteria checks and the final output.
Why is the COUNTIFS function returning an error in my VBA script?
Ensure you are calling it properly using Application.WorksheetFunction.CountIfs. Also, verify that the range references inside the function accurately reflect the dynamically changing rows inside your loop using Range objects.
What is the difference between Formula and FormulaR1C1 in Excel VBA?
The 'Formula' property uses standard column letters and row numbers (like =J4+K4). The 'FormulaR1C1' property uses relative positioning based on rows and columns (like =RC[3]+RC[4]), which is highly efficient and easier to use when looping through multiple rows.




