Hi Team,
Need your help in creating Sum formula Dynamically. as per Criteria List.
in K Column.
Below is my attempted code, which is working. are there any chances to improve in code.
or any other method, but it should be in Sum formula only. as my criteria list lies in other workbook.
Below is a Table.
Thanks
mg
Need your help in creating Sum formula Dynamically. as per Criteria List.
in K Column.
Below is my attempted code, which is working. are there any chances to improve in code.
or any other method, but it should be in Sum formula only. as my criteria list lies in other workbook.
VBA Code:
Sub Create_SumFormula()
Dim rgSearch As Range
Dim cell As Range
Dim fname As String
Dim L As String
Dim n As String
Dim s As String
Dim ms As String
Set rgSearch = Range("K3", Range("k" & Rows.Count).End(xlUp)) 'Search each cell in D Column.
For Each cell In rgSearch
Set cell = Range("D:D").Find(cell.Value)
If Not cell Is Nothing Then
L = Split(cell.Offset(, 1).Address, "$")(1)
n = Split(cell.Offset(, 1).Address, "$")(2)
If s = "" Then
s = L & n
ms = s
Else
s = L & n
ms = ms & "," & s
End If
End If
Next cell
If ms <> "" Then
sumformula = "=SUM(" & ms & ")"
Range("e18:G18").Formula = sumformula
'Recorded code
Range("E18:G18").FormulaR1C1 = "=SUM(R[-12]C,R[-10]C,R[-8]C)"
End If
End Sub
Below is a Table.
Book2.xlsm | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | I | J | K | |||
1 | Table | |||||||||||
2 | Cost Head | 1000 | 2000 | 3000 | Expendiature List | |||||||
3 | Basic | 22022 | 32717 | 35091 | Telephone Reimbursement | |||||||
4 | DA | 38886 | 20227 | 31564 | Fuel & Maintenance | |||||||
5 | HRA | 23044 | 21483 | 36847 | Driver's Salary | |||||||
6 | Telephone Reimbursement | 100 | 200 | 300 | ||||||||
7 | Bonus | 24203 | 32033 | 20467 | ||||||||
8 | Fuel & Maintenance | 300 | 500 | 800 | ||||||||
9 | Car Allowance | 26432 | 20531 | 29890 | ||||||||
10 | Driver's Salary | 400 | 300 | 1200 | ||||||||
11 | ||||||||||||
12 | ||||||||||||
13 | ||||||||||||
14 | ||||||||||||
15 | ||||||||||||
16 | Reimbursement Payble | 800 | 1000 | 2300 | ||||||||
17 | ||||||||||||
18 | Reimbursement Payble | 800 | 1000 | 2000 | ||||||||
Sheet1 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
E18:F18,E16:F16 | E16 | =SUM(E$6,E$10,E$8) |
G16,G18 | G16 | =SUM(G6,G8,G10) |
Thanks
mg