Hi Folks,
I have one excel file in that excel I have Fund code column and for each fund code i want to sum other columns and I want to update grand total for each fund code category. for example for general account fund code is GA and for mutual fund it is MA how i get the total for each fund code.
For reference I am attaching screenshot
please do let me know how I achieve this!
Thanks in Advance
Please let me know if anyone has any solution?
Hey Bro, this can be easily achieved using VBA code, let me know if you are looking for a vba code to achieve this.
UiPath solution
-
read excel file using Excel Application Scope into variable Dt
-
get unique values from your column fund code using below LINQ query
UniqueCodes=dt.AsEnumerable().Select(Function(row) row.Field(Of String)(“Fund Code”)).Distinct().ToArray()
-
Loop through each string in UniqueCodes array and inside the loop use below query to get all filtered columns sum based on Fund Code
SumOfColumns= dt.AsEnumerable().Where(Function(row) row.Field(Of String)(“Fund Code”) = UniqueCodes(0)).SelectMany(Function(row) dt.Columns.Cast(Of DataColumn).Select(Function(col) Convert.ToInt32(row(col)))).GroupBy(Function(x, i) i Mod dt.Columns.Count).Select(Function(g) g.Sum()).ToList()
the above query will give sum of each column as list of integers
-
then use below query to get cell address where “Grand Total” is found
CellAddress= dt.AsEnumerable() _
.SelectMany(Function(row, rowIndex) dt.Columns.Cast(Of DataColumn) _
.Where(Function(col) row(col).ToString() = valueToFind) _
.Select(Function(col) $“({rowIndex + 1}, {dt.Columns.IndexOf(col) + 1})”)) _
.ToList()
-
Use above cell address and increment column index and replace the value with sum stored in SumOfColumns variable
-
write back the datatable to another excel sheet
you can achieve this using 9-19 lines vba code as well , more efficient and dynamic and less complex.
Try and let me know
@armamidwar are you able to share VBA code?
i 'll share the code with you tomm , share me a sample files with few row entries if possible.
amamidwar@comscore.com
@armamidwar I Have shared excel file
1 Like
- Use Excel Application Scope
- Read Range to get all sheet data in DataTable Dt
- Get your unique FundCode
UniqueCodes=dt.AsEnumerable().Select(Function(row) row.Field(Of String)(“Fund Code”)).Distinct().ToArray()
- Loop through each FundCode as pass it as argument to VBA code i have shared
- Invoke VBA code (code as VBS file)
- Save workbook
Mark it as solution if it is you expecting to achieve.
So in the for each loop we need to use vba code correct ?
yes ,vba code expect a FuncCode
Main.xaml (11.5 KB)
Hope this helps
Mark it as solution if helped
Showing document is invalid
Can you send in zip it would be better?
While running invoke vba getting this error
hey send me the vba code here and xlsx file
let me know once you find solution
this is working solution for GA, fix it for other FundCode
test.zip (21.9 KB)
Hi it is working for GA not MF while its working for GA as it was filtered with GA that is why it is not working for MF?