Example: To calculate the sum of values in the range of A1:A10 I can use
or I can use
I'd like to convert any range in a formula (e.g. A1:A10) to a list formart (i.e., A1,A2,An)...
I used VBA to do and it sort of works; however, there are a number of limitations:
1. I can't use more than 1 function in a cell (e.g.
)
2. It breaks when I use paranthesies before my formula (e.g.
)
I feel like I might be overcomplicating it and thought of asking if there was a better way...
Here's what I have so far:
Excel Formula:
=SUM(A1:A8)
Excel Formula:
=SUM(A1,A2,A3,A4,A5,A6,A7,A8)
I used VBA to do and it sort of works; however, there are a number of limitations:
1. I can't use more than 1 function in a cell (e.g.
Excel Formula:
=SUM(A1:A10)/COUNT(A1:A5)
2. It breaks when I use paranthesies before my formula (e.g.
Excel Formula:
=(B1*B2)/SUM(A1:A10)
I feel like I might be overcomplicating it and thought of asking if there was a better way...
Here's what I have so far:
VBA Code:
Sub conv()
Dim x, start_w, end_w, refr, refr2, lst As String
'get formula from cell without "="
x = Sheet1.Range("B1").Formula 'example "=SUM(A1:A8)"
'Find the position of ":" which is equivalent to a range
t = Application.WorksheetFunction.Find(":", x)
'Begin of function
Z = Application.WorksheetFunction.Find("(", x)
'Find reference cells
form_strt = Left(x, Z)
ref_strt = Mid(x, t - 2, 2) 'Starting Reference (i.e., A1)
ref_end = Mid(x, t + 1, 4) 'Ending Reference (i.e., A8)
'Remove non-numeric characters from starting reference
start_w = ""
For k = 1 To Len(ref_strt)
refr = Mid(ref_strt, k, 1)
If Asc(refr) >= 48 And _
Asc(refr) <= 57 Then
start_w = start_w & refr
End If
Next
'Remove non-numeric characters from ending reference
end_w = ""
For k = 1 To Len(ref_end)
refr = Mid(ref_end, k, 1)
If Asc(refr) >= 48 And _
Asc(refr) <= 57 Then
end_w = end_w & refr
End If
Next
'Column Reference
ref_col = Left(ref_strt, 1) 'example Column "A"
'Create a list
lst = ""
For i = 1 To CInt(end_w)
If i <> CInt(end_w) Then
lst = lst & ref_col & i & ", "
Else
lst = lst & ref_col & i
End If
Next i
'Return list
Sheet1.Range("B2").Value = form_strt & lst & ")"
End Sub