Sum squares

TheAaron

New Member
Joined
Oct 1, 2015
Messages
11
Hi!
I want to make a code in vba in wich the user selects a range of only one row and as many columns as he wants.
In the range the user selects, the code must sum the square of the first number, then the third, the fifth and so on (2 by 2)...
The result of this code has to appear exactly below the end of the range the user selected.
The code so far:

Sub Range()
Set MyRange = Application.InputBox("Set Range", Type:=8)
MyRange.Select
ActiveCell.Offset(MiRango.Count, -1) = "Sum"
n = ActiveCell.CurrentRegion.Rows.Count
End Sub

Thanks!
 
Or you can see if this is what you want ....
Code:
Sub SumSquareOfOddCells()
Dim R As Range, S As Double
On Error Resume Next
Set R = Application.InputBox("Select range", Type:=8)
On Error GoTo 0
If R Is Nothing Then Exit Sub
For i = 1 To R.Count Step 2
       If WorksheetFunction.IsNumber(R.Cells(1, i)) Then
              S = S + R.Cells(1, i) ^ 2
       End If
Next i
R.Cells(1, R.Cells.Count).Offset(1, 0).Value = S
End Sub
 
Upvote 0
That's not what I want, for example:

1
3
2
4
3
=14

I need to sum the square 1, 2 and the last 3 in this case.
But this code has to work on any given amount of numbers
 
Upvote 0

Excel 2010
BCDEF
11641214981
2421179
3
4218
Sheet22
Cell Formulas
RangeFormula
B4=SUMPRODUCT(--(MOD(COLUMN(B2:F2),2)=0),B2:F2,B2:F2)


Is the formula I would write. But JoeMo's code works too, what result are you getting?
 
Last edited:
Upvote 0
The resulting code:

Sub Rango()

Set MiRango = Application.InputBox("Dime el rango", Type:=8)
MiRango.Select
ActiveCell.Offset(MiRango.Count, -1) = "Sumas"
n = MiRango.Count

For q = 1 To n Step 2
x = x + MiRango.Cells(q, 1) ^ 2
Next q

MiRango.Select
ActiveCell.Select
ActiveCell.End(xlDown).Select
ActiveCell.Offset(1, 0).Value = x
ActiveCell.Offset(1, -1).Value = "Sumas"

End Sub

Thanks!
 
Upvote 0
The resulting code:

Sub Rango()

Set MiRango = Application.InputBox("Dime el rango", Type:=8)
MiRango.Select
ActiveCell.Offset(MiRango.Count, -1) = "Sumas"
n = MiRango.Count

For q = 1 To n Step 2
x = x + MiRango.Cells(q, 1) ^ 2
Next q

MiRango.Select
ActiveCell.Select
ActiveCell.End(xlDown).Select
ActiveCell.Offset(1, 0).Value = x
ActiveCell.Offset(1, -1).Value = "Sumas"

End Sub

Thanks!
Your initial post stated: "the user selects a range of only one row and as many columns as he wants.", but your code looks like it's set up for the user to select rows in a single column. Why the change?
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top