using variable inside SUMPRODUCT formulaR1C1 for offseting - excel-formula

My situation seems to be similar to this one, but net quite exactly the same as I need to use the offset as a variable inside 2 formulas
using variable inside formulaR1C1 for offseting
Here's something that works :
ActiveCell.FormulaR1C1 = "=SUMPRODUCT((MONTH(R[-188]C[-16]:R[-1]C[-16])=R[5]C[-15])*(R[-188]C:R[-1]C))"
Though, I need to use the Row as a variable v_LastRow and this doesn't work as I get an execution error 1004 and not sure why
ActiveCell.FormulaR1C1 = "=SUMPRODUCT((MONTH(R[-v_LastRow]C[-16]:R[-1]C[-16])=R[5]C[-15])*(R[-v_LastRow]C:R[-1]C))"
Any help is much appreciated, thanks !

You must build the formula with the value of your variable
ActiveCell.FormulaR1C1 = "=SUMPRODUCT((MONTH(R[-" & v_LastRow & "]C[-16]:R[-1]C[-16])=R[5]C[-15])*(R[-" & v_LastRow & "]C:R[-1]C))"

Related

Using vlookup function with VBA

x = Application.GetOpenFilename(Title:="Please Select the required File")
lNewBracketLocation = InStrRev(x, Application.PathSeparator)
x = Left$(x, lNewBracketLocation) & "[" & Right$(x, Len(x) - lNewBracketLocation)
Sheets("T0").Range("AC2").FormulaR1C1 = "=IFERROR(VLOOKUP(LEFT(RC[-28],8),'" & x & "]SheetName'!C1:C3,3,0,)""Not Available"")"
I am asking the user to select the file which the user needs to perform the mapping task and for that I am using the above code, but it isn't working.
The error comes from a syntax error in your formula. So Excel (not VBA) complains about that error by raising an "application-defined" error to VBA, and VBA shows it to you. You simply cannot enter an invalid formula into a cell. If you try manually from Excel, you will get something like "There is an error with that formula".
Now, while it's hard to write a complicated formula in Excel, it's even harder to get it right from VBA. Following things can help to find the problem:
(1) Don't write the formula direct into the excel range. Use an intermediate variable:
Dim formula as string
formula = "=IFERROR(VLOOKUP(LEFT(RC[-28],8),'" & x & "]SheetName'!C1:C3,3,0,)""Not Available"")"
Debug.Print formula
Sheets("T0").Range("AC2").FormulaR1C1 = formula
(2) Write the formula into Excel manually, keep the cell active, switch to the VBA environment and type in the immediate window:
? activecell.FormulaR1C1
Compare the result with the content of the formula-variable. If they are not identically, you probably have found your problem.
In your case, you have a misaligned comma in the formula. Look at C1:C3,3,0,) - it should have the closing parenthesis before the comma: C1:C3,3,0),
"=IFERROR(VLOOKUP(LEFT(RC[-28],8),'" & x & "]SheetName'!C1:C3,3,0),""Not Available"")"
I cannot guarantee that this the only error in the formula - but I hope it can help to narrow down such problems.

VBA use variable value as cell reference in formula

I am trying to enter a "=$M15-$N15" formula to cell A15. However, need to use variable (r) instead of 15 as I don't know that value until later in the code. Trying the below but it is not working. What am I doing wrong?
Range("A" & r).formula = "="$M" & r - "$N" & r"
after the code is run, I need to have a formula ($M15-$N15) in Cell A 15.
I suspect you're looking for the INDIRECT function. An example of this, based on your explanation, would look like:
=INDIRECT("M"&A1)-INDIRECT("N"&A1)
If you're actually looking for VBA, you could use the following:
Range("A" & r).formula = "=$M" & r & "-$N" & r
However, this makes me suspect this is part of some over-arching routine, and, without knowing more, I can only speculate there might be a better or more efficient method(s) for going about it.

Add formula with variable in specific cell with VBA

I'm trying to add a formula to a specific cell using VBA. The formula needs to contain a variable which I calculated before within the VBA script.
The formula is looking like this:
ActiveCell.FormulaR1C1 =
"=IFERROR(VLOOKUP(RC[-4],tbl_LTCSREP,rngCell_NoUsed.Column,FALSE),""NO LTCS
VALUE AVAILABLE"")"
The rngCell.Column variable is the one I calculated prior to that. When I do it this way, VBA just pastes the plain text in the cell and Excel itself is not able to use the value behind the variable and the VLOOKUP gives back an error.
Thank you in advance for your support.
It pastes your variable as text because you've written it as text, not a variable.
When you write "Hello someVariable", the compiler interprets someVariable as a piece of text instead of a variable.
Solution:
"Hello " & someVariable
Or for your case:
Dim str As String
str = "=IFERROR(VLOOKUP(RC[-4],tbl_LTCSREP, " & rngCell_NoUsed.Column & ",FALSE),""NO LTCS VALUE AVAILABLE"")"
ActiveCell.FormulaR1C1 = str
You'll see that we've closed off the first part of the string by adding a ". Then, we tell the compiler to concatenate (&) with the contents of the variable. Then we add another concatenation operator, and we've also added another " to signify that we are once again inputting pure text.
The result can be thought of more simply in this way:
"text" & content_from_a_variable & "more text"
That works perfectly fine.
I already tried to do that in one step like:
ActiveCell.FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-4],tbl_LTCSREP," & _
rngCell_NoUsed.Column & ",FALSE),""NO LTCS VALUE AVAILABLE"")"
This did not work either.
But I got it. Thanks again.

Dynamic file names with Excel INDEX() function

I'm doing some data crunching work in Excel. What I need to do is to fetch values from a Cell (Say D3) from sequentially numbered workbooks like 1.xlsx, 2.xlsx, 3.xlsx...so on.
I can't use INDIRECT() function as it would require all the target files to be opened (which is not possible in my case). So, I'm using the INDEX() function with the following values:
INDEX( [1.xlsx]Sheet1!D:D, 1,1 )
Now, I want the bold part of the formula to be dyamic based on the number on left column as shown in the image below:
Somebody please suggest solution to the problem at hand as I'm tired on googling :)
Thanks in advance.
Perhaps someone will prove me wrong, but I don't believe it is possible to generate such dynamic referencing without resource to INDIRECT, unless you are happy with a longhand (and static) method such as:
=INDEX(CHOOSE(ROWS($1:1),[1.xlsx]Sheet1!D:D,[2.xlsx]Sheet1!D:D,[3.xlsx]Sheet1!D:D),1,1)
though this requires that each workbook reference be entered explicitly within the formula. As such, this also means that these cannot be created via reference to any cells within the worksheet, as you might have hoped.
Or, if VBA is an option:
Sub Indexing()
Dim i As Long
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
Range("B" & i).Formula = "=INDEX([" & Range("A" & i).Value & ".xlsx]Sheet1!D:D,1,1)"
Next i
End Sub
Regards

Using a relative (unknown) reference with formulaR1C1

I'm trying to input a simple formula in the current active cell. The formula should include a rounding of a division of the cell to the left by the last cell with data on the same column (which is a total).
I'm using this:
Dim strAddress3 As String
strAddress3 = Range("G8").End(xlDown).Address
ActiveCell.FormulaR1C1 = "=+ROUND(RC[-1]/" & strAddress3 & ",2)"
Apparently, I can't use strAddress3 but I don't know why.
My apologies if this is a simple questions, I've been looking for an answer but couldn't find it.
.Address without further arguments returns the address in the xlA1 notation. I think you should use strAddress3 = Range("G8").End(xlDown).Address(true, true, xlR1C1 )

Resources