vba adding across columns

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
985
Office Version
  1. 2010
Platform
  1. Windows
Hi All.
working on:
VBA Code:
Sub Add_acr_col()

      Dim Cl As Long
      
                  For Cl = 2 To Cells(Columns.Count, "B").End(xlUp).Columns
                                 Cells(2, "A").Value = Cells(2, "A").Value + Cells(2, Cl).Value
                  Next Cl


End Sub
1620955246072.png

simple and short for this post. I want to add across column, using just the for...next loop,
the three lines I have give me the wrong answer.
Is a dynamic line, reason I use End(xlup).
Thanks for reading.
Your input matters, Please.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi, you are confusing columns & rows ! So you must read VBA help of Cells statement …​
Anyway :​
VBA Code:
Sub DemoDumb()
    For C% = 2 To [B2].End(xlToRight).Column:  [A2].Value2 = [A2].Value2 + Cells(2, C).Value2:  Next
End Sub
As any good VBA coder does very not need any loop even with a so called 'dynamic line' !​
 
Upvote 0
Solution
Marc L Thank you, you make simple and sharp. excellent.
Confuse, yes I am.
To find the right books, or the best teacher I think is difficult, I think you are lucky, you have everything figure out, right school, right connections, right books
would be nice if you let me know the path to learn like you. If you don't mind.or expensive courses I never believe there are dummies or smart people, is about lucky star. for me must be a 500 pages book about only for next loop, no just couple examples and by like most of the books. thanks Marc L
I really like your comments.
 
Upvote 0
As I never read a book : just comparing code generated by the Macro Recorder and the VBA help …​
Later with the rising of some Excel forums just reading some threads …​
So without useless looping :​
[A2].Value2 = Application.Sum([B2].Resize(, [A2].CurrentRegion.Columns.Count - 1))
[A2].Value2 = Application.Sum(Range("B2", [B2].End(xlToRight)))
[A2] = [SUM(2:2)-A2]
 
Upvote 0

Forum statistics

Threads
1,223,713
Messages
6,174,038
Members
452,542
Latest member
Bricklin

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