Stuck on VBA code to sum xlup range

Redford Kenny

New Member
Joined
Feb 23, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

Trying to put a SUM formula a couple of rows below my array but I am having no luck.

Can you look at the code and advise please.

VBA Code:
Range("J17").End(xlDown).Offset(4, 0).Select
    ActiveCell.Offset(-4, 0).Select
   
    Dim x As Range
   
    Set x = Range(Selection, Selection.End(xlUp)).Select

   
    Range("J17").End(xlDown).Offset(4, 0).Select
    ActiveCell.Formula = "=SUM(" & x.Address & ")"

Many thanks,
Red
 
Last edited by a moderator:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
When setting a range variable (object), you do NOT use "SELECT", i.e.
this:
VBA Code:
Set x = Range(Selection, Selection.End(xlUp)).Select
should just be this:
VBA Code:
Set x = Range(Selection, Selection.End(xlUp))
(assuming your logic is correct, we cannot tell since we do not know what your data looks like).

Also, most anytime you have one line of code end in "Select", and the next begins with "Selection" or "ActiveCell", you can combine those two lines together.
Not only does it make your code shorter, but "Selections" are usually unnecessary and slow your code down (you usually do not need to "Select" ranges to work with them in VBA).

So these lines:
VBA Code:
Range("J17").End(xlDown).Offset(4, 0).Select
ActiveCell.Formula = "=SUM(" & x.Address & ")"
could simply be reduced to:
VBA Code:
Range("J17").End(xlDown).Offset(4, 0).Formula = "=SUM(" & x.Address & ")"
 
Upvote 0
Solution
In future please you code tags rather than quotes when posting code How to Post Your VBA Code

Replace all your code with
VBA Code:
Range("J17").End(xlDown).Offset(4, 0).FormulaR1C1 = "=sum(r17c:r[-4]c)"
 
Upvote 0
Hello,

They both worked perfectly, thanks so much. My VBA is pedestrian at best so I appreciate the tips on the range and the R1C1 sum (very interesting how it picks up the entire cell after!).
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,766
Messages
6,126,758
Members
449,336
Latest member
p17tootie

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