I need help adjusting an existing UDF to meet a new need.
The script below is designed to take a vertical range of cells with comma separated values, then add all the values together and display the result in the same form- Like so...
6,4,1
5,6,1
3,2,1
________
14,12,3
Make sense?
It works great. Even if I don't understand what's happening (I didn't make it, I inherited it).
What I would like it to do differently is evaluate a ROW instead of a column.
I tried it myself and was disappointed... (mostly in myself)
I appreciate your help.
_____________________________________________________
Public Function ColumnsSum(source As Range) As String
Dim Result As String
Dim aResult() As Variant
Dim Arr() As Double
Dim index As Long
Dim rowindex As Long
Dim ThisRow As Range
Dim splitter As Variant
Dim maxdepth As Long
Dim maxwidth As Long
maxdepth = source.Rows.Count
For Each ThisRow In source.Rows
splitter = Split(ThisRow.Value, ",")
rowindex = rowindex + 1
maxwidth = UBound(splitter, 1)
ReDim Preserve Arr(1 To maxdepth, 0 To maxwidth)
For index = 0 To maxwidth
Arr(rowindex, index) = splitter(index)
Next
Next
ReDim aResult(0 To maxwidth)
For rowindex = 1 To maxdepth
For index = 0 To maxwidth
aResult(index) = aResult(index) + Arr(rowindex, index)
Next
Next
Result = Join(aResult, ",")
ColumnsSum = Result
End Function
The script below is designed to take a vertical range of cells with comma separated values, then add all the values together and display the result in the same form- Like so...
6,4,1
5,6,1
3,2,1
________
14,12,3
Make sense?
It works great. Even if I don't understand what's happening (I didn't make it, I inherited it).
What I would like it to do differently is evaluate a ROW instead of a column.
I tried it myself and was disappointed... (mostly in myself)
I appreciate your help.
_____________________________________________________
Public Function ColumnsSum(source As Range) As String
Dim Result As String
Dim aResult() As Variant
Dim Arr() As Double
Dim index As Long
Dim rowindex As Long
Dim ThisRow As Range
Dim splitter As Variant
Dim maxdepth As Long
Dim maxwidth As Long
maxdepth = source.Rows.Count
For Each ThisRow In source.Rows
splitter = Split(ThisRow.Value, ",")
rowindex = rowindex + 1
maxwidth = UBound(splitter, 1)
ReDim Preserve Arr(1 To maxdepth, 0 To maxwidth)
For index = 0 To maxwidth
Arr(rowindex, index) = splitter(index)
Next
Next
ReDim aResult(0 To maxwidth)
For rowindex = 1 To maxdepth
For index = 0 To maxwidth
aResult(index) = aResult(index) + Arr(rowindex, index)
Next
Next
Result = Join(aResult, ",")
ColumnsSum = Result
End Function