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)"
Related
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) & ")"
Recently you guys helped me solve looping through a pivot table by inserting a dynamic formula next to a pivot table and auto filling down.
'formula
frml = "=if(" & myrange.Address(0, 0) & ">0.3%," & myrange2.Address(0, 0) & ","""")"
'where i want the formula to go
pt.ColumnRange.End(xlToRight).Offset(1, 0).End(xlToLeft).Offset(1, 1).formula = frml
ActiveSheet.range("P7:P36588").FillDown
I've tried updating this formula by adding
"=if(and("
and then also adding in another range. Problem is the additional ( never shows up. I've also tried just adding "(" as it's own part but then the next part (the 'where the formula goes) stops working which makes no sense to me.
Can someone help me convert this formula into an ifand statement and also explain why the 'where the formula should go' part all of the sudden stops working?
Edit:
In total, the new addition would look like this:
"if(and(" & myrange.address(0,0) & ">0.3%," & myrange3.address(0,0) & ">$100," & myrange2.address(0,0) & ","""")
to output this formula in the corresponding cell by the pivot table
=if(and(c3>.3%,d3>$100),a3,""
the problem right now is adding that and part to the formula which would be d3. If those 2 statements work, then it should bring out a3, if not it will return nothing.
A couple of notes:
#Jeeped already answered the reason to your error - you need to add closing bracket ) to your AND.
I like to use Chr(34) to write down " inside formula strings.
I like to avoid comparing the formats inside the cell, just the values. So instead of myrange.address(0,0) & ">0.3%" I like using myrange.address(0,0) & ">0.003".
Code
frml = "=IF(AND(" & myrange.Address(0, 0) & ">0.003," & myrange3.Address(0, 0) & ">100)," & myrange2.Address(0, 0) & "," & Chr(34) & Chr(34) & ")"
You aren't closing off the AND clause with a ).
'formula
frml = "=if(and(" & myrange.address(0,0) & ">0.3%, " & myrange3.address(0,0) & ">$100), " & myrange2.address(0,0) & ", text(,))
text(,) is a good substitute for """" in a quoted string formula.
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.
I am trying to use the Application.VLookup function in Visual Basic to find a value in a different workbook. However, whenever I use it, it always returns #N/A.
This is the layout of my function. LastRow() just returns the row number of the last row. SHORTAGE_SBT is a variable containing the source workbook name. SBT_Last is the last row of SHORTAGE_SBT. The ID that I'm searching with in in the B column, hence why I use "B" & ind to refer to it.
For ind = 4 To LastRow()
Range("H" & ind).Select
ActiveCell.Value = Application.VLookup("B" & ind, Workbooks(SHORTAGE_SBT).Sheets(1).Range("A14:DZ" & SBT_Last), Range("DZ1").Column, False)
Next
I have tried recording a macro for VLookup to see if it would help me understand the problem. The macro gave me this function, which worked but could not be used because it contains the hardcoded file name instead of using the variable.
Range("H" & ind).FormulaR1C1 = "=VLOOKUP(RC[-6],'[filename.xls]Sheet1'!R14C1:R2382C130, COLUMN(R[-3]C[122]), FALSE)"
I cannot see any significant difference between the way the macro lays out the arguments of the function as opposed to mine, other than using more direct references. I have tried using direct numbers in my code but doing so hasn't helped either.
Application.VLookup("B" & ind, Workbooks(SHORTAGE_SBT).Sheets(1).Range("A14:DZ" & SBT_Last), Range("DZ1").Column, False)
"B" & ind will be searched for "literally"; it will not be transformed into a range address because it is interpreted here by VBA, not by Excel. Try:
Application.VLookup(Range("B" & ind), Workbooks(SHORTAGE_SBT).Sheets(1).Range("A14:DZ" & SBT_Last), Range("DZ1").Column, False)
' ^^^^^^^^^^^^^^^^
Combine the two approaches:
Range("H" & ind).FormulaR1C1 = _
& "=VLOOKUP(RC[-6],'[" & Workbooks(SHORTAGE_SBT) _
& "]Sheet1'!R14C1:R2382C130, COLUMN(R[-3]C[122]), FALSE)"
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