Using relative/abs references to insert a function using VBA - vba

I have some formulas I am attempting to insert into my worksheet through the use of VBA. The rows are dynamic. Whether I attempt to insert the formula with a standard formula or R1C1, part of the formula changes the relative references where I need but not on another.
Range("B25:B" & LastRow).Formula =
"=IFERROR(IF(ISTEXT(E$21),AVERAGEIF(BottomLeftTable[Billing
Name],$A25,BottomLeftTable[Margin]),""""),"""")"
This causes the istext portion to evaluate only E21 when it should be E21 in B25, F21 in B26, G21 in B27, etc. What I find weird is the inserted formula will actually change the $A25 as needed. It places A25 in B25, A26 in B26, A27 in B26, etc.
From what I've read, VBA does not allow R1C1 functions to change the reference so ultimately my question is can the standard insert formula work to paste the formulas where I need them while changing the reference as the formula fills down rows and if so, how to go about doing so? Thank you!

FWIW, here is a loop that would individually set the formula in each cell:
Dim r As Long
For r = 25 To lastRow
Cells(r, "B").Formula = "=IFERROR(IF(ISTEXT(" & Cells(21, r - 20).Address & _
"),AVERAGEIF(BottomLeftTable[Billing Name],$A" & r & _
",BottomLeftTable[Margin]),""""),"""")"
Next r

Related

VBA script to insert INDEX/MATCH formula with dynamic reference/arrays

I'm looking to simply insert an INDEX/MATCH formula in the column next to an existing selection (this will be part of a larger Sub). The reference/look-up array in the formula needs to be dynamic.
So far I've managed to successfully insert the formula. However, the reference/lookup-arrays have been manually input by me as I know the current Range (which is likely to change):
Selection.Offset(0, 1) = "=INDEX($J$3:$J$31,MATCH(INDIRECT(""RC[-2]"",0),$H$3:$H$31,0))"
How can I amend the $J$3:$J$31 and the $H$3:$H$31 to update dynamically? I've had success separately with something like 'Range("J3", Range("J3").End(xlDown))', but I can't seem to work it into the code above.
You can integrate variables into the formula, it becomes a bit tricky, and I imagine there are better ways to accomplish this, but it's the method I use when I need dynamic ranges in formulas in VBA. I'll provide an example below that should help:
I'm assuming the J3 and H3 will remain the same, but the end of the range is what you expect to change. If this is not the case, let me know.
You're essentially replacing the "31" in your range reference with " & [variable] & " (quotes included).
EDIT: I use Sheet1 as an example for the row count; so just update that to whatever the applicable sheet & range would be.
Dim rCount As Long
rCount = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
Selection.Offset(0, 1) = "=INDEX($J$3:$J$" & rCount & ",MATCH(INDIRECT(""RC[-2]"",0),$H$3:$H$" & rCount & ",0))"

Writing a formula with concatenated parts into a cell

Scenario: I have a code that should write a formula to a worksheet cells. This formula is for an API to retrieve some value. My formula is inside a loop (this is done for multiple columns) and references the first row for an identifier.
The original formula:
=FS(B1;"FI(DATE,,DATE)")
The modified formula with the floating reference (inside the loop):
For i = 1 To lColumn
If wb.Worksheets("Dates").Cells(i, 1).Value <> "" Then
wb.Worksheets("Dates").Cells(i,2).value = "=FS(" & i & "1;"FI(DATE,,DATE)")"
End If
Next i
Where lColumn is some pre-defined number.
Issue: I keep getting the "Unexpected end of statement" error in the formula part of the loop.
What I already tried: I tried different variations, repositioning the "s and 's, for example:
wb.Worksheets("Dates").Cells(i,2).value = "'"=FS(" & i & "1;"FI(DATE,,DATE)")""
or
wb.Worksheets("Dates").Cells(i,2).value = "'=FS(" & i & "1;"FI(DATE,,DATE)")"
or
wb.Worksheets("Dates").Cells(i,2).value = "'""=FS(" & i & "1;"FI(DATE,,DATE)")"
and so on. But the error still persists.
Question: What is the proper way to do this operation?
Working with formulas in VBA is a little bit tricky:
To write a formula, use the range.formula property, not the .value.
You have to write the formula as if you are using an english Excel. Parameter-separator is comma (not semicolon).
If a formula needs a quote, double it so that the VBA compiler understands that you want a quote within a string.
I find it helpfull to write a formula into a variable before assigning it - you can check in the debugger if it is exactly how it should before assigning it.
To check how the formula should look like, write it into a cell, change to the VBA-editor, open the immediate window and write ? activecell.formula
Try (untested as the formula you need is not valid to us):
with wb.Worksheets("Dates")
dim f as string, adr as string
adr = cells(i, 1).address(false, false) ' get rid of Dollar signs
f = "=FS(" & adr & ",""FI(DATE,,DATE)"")"
.Cells(i, 2).formula = f
end with
wb.Worksheets("Dates").Cells(i,2).formula = "=FS(" & Cells(1, i).Address(0,0) & ";""FI(DATE,,DATE)"")"
There may be a better way to convert the column number to a letter (which is the problem you are having, along with the double quotes)!

r1c1 formula not working

I am trying to rank values using column J and tie breaker in column K with the result populating in column N. Column J & K are values.
Somehow it only generates one value wherever my cell is pointed at, which means if I run the vba codes at cell C19, it will just populate value 1 in C19, not from N6 where I want the results to be.
Here are my codes,
Sub test()
Dim LR1 As Long
LR1 = Range("J" & Rows.Count).End(xlUp).Row
With Range("N6:N" & LR1)
ActiveCell.FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
End With
End Sub
I am not sure what went wrong. I tried to do it manually using the excel formula and its working fine but not my vba codes.
ActiveCell is your issue.
Change
ActiveCell.FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
To .FormulaR1C1 = "=1+SUMPRODUCT(--(R6C10:R33C10<RC[-4]))+SUMPRODUCT(--(R6C11:R33C11<RC[-3]),--(R6C10:R33C10=RC[-4]))"
Remove that and it should do what you want.
You will want to fully qualify your Range references that way they aren't depending on the ActiveSheet. This will provide you with consistent behavior and results.

putting a string from a cell into the middle of my index-match VBa script

I am trying to use the index-match formula to reorganize data such that all of the names in column J that have a matching value in column A will be placed in the same spot. I'm going to do this for 5 different columns so that the 5 names on a team will be in the same row as the name of the corresponding client.
My issue is that the index-match formula needs to be able to dynamically shorten or lengthen the size the arrays it uses based on how many clients there are when the VBA script is run.
I can dynamically determine what numbers I need in the formula using COUNTA, but the code will not compile when I try to put it in my formula. My formula is below
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & ",MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0)"
As you can see I need the strings in cells J3 and J1 to be used as the arrays for the index match. J3 = $J$2:$J$2369 and J1 = $A$2:$A$1113
When I run the code it gives me a "Application-Defined or Object-defined error."
You need to use the Range member of worksheet
so use 'test sheet two'!Range("J2:J2369") rather than 'test sheet two'!("J2:J2369").
The following runs
ActiveCell.Formula = _
"=INDEX('test sheet two'!Range(""" & Range("J3") & """) _
,MATCH(Sheet1!A5,'test sheet two'!Range(""" & Range("J1") & """),0))"
Your formula was not including the column criteria for the INDEX Function.
Try:
Range("B7").Select
ActiveCell.Formula = "=INDEX('test sheet two'!" & Range("J3") & "," & _
"MATCH(Sheet1!A5,'test sheet two'!" & Range("J1") & ",0), 1)"
Notice the additional , 1)" on the end of the formula.
Also, you do not have to first Select the cell which you want to enter the formula in, you could just use:
Range("B7").Formula =

Excel Referencing cell entries for use in formula

I'm not sure exactly what to call what I'm trying to do, so searching it has been tough. Basically I have a table of equations, each row has a different equations/references a different column, but all of them reference the same range of rows. i.e. Eq. A = average(A200:A400), Eq. B = sum(C200:C400), etc...
From file to file the range of rows changes, so what I want to do is be able to do is enter the start and end rows into cells and have them auto populate the equations. If anyone could tell me how to do it just for one cell and not an entire table, I could figure it out from there.
Thanks!
Sounds like the INDIRECT function would accomplish this. It allows you to enter in text to be interpreted as a cell reference.
For instance, lets say you wanted the range to cover A200:A400 for a given sheet, and you wanted that desginated in cell A1 of that sheet. In cell A1 you would just type in "A200:A400" then, in the actual equations, you would have:
=AVERAGE(INDIRECT(A1))
You can obviously split this further down, but thats the concept of it.
You could create a form with a few text boxes. Enter the start and end row. Then your code could go through and enter the formula.
Something like this.
Dim lRow as long
Dim lEnd as long
lRow = val(txtBoxStartRow.text)
lEnd = val(txtBoxEndRow.text)
ws.Range("A" & lEnd + 1).Formula = "=average(A" & lRow & ":A" & lEnd & ")"
ws.Range("C" & lEnd + 1).Formula = "=average(C" & lRow & ":C" & lEnd & ")"
This should do:
=AVERAGE(INDIRECT(ADDRESS($B$1;ROW())):INDIRECT(ADDRESS($B$2;ROW())))
In that code I'm assuming cells B1 and B2 contain the limits (you can replace these references with hard number), to use your example: B1 = 200 and B2 = 400.
If you then place this code in any row, you'd get average("rowNumber"200:"rowNumber"400).
Address() gives you the right range reference
Indirect() makes a range out of it
Then you can wrap it in whatever function you like.