Referring Cell range contained in variable in SUM formula - vba

I have two cell addresses stored in my two variables
saddr and eaddr (starting and ending cell addresses respectively)
I have to find the some of all cells in between the range and have the value assigned to another random cell. My code below throws an error. I am pretty sure i am not using the variable containing the cell addresses in the right format. Please help
Code:
saddr = Cells(x, 3).Address
eaddr = Cells(x, (3 + 6)).Address
Worksheets("Sheet2").Range("C2").Select
ActiveCell.Formula = "=Sum(Sheet1!:"&saddr&":"&eaddr&")"
The last line throws up this error. Can you tell me how to use the variable cell references correctly in the formula?

You probably want
ActiveCell.Formula = "=Sum(Sheet1!" & saddr & ":" & eaddr & ")"
Even if this gets you going, you better check/fix a few things.
I strongly recommend you check this.

Related

How to combine indirect and direct references in VBA?

Issue:
I need to call direct and indirect references. I have a column that shifts to the right every time the program is run. The column is being used to collect the sum of the preceding columns. (I'm aware I can combine the sheet1 within the with, however there are pieces of code between the with and sheet select and this particular question focuses on just the .value section.)
Code 1:
Sheets("Sheet1").Select
With Range("AL2")
.Value = "=SUM(B2:RC[-1])"
End With
B2 is being inserted as a string, instead of as a cell name.
I've also tried:
Sheets("Sheet1").Select
With Range("AL2")
.Value = "=SUM(range("B2") & ":RC[-1])"
End With
Sheets("Sheet1").Select
With Range("AL2")
.Value = "=SUM("& B2 & ":RC[-1])"
End With
This particular set of code does not run correctly and returns:
SYNTAX ERROR
So, the problem in this specific instance is that you are mixing R1C1 and A1 style notation.
Anyway, I'd change your code to set the .Formula property instead of the .Value property, and specifically change it to .FormulaR1C1 if you want to use that notation or leave it as .Formula for A1 notation.
Anyway, your original code:
Sheets("Sheet1").Select
With Range("AL2")
.Value = "=SUM(B2:RC[-1])"
End With
This is setting B2 as a string because Excel is recognizing the RC notation, correctly interpreting that "RC[-1]" is a cell reference, but then not understanding what "B2" means.
So, I'd use something like this:
With Sheets("Sheet1").Range("AL2")
.FormulaR1C1 = "=SUM(R2C2:RC[-1])"
End With
That however would leave $B$2 as an absolute cell reference, and AK2 as a relative reference, which is kind of gross. Depending on your needs you could do this:
With Sheets("Sheet1").Range("AL2")
.FormulaR1C1 = "=SUM(R2C2:R2C37)"
End With
Which would lead to both being absolute references.
If you want something more dynamic, you'll need to use string manipulation on the string you're inputting into the worksheet. For instance, if you have r1 as a range object (with address B2) and r2 as a range object (with address AK2), you could do this:
With Sheets("Sheet1").Range("AL2")
.Formula = "=SUM(" & r1.address ":" & r2.address & ")"
End With
If you have questions, let me know.

VBA: Using VLookUp to Set Dynamic Values in loop?

New VBA user here...
I have set up a Macro that runs for each file in a folder, as a loop.
In one part of the macro, there is an equation that contains a value that differs for each file.
Here is the equation (the value that changes for each file is 0.2483, everything else stays the same):
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-83]C)/R[-85]C)*1000*0.2483"
I have set up a table that lists each file name and its corresponding value in another workbook. I have attempted to use vLookUp within the equation to find the value based on the file name - the file of which is active.
Here's what I have so far, for which I get a "Run-Time Error: 1004" on:
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-88]C)/R[-90]C)*1000*(=VLOOKUP(" & ActiveWorkbook.Name & ",'[Calibration
Curves.xlsm]Sample Weights'!A2:B10,2,FALSE))"
Suggestions on how to make this work?
Try getting rid of the = in the formula and also use consistent cell notation (i.e. don't mix A1 notation with R1C1 notation):
ActiveCell.FormulaR1C1 = "=(((" & signal_array(Element, 1) & ")-
R[-88]C)/R[-90]C)*1000*(VLOOKUP(" & ActiveWorkbook.Name & ",'[Calibration
Curves.xlsm]Sample Weights'!R2C1:R10C2,2,FALSE))"

VBA Error method of '_default' if object 'range' failed when inserting formula into cell

I am trying to use VBA to put a formula into a cell. This is the formula.
Range(Cells(2, 15)).FormulaR1C1 = "=SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7clrow,Classes!R[]C[-13]))"
The error is inside the quotes on the right. I've tried taking off range, using A1 notation, and everything else I can think of. I have several extremely similar lines before and after that all work fine, so I'm not quite sure what I'm missing. Thanks for any help.
EDIT: Here is the line several people have suggested:
Cells(2, 15).FormulaR1C1 = " =SUMPRODUCT(COUNTIF('All Failing Classes'!$G$2:$G$" & lrow & ",Classes!B2))"
or
Cells(2, 15).FormulaR1C1 = "=SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7C" & lrow & ",Classes!R[]C[-13]))"
Which is the same as far as I can tell.
While Mat was helping me I accidentally added a space in front of the = to create the line:
Cells(2, 15).FormulaR1C1 = " =SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7C" & lrow & ",Classes!R[]C[-13]))"
and that line goes in just fine? When I move to XL and take the space out, the formula works as intended. Does this help narrow down what I'm doing wrong?
Edit 2: Here is what I was doing wrong - I had redefined a variable before that line that referenced another sheet. When I did that the program apparently kept referencing that sheet, and gave me my issues. When I changed and added the sheet before the cells (sheets(blah).cellc(blah) then it worked. Thanks to everyone (especially Mat) who helped out.
Does =SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7clrow,Classes!R[]C[-13])) work as a formula?
If it doesn't work (and no, it doesn't), then Excel itself can't assign the broken formula to the cell you're typing it in - for the exact same reason, VBA won't be able to do it either.
So you mean lrow to be evaluated by VBA before it's inserted into the formula, right? For this to happen, VBA needs to be able to see that lrow variable. Being in a string literal, it doesn't, and it can't guess at your intentions either: as far as VBA is concerned, this is what's happening:
SomeObject.SomeProperty = "some string literal"
That's all.
You need to concatenate the variable's value into the string literal, the way you concatenate strings in VBA:
SomeObject.SomeProperty = "some " & someVariable & " string literal"
In other words make the right-hand side of the assignment a string-valued expression, not just a literal:
"=SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7c" & lrow & ",Classes!R[]C[-13]))"
Try this
Cells(2, 15).FormulaR1C1 = "=SUMPRODUCT(COUNTIF('All Failing Classes'R7C2:r7c" & lrow & ",Classes!R[]C[-13]))"
I'm guessing that lrow is a variable...

Way to set a formula using counter as cell reference in vba?

I am trying to set a cell's value to a formula, but the formula includes a counter because the column is always set, but the row of the cell varies during every initiation of the macro. How do I set this up? This is what I have, but it keeps giving me an object-defined error.
counter = activecell.row
Range("A1").FormulaR1C1 = "=VLOOKUP(AH & counter &, reference)"
The reference part is set up correctly, but I don't know how to get the first part to work.
Two things, you do not want R1C1 format and vba variables need to be outside the "" and concatenated with &
Range("A1").Formula = "=VLOOKUP(AH" & counter & ",...

Summing cells from another sheet using offset

I'm trying to get this code to work for summing cells:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum("
&Worksheets("Sheet1").Range("A3").Offset(2*i,j).Address & ":" &
Worksheets("Sheet1").Range("A7").Offset(2*i,j).Address & ")"
It keeps giving me the right cells but from the wrong sheet. So for the first iteration I get sum(A3:A7) in cell C3 of Sheet2 but the A3:A7 stays referenced to Sheet2 not Sheet1.
Thanks!
You need to specify the name of the sheet in the formula too. Your code will work if you write it like this:
Worksheets("Sheet2").Range("C3").Offset(i, j).Formula = "=Sum(Sheet1!" & _
Worksheets("Sheet1").Range("A3").Offset(2 * i, j).Address & ":" & _
Worksheets("Sheet1").Range("A7").Offset(2 * i, j).Address & ")"
Try this code - it uses the External:=True parameter of .Addressto retrieve the full address. While this also includes the workbook name, Excel will remove this automatically so you end up with Sheet1!A3. Also note that I used the range A3:A7 as source as .Address can handle multi-cell ranges and you don't need to take care of it manually:
Sheets("Sheet2").Range("C3").Offset(i, j).Formula = "=SUM(" & _
Sheets("Sheet1").Range("A3:A7").Offset(2 * i, j).Address(External:=True) & ")"
Be aware that hard coding references such as A3 can lead to bugs in the long run, as the user (or even the developer at some stage) might modify the sheet structure. It is best practice to use named ranges, i.e. create a named range for each cell/range you refer to and then access it in VBA with SheetX.Range("rngStartCell") or similar!