Converting/transforming sum(A1:D1) into A1+B1+C1+D1 in Excel

2

0

So, the situation is this, I want to know if there's a way to convert the sum of a range of cells into the sum resulted from adding each cell, separately, as shown in the title. For instance, say one has this computation in cell E1, E1 = SUM(A1:D1) and wants to split it into sum of the separate component cells, like in E1 = A1 + B1 + C1 + D1.

Also, if it would be great if someone could indicate how to do this for a range of cells, like in the case: E1 = SUM (A1:D2) being converted into E1 = A1 + B1 + C1 + D1 + A2 + B2 + C2 + D2.

EDIT: Someone suggested that I use VBA, and I thought about that too. If anyone has some suggestions on the matter, it would be greatly appreciated (I'm not very good at programming in VBA, although I know the basics and I'll give it a try on my own.

streamline

Posted 2017-04-07T05:06:09.830

Reputation: 21

3I'm confused why you need to do this... – Aurus Huang – 2017-04-07T06:10:31.810

Short answer: no, you can't do it unless typing it manually. I agree with previous comment: why do you need it? The result will be exactly the same. – Máté Juhász – 2017-04-07T06:17:01.117

Hi, @AurusHuang & Mate, there are two main reasons, first, to show students how to work on formulas (a little bit complicated to enter into details), and secondly, think of a scenario where you need to shuffle columns around, and you have on column with sums of other (say previous) columns. – streamline – 2017-04-07T07:04:11.987

Thank you, @Louis, for edits. If the question is answered or inadequate, please close it. – streamline – 2017-04-07T07:07:36.163

I think you can do this with VBA. It will be a little bit tricky and complicated. But you have access to all elements of the range, you can iterate over the elements and you can derefer the single elements of the range. – IQV – 2017-04-07T07:25:29.320

Hi, @IQV, those were my thoughts, exactly. Still, I was wondering if there's some command / function in Excel that I didn't know about. Also, I know a little bit of macro programming, but I'm afraid I won't produce the cleanest code. Do you have any suggestions? Thanks for your input! – streamline – 2017-04-07T07:39:46.377

1It's a long time I wrote VBA-macros. But the Range-function, simple for-loops and the functions Cells and Offset should do it for your problem. – IQV – 2017-04-07T07:43:34.850

Why does it matter if you produce clean code? You're writing a macro, not a program! Second, you can use Range("A1").Formula to get the formula. From here, you perform the logic and replace the cell contents – Dave – 2017-04-07T08:16:56.973

a simple program in any programming language could do that, you could hire somebody to write a web interface to do that, it may cost you but not much, like somebody may do it for $10 – barlop – 2017-04-07T08:35:25.693

@barlop, the question was not about paying someone to do it, but to learn how to do it. I have lots of students who major in computer sciences and that can do it for free. that's not the problem... – streamline – 2017-04-07T14:18:41.727

@streamline well, you're a bit lucky to have got an answer that gave you a programmed solution.. 'cos the site for programming is stackoverflow, but they'd say they don't do homework type questions where somebody says they want a program written.. They may even have said that here, except that it was possible that maybe it didn't need programming.. If you want to learn then the expected wawy is to see ah ok it requires VBA, and then try and do something simpler like make a macro whereby if you type =A in a cell, it will say =Z and if you get stuck, ask.. And build it up to what you want. – barlop – 2017-04-07T14:45:48.750

@Barlop, I appreciate any kind of answers, even criticism if it is constructive. As I said, I wanted to show to my students that have minimal Excel skills some stuff about computing the variance for multiple samples and this entitles shuffling columns around. As for the 'gift', I asked for some advice. Being 'lucky' is a matter of interpretation. It is up to the moderators to close/block an inadequate/improper question, and to the veteran users to report/modify it, as/if needed. I'm convinced that Dave's answer will help a lot of people, undoubtedly! – streamline – 2017-04-07T20:28:14.140

Converted is a strong word. SUM(A1:D2) is the same as SUM(A1,A2,B2,...D2) is the same as =A1+A2....+D2 is the same as SUM(A1:A2, B1:B2, ..., D1:D2). I believe using VBA ins't even a factor unless you like programming for the sake of programming. For fun programmers do mundane things like this, but it's what programmers do. – ejbytes – 2017-04-19T21:47:06.743

Answers

1

Although you've tagged this with Worksheet function you talk about using VBa in the quesiton. This VBa does both of the examples you gave

Option Explicit
Sub EeekPirates()

Dim formula As String
formula = Range("B4").formula

Dim split1() As String
split1 = Split(formula, "(")

Dim temp As String
temp = Replace(split1(1), ")", "")

Dim splitty() As String
splitty = Split(temp, ":")

Dim firstCol As Integer
firstCol = AscW(Left(splitty(0), 1))

Dim secondCol As Integer
secondCol = AscW(Left(splitty(1), 1))

Dim firstRow As Integer
firstRow = Right(splitty(0), 1)

Dim secondRow As Integer
secondRow = Right(splitty(1), 1)

Range("B5").Value = ""   ' this could be updated to `B4 = ` 
Dim i As Integer
Dim j As Integer

For j = firstRow To secondRow
    For i = firstCol To secondCol
        Range("B5").Value = Range("B5").Value & Chr(i) & j & "+"
    Next i
Next j

Dim length As Integer
length = Len(Range("B5").Value) - 1
Range("B5").Value = Left(Range("B5").Value, length)

End Sub

Just remeber there is no undo, so take a back up first.

How do I add VBA in MS Office?

Example with A1:D1

enter image description here

Example with A1:D2

enter image description here

As per the comments in the code, if you update from

Range("B5").Value = ""

to

Range("B5").Value = "B4 = "

You will end up with (in B5)

B4 = A1 + B1 + C1 + D1

Dave

Posted 2017-04-07T05:06:09.830

Reputation: 24 199

Hi, @Dave! Thank you! Just out of mere curiosity, why's there no undo for it? It can't be done, or it's just the case for this solution? Only if you have the time to answer it, otherwise, thank you again. And, sorry about not posting my question in a VBA related section. I only thought about it after my edit (after IQV's comment). – streamline – 2017-04-07T14:21:55.370

Thanks again, @Dave. I was just curious. I can always duplicate the sheet, to have a backup, but I didn't know that about VBA macros. – streamline – 2017-04-07T20:22:13.643

1

Minimal working example with VBA function unroll(), which takes a reference to a cell with a single function (like sum,count, min) and unrolls its argument (a list of ranges) as a list of single cells.

Option Explicit

Function rangeText(s As String) As String
Dim i As Integer, j As Integer
i = Excel.WorksheetFunction.Find("(", s)
j = Excel.WorksheetFunction.Find(")", s)
rangeText = Mid(s, i + 1, j - i - 1)
End Function

Function rangeToList(s As String)
Dim rg As Range: Set rg = Range(s)
Dim i, j As Integer: Dim c As String
For j = 0 To rg.Rows.Count - 1
  For i = 0 To rg.Columns.Count - 1
    c = c + IIf(c <> "", ",", "") + Chr(64 + rg.Column() + i) + Format(rg.Row() + j)
  Next i
Next j
rangeToList = c
End Function

Function unroll(x As Range) As String
  Dim s As String: Dim i, j As Integer: Dim list() As String
  If Not x.HasFormula Then
   s = "Not a formula"
  Else
  s = rangeText(x.Formula)
  list = Split(s, ",")
  s = ""
  For i = 0 To UBound(list)
    s = s + IIf(i > 0, ",", "") + rangeToList(list(i))
  Next i
End If
unroll = s
End Function

enter image description here

Note: as a minimal example, it does not handle two-letters column references correctly.

* Edit *

Added Function ColumnNoToName to handle cell references with columns > 26.

enter image description here

Option Explicit

Function rangeText(s As String) As String
Dim i As Integer, j As Integer
i = Excel.WorksheetFunction.Find("(", s)
j = Excel.WorksheetFunction.Find(")", s)
rangeText = Mid(s, i + 1, j - i - 1)
End Function

Function ColumnNoToName(colNo As Integer) As String
  Dim lo, hi As Integer: Dim s As String
  lo = (colNo - 1) Mod 26
  If colNo > 26 Then
    hi = (colNo - 1 - lo) \ 26
    s = Chr(64 + hi)
  End If
  s = s + Chr(64 + lo + 1)
  ColumnNoToName = s
End Function

Function rangeToList(s As String)
Dim rg As Range: Set rg = Range(s)
Dim i, j As Integer: Dim c As String
For j = 0 To rg.Rows.Count - 1
  For i = 0 To rg.Columns.Count - 1
    c = c + IIf(c <> "", ",", "") _
      + ColumnNoToName(rg.Column() + i) _
      + Format(rg.Row() + j)
  Next i
Next j
rangeToList = c
End Function

Function unroll(x As Range) As String
  Dim s As String: Dim i, j As Integer: Dim list() As String
  If Not x.HasFormula Then
   s = "Not a formula"
  Else
  s = rangeText(x.Formula)
  list = Split(s, ",")
  s = ""
  For i = 0 To UBound(list)
    s = s + IIf(i > 0, ",", "") + rangeToList(list(i))
  Next i
End If
unroll = s
End Function

Function cellFormula(x As Range) As String
  cellFormula = x.Formula
End Function

g.kov

Posted 2017-04-07T05:06:09.830

Reputation: 839

What does : do? Does this just allow multiple declarations in one line? – Dave – 2017-04-10T07:27:35.583

@Dave: see for example, Using colons to put two statements on the same line in Visual Basic.

– g.kov – 2017-04-10T07:43:11.743

Wow, @g.kov (it probably doesn't mean much, from a beginner like me, but wow), thanks! This is also great! Thanks for sharing and taking the time to post it. – streamline – 2017-04-10T11:00:49.370

Hi, @Dave, I did upvote both answers, yours and g.kov. How else! But, because I'm new to superuser, I must reach some minimum threshold for reputation (15 I think) in order for my vote to show. I'm sorry about that. I don't know, though, what you mean by accepting answers. – streamline – 2017-04-12T03:17:57.067