Sum a range in VBA With R1C1 Notation (1 Viewer)

Insane_ai

Not Really an A.I.
Local time
Today, 06:13
Joined
Mar 20, 2009
Messages
264
I need to get a sum of columnar data from an excel spreadsheet. I can get some data but not all because I won't know which column I'm working in until run time.

I have an idea of converting the numerical column reference to an letter but I'd rather just use R1C1 formatting if possible to simplify the procedure.



Code:
'This Fails due to the colon
Debug.Print WorksheetFunction.Sum(R11C & intColumn:R1017C & intColumn)
 
'This works but only sums the first and last of the range.
Debug.Print WorksheetFunction.Sum(R11C & intColumn,R1017C & intColumn)
 
'This also fails as I am using Range improperly but don't understand why yet.
If Range.Select(R11C[intColumn],R1017C[intColumn])

Thanks in advance
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:13
Joined
Sep 21, 2011
Messages
14,234
I think you can use Range(Cells notation?

Code:
    Range(Cells(2, iTCol), Cells(lLastRow, iTCol)).FormulaR1C1 = "=COUNTIF(C1,RC1)"
 

Insane_ai

Not Really an A.I.
Local time
Today, 06:13
Joined
Mar 20, 2009
Messages
264
I tried
Code:
Debug.Print WorksheetFunction.Sum(Range(Cells(R11C & intColumn), Cells(R1017C & intColumn)))

And it looks like it wants to work but it produces a zero result where is should be a sum in the millions.


Next try is what the Gasman posted above.
 

Insane_ai

Not Really an A.I.
Local time
Today, 06:13
Joined
Mar 20, 2009
Messages
264
This Fails with invalid or unqualified reference at "Sum" :

Code:
Debug.Print WorksheetFunction.Sum(Range(Cells(R11C & intColumn), Cells(R1017C & intColumn))).FormulaR1C1 = "COUNTIF(C1,RC1)"
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:13
Joined
Sep 21, 2011
Messages
14,234
That is not what I posted?

Even I am having a problem seeing how it worked, as this was a long time ago, but that code is using (row, column) notation.

My column number was determined by finding the required column (which could move around)

Code:
    i = Application.WorksheetFunction.Match("Outstanding Items", Range("A1:AZ1"), 0)
    iTCol = i
    lLastRow = Range("B" & Rows.Count).End(xlUp).Row

then apply that to add formulae

Code:
Range(Cells(2, iTCol), Cells(lLastRow, iTCol)).FormulaR1C1 = "=COUNTIF(C1,RC1)"

https://stackoverflow.com/questions/17852071/set-sum-formula-in-excel-using-vba
 

Insane_ai

Not Really an A.I.
Local time
Today, 06:13
Joined
Mar 20, 2009
Messages
264
Solution:


Code:
Debug.Print objXL.Application.WorksheetFunction.Sum(Range(Cells(11, intColumn), Cells(1017, intColumn)).Value)

I had to reference objXL first to get WorkSheetFunction to work. The rest of the solution comes from Gasman and one of my co-workers assisting me with the .Value at the end.

Thank you to all that helped, I hope this helps someone else later.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:13
Joined
Sep 21, 2011
Messages
14,234
I would have just put the Sum formula into the required cell, the same as you would manually, after determining the range involved.

Solution:


Code:
Debug.Print objXL.Application.WorksheetFunction.Sum(Range(Cells(11, intColumn), Cells(1017, intColumn)).Value)

I had to reference objXL first to get WorkSheetFunction to work. The rest of the solution comes from Gasman and one of my co-workers assisting me with the .Value at the end.

Thank you to all that helped, I hope this helps someone else later.
 

Insane_ai

Not Really an A.I.
Local time
Today, 06:13
Joined
Mar 20, 2009
Messages
264
Thank you Gasman, I should have been more clear that I am not working in a cell. I am manipulating data within a spreadsheet from inside MS Access.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:13
Joined
Sep 21, 2011
Messages
14,234
Ah, ok, that makes more sense.

I've only ever inserted formulae using VBA, not made calculations on the fly, so thanks for that.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:13
Joined
Feb 28, 2001
Messages
27,140
The need for using .Value is the rare exception because for an Access object, .Value is the default property. But that is not an Access object. You have opened Excel's innards and thus have to live by those standards. And in Excel, .Value is a special property. So that explains why you needed the .Value property.
 

Users who are viewing this thread

Top Bottom