optimize below code and rsponsive : B3-g3 to B21-G21
Sub SumMatchingData()
Dim wsBase As Worksheet
Dim wsCalc As Worksheet
Dim headerValue As Variant
Dim tzValue As Variant
Dim targetB3 As Variant
Dim totalSum As Double
Dim col As Long
Dim lastRow As Long
Dim row As Long
code
' Set worksheet references
Set wsBase = Worksheets("base")
Set wsCalc = Worksheets("calc")
' Get the value in calc!B3
targetB3 = wsCalc.Range("B6").Value
totalSum = 0
' Determine last row (assuming data up to row 17000)
lastRow = 16460
' Loop through rows 3 to 17000
For row = 3 To lastRow
' Loop through columns 1 to 546 to check headers
For col = 1 To 546
headerValue = wsBase.Cells(1, col).Value
' Check if header is numeric and between 500 and 800
If IsNumeric(headerValue) Then
If headerValue >= 500 And headerValue <= 800 Then
' Check if the "TZ" column matches calc!B3
tzValue = wsBase.Cells(row, 546).Value
If tzValue = targetB3 Then
' Add the cell value to totalSum
totalSum = totalSum + wsBase.Cells(row, col).Value
End If
End If
End If
Next col
Next row
' Output result in calc!G3
wsCalc.Range("G6").Value = totalSum
End Sub
Assistant
To optimize your code and make it more efficient and faster, here are some key points:
Avoid looping through each cell individually (especially large ranges) as much as possible.
Read data into VBA arrays and process in memory (much faster).
Minimize worksheet reads/writes.
Use variables effectively and avoid redundant operations inside loops.
Ensure the code is responsive by disabling screen updating and events while running.
Optimized and Responsive Version of your Macro
vba
Sub SumMatchingData_Optimized()
Dim wsBase As Worksheet, wsCalc As Worksheet
Dim headers As Variant, data As Variant
Dim targetB6 As Variant
Dim totalSum As Double
Dim lastRow As Long, lastCol As Long
Dim iRow As Long, iCol As Long
Dim tzCol As Long
Dim headerVal As Variant
Dim tzValue As Variant
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
' Set worksheets
Set wsBase = ThisWorkbook.Worksheets("base")
Set wsCalc = ThisWorkbook.Worksheets("calc")
' TZ column is 546 based on your code (You can set dynamically if needed)
tzCol = 546
' Target value in calc!B6 (not B3, your code uses B6)
targetB6 = wsCalc.Range("B6").Value
' Determine last row and last col dynamically if needed
' Here lastRow fixed as per your code
lastRow = 16460
lastCol = 546
' Read header row 1 columns 1 to 546 into array
headers = wsBase.Range(wsBase.Cells(1, 1), wsBase.Cells(1, lastCol)).Value
' Read the entire relevant data block (rows 3 to lastRow, cols 1 to lastCol) into array
data = wsBase.Range(wsBase.Cells(3, 1), wsBase.Cells(lastRow, lastCol)).Value
totalSum = 0
' Loop rows and columns through arrays - more responsive
For iRow = 1 To UBound(data, 1) ' data is 3:lastRow so iRow=1 corresponds to actual row 3
' Check the TZ col value for this row first
tzValue = data(iRow, tzCol)
If tzValue = targetB6 Then
' Loop through all columns headers and for those between 500 and 800 add the cell value
For iCol = 1 To lastCol
headerVal = headers(1, iCol)
If IsNumeric(headerVal) Then
If headerVal >= 500 And headerVal <= 800 Then
' Sum the data cell value
If IsNumeric(data(iRow, iCol)) Then
totalSum = totalSum + data(iRow, iCol)
End If
End If
End If
Next iCol
End If
Next iRow
' Output result in calc!G6
wsCalc.Range("G6").Value = totalSum
' Restore application settings
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Key Improvements:
Bulk reading headers and data into arrays: This is extremely faster than repeatedly accessing cells.
Single pass through data array with checks for TZ column and header filtering.
Error handling for non-numeric data in cells before summing.
Disabling Screen Updating and Events during operation for responsiveness.
Dynamic ranges can be added if needed - here I maintained your fixed sizes.
Making it Responsive:
Running heavy loops on large datasets always takes time.
You can show a progress bar or update status by using DoEvents inside the loop if you want responsiveness on UI.
Example (update every 1000 rows):
vba
If iRow Mod 1000 = 0 Then DoEvents
Optional: Dynamically Find Last Row and Last Col (if your data size varies)
To find last used row in a specific column, say column A or your TZ column: