Writing a formula as Syntax in VBA - vba

I want to run a rank formula across a range of cells. I am scratching my head as to why it's not working. Lastrow is just the formula that counts the number of rows.
Range("B1:B" & Lastrow).Formula = "=RANK(A1,Offset(" & Chr$(36) & "A1" & Chr$(36) & "," & Lastrow & ",0))"
I feel like it's something wrong with Chr$(36), but when I try Chr(36) it doesn't work either. (removing these chr(36)'s and just having Offset(A1... etc) works fine).
Thanks in Advance!

It's hard to see what you want to do. The line of VBA code below will, at least, work.
Range("B1:B" & LastRow).Formula = "=RANK(ROW(),$A1:$A$" & LastRow & ",0)"
Your issue seems to be with the RANK() function. It has 3 arguments, (1) the rank, (2) the range in which to find the rank and (3) Ascending/Descending. In your formula the first argument is missing and your "Offset(" & Chr$(36) & "A1" & Chr$(36) & "," & LastRow" doesn't describe a range, read as Offset($A1$,300 with closing bracket missing.
My above formula suggests the Row number as rank, meaning 1 for Row 1, 2 for Row 2 etc. but descending, as indicated by the 3rd argument (taken from your formula) and, as second argument a range in column A between A1 and the LastRow. It probably isn't what you wanted but I hope you will be able to tweak it.

Related

Summing in VBA with a variable column and range

I know this question gets asked a lot but I haven't been able to find the answer I'm looking for. I'm trying to write a code that will be able to find a key term and then sum that column for a certain amount of rows.
I've tried simply replacing "G" in this code with my variable for the correct column (col) and I've made sure that my column variable is matching to the correct column.
Cells(subRow, col).Formula = "=SUM(G" & row & ":G" & subRow & ")"
The above, for example, works; but I would like it to look like this:
Cells(subRow, col).Formula = "=SUM(col" & row & ":col" & subRow & ")"
I've tried moving the col variable around in and outside of the quotes, and I can't seem to find a way to do it.
Thanks in advance.
Based on the case you have described, you would have to use R1C1 Reference Style:
Cells(subRow, col).FormulaR1C1 = _
"=SUM(R" & row & "C" & col & ":R" & subRow & "C" & col")"
There is also a Cells(row, column).Resize(number of rows, number of columns) option :
Cells(subRow, col).Formula = "=SUM(" & Cells(row, col).Resize(subRow - row, 1).Address(0, 0) & ")"

VBA FormulaR1C1 property across new sheet and multiple columns

I'm afraid I can't wrap my head around the FormulaR1C1 property when trying to find multiple columns in another sheet, I have these formulas currently (very slowly) autofilling:
LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("F2:F" & LastRow).FormulaR1C1 = "=IFERROR(VLOOKUP(R2,LU!C[1]:C[5]),"""")"
Range("F2:F" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,2,true),"""")"
Range("G2:G" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,3,true),"""")"
Range("H2:H" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,4,true),"""")"
Range("I2:I" & LastRow).Formula = _
"=IFERROR(VLOOKUP(A2,LU!A:E,5,true),"""")"
Range("J2:J" & LastRow).Formula = _
"=COUNTIF(A:A,'Pivot Counter'!A7)"
You can see in the top formula, I've attempted it to no success. I keep getting an application defined error, but don't know enough about the property to fix it.
The formula
Range("F2:F" & LastRow).Formula ="=IFERROR(VLOOKUP(A2,LU!A:E,2,true),"""")"
converted to R1C1 reference style is
Range("F2:F" & LastRow).FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-5],LU!C[-5]:C[-1],2,True),"""")"
To understand it completely considering the following:
the cell location where you enter the formula is the starting point
starting column is F
column A is 5 columns to the left of F, so A2 converts to RC[-5] (same row, 5 columns to the left of starting point (or think of 6th column (F) and move -5 from there))
same principle for the range lookup. You want to look in columns A:E in sheet LU. So -5 columns from column F (remember starting point is column F, even though it's now looking at a different sheet) to -1 column
Knowing this, you can convert the other formulas, even the COUNTIF.
Also, if you get stuck again, following #BruceWayne's suggestion of turning R1C1 reference style under Excel > Options > Formulas to see what the formula would be when you type in manually.

How do I set a formula in a cell using R1C1 notation from variables?

I've got a series of IF/ELSE clauses to see if items fit into one of three categories. If an item doesn't fit into any of them, its category field should simply read "SELECT".
Now, with simple text strings it works fine. But I also want to include a simple formula (as in most of the other autopopulated fields in the sheet) to blank the field if it's on an empty line. Based on the code I've used in other cases, I would want to set the cell's formula to
=IF(ISBLANK($A2, "", "SELECT")
(...where the cell itself is B2)
Problem is, the cell to look at will depend on which line it's on; A2, A3, A4 and so on. The column number remains constant, but the row number is defined by the variable iImportCounter.
I've tried both .Value and .Formula, both """ and Chr(34), and it still doesn't seem to work. The full code for that line as it stands is
Else:
wsCalc.Cells(iImportCounter, 3).Formula = "=IF(ISBLANK($" & Application.ConvertFormula(Cells(iImportCounter, 2), xlA1) & ", " & Chr(34) & Chr(34) & ", " & Chr(34) & "SELECT" & Chr(34) & ")"
Is there something terribly obvious I'm missing?
I've seen comments elsewhere that the fact that it's in a module rather than a sheet-based macro could make some of that code malfunction, but I'm not sure what or how.
It depends on what the iImportCounter starts at. If it is 3 (row 3 in B3) then the formula would be,
with wsCalc.Cells(iImportCounter, 3)
.FormulaR1C1 = "=IF(ISBLANK(R[-1]C1), """", ""SELECT"")
end with
The R in RC1 means the same row that you are putting the formula on so if you are putting on row 2 and you want to reference A2 then R is all you need. If the formula is going into B3 then you need to reference the current row minus 1 with R[-1]C1.
The C1 in RC1 means a 'locked' (absolute) column A (the first column). This doesn't seem absolutely necessary as the formula does not travel laterally.
Assuming wsCalc is a reference to a range of cells:
wscalc.FormulaR1C1 = "=IF(ISBLANK(RC1),"""",""SELECT"")"
As a working example:
Sub Test()
ThisWorkbook.Worksheets("Sheet1").Range("B1:B10").FormulaR1C1 = "=IF(ISBLANK(RC1),"""",""SELECT"")"
End Sub

Range object with variables

I've found a lot of examples using the Range identifier and a variable as the end of the range but not as the first part.
Range("B2:B" & lastrow)
works for me, as long as I identify what the lastrow variable is.
But! If I want to do something where the start of my range is the last row and the end of my range is five rows below that, how would I write that?
Range("B" & lastrow":B" & lastrow + 5)
doesn't seem to work, and I've shifted around the quotation marks into every position I could think of.
Any and all help is greatly appreciated!
You have a typo. You are missing an ampersand after lastrow. It should be:
Range("B" & lastrow & ":B" & lastrow + 5)

Specifying cells and sheets in formula using VBA

I need my macro to input the following formula:
Worksheets("U_NEDC_COLD_online_0").Cells(3, A).formula = "=IF(" & Worksheets (U_NEDC_COLD_online_0).Cells(3, AA).Value & "=" & Worksheets(U_NEDC_COLD_online).Cells(3, AA).Value & ";" & Worksheets(U_NEDC_COLD_online).Cells(3, A).Value & ";" & Worksheets(U_NEDC_COLD_online_0).Cells(3, A).Value & ")"
I've also tried the same formula with the ".Address" property and it doesn't work. What am I doing wrong?
The final formula should look like this:
=IF($AA3 = U_NEDC_COLD_online!$AA3; U_NEDC_COLD_online!A3; U_NEDC_COLD_online_0!A3)
PS: Worksheets("U_NEDC_COLD_online_0") is not the same as Worksheets(U_NEDC_COLD_online_0). (its not a typo)
Thanks.
Use commas instead of semicolons to separate the arguments of the IF statement.
Your formula appears to have a circular relationship; the formula is going into cell A3 and references cell A3... I'll leave that to you to work out.
Always use Option Explicit. Once you put that line at the top of your module, you'll get the error: Variable not defined for the variables: A, AA, and possibly U_NEDC_COLD_online and U_NEDC_COLD_online_0 that you probably intended to use as string literals. (see next bullet for a workaround)
If U_NEDC_COLD_online_0 is truly a variable/constant name and is not equal to the string literal that you use elsewhere (e.g. U_NEDC_COLD_online_0), you should really change the variable name to something else!
If the only reason you are dynamically building the formula is to accommodate variable row indexes, use this where the 3 can be replaced by a variable:
Worksheets("U_NEDC_COLD_online_0").Cells(3, 1).Formula = "=IF($AA" & 3 & "=" & "U_NEDC_COLD_online!AA" & 3 & ", U_NEDC_COLD_online!A" & 3 & ", A" & 3 & ")"
If you don't even need variable row indexes, just use this:
Worksheets("U_NEDC_COLD_online_0").Range(A3).Formula = "=IF($AA3=U_NEDC_COLD_online!AA3, U_NEDC_COLD_online!A3, A3)"