• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Excel UDF Question - SUM columns on other sheets

Lord Zado

Senior member
I've been wracking my brain trying to piece together a UDF to save me a ton of time, but I'm having difficulty.

So here's the situation:
- I have several comma delineated .txt files (10-20 or so) that are imported into an Excel file.
- Each .txt file gets its own Sheet in the Excel workbook.
- The first sheet in the workbook is a table that references all of the other sheets to make various calculations.
- Each sheet of raw data has varying rows and columns. Columns vary from 4-20 and rows from 10-1000.
- The first row in each sheet is a column header from the imported .txt file and every sheet has a column called "Acres".
- What I want to do is search a sheet for the column "acres" and then sum that column.

So my thought was to create a UDF called SumColumn, that takes the sheetname and desired column name as arguments.

Something like:

Function SumColumn(SheetName As String, ColName As String)
*Search Row 1 of SheetName and find the word ColName and return column*
*Sum corresponding column*
End

I did a lot of googling and looking at various code snippets trying to figure out a way to do this seemingly simple operation, but I'm having no luck. I'm having trouble with the syntax of returning the column value as a letter, not a number (which I assume you need to later sum the column). I'm also having trouble with the syntax of referencing other sheets, with the sheet name being a variable. Should it be declared as a string initially or what?

Currently I just manually find the column named acres and do =SUM(SheetA!E:E) or =SUM(SheetB!G:G) and so on. That requires me to look at each sheet and manually enter the column, but it's possible the columns could change on a given sheet, so checking for the column name would be best.

Any help would be appreciated. Thanks!
 
Here's a straight up excel formula:

=SUM(INDIRECT(A1 & "!" & SUBSTITUTE("x:x","x",SUBSTITUTE(ADDRESS(1,MATCH(B1,INDIRECT(A1 & "!1:1")),4),"1",""))))

Where A1 is a cell with the sheet name and B1 is a cell with the column header name.
 
Here's a straight up excel formula:

=SUM(INDIRECT(A1 & "!" & SUBSTITUTE("x:x","x",SUBSTITUTE(ADDRESS(1,MATCH(B1,INDIRECT(A1 & "!1:1")),4),"1",""))))

Where A1 is a cell with the sheet name and B1 is a cell with the column header name.

Thanks a lot. I plugged this in and it gave me an error, so I did some searching and I just needed to add a 0 for the match_type argument in the Match function so it would be an exact match. Popped up the correct number. I should be able to create the function from this.
 
Back
Top