I need to use vba to place an IF function into a cell which is 8 columns to the right of the active cell. The IF function compares the value of the cell 2 columns to the left of the active cell.
So far i have tried various variations of the below but can't seem to get it to work, any ideas?
targetRange.Ofsett(0, 8).Formula = "=IF(" & targetRange.Ofsett(0, -2).Address & " =""FAIL"",""Enter Required Data"","""")"
targetRange.Ofsett(0, 8).FormulaR1C1 = "=IF(R[0]C[-10]=""FAIL"",""Enter Required Data"","""")"
Related
I have an excel macro that creates a new excel sheet and fills it with a variable number of rows. I have a count of the number of rows stored in a variable. I'm using an R1C1 formula to multiply two cells together based on my row count. In the macro the formula looks like it's correct (if I add a break point and put a watch on it) but when the macro finishes the formula in the cell is incorrect.
VB Code to generate formula:
MainSheet.Cells(RowCount + 6, 6).FormulaR1C1 = "=R[" & RowCount + 3 & "]C" & "*" & "R[" & RowCount + 4 & "]C"
Watch on the formula with a break point reveals this: "=R[430]C*R[431]C"
Actual cell data after macro finishes: =F863*F864. I need this to be =F430*F431
By using the [] you are telling vba that you want the relative positioning. Which means it will add(or subtract) the number in the [] to the current row and/or column in which the formula is being placed.
Remove the RowCount:
MainSheet.Cells(RowCount + 6, 6).FormulaR1C1 = "=R[3]C*R[4]C"
I am trying to get an Excel If formula to apply to all rows with content in a sheet. If the cell value entered after the macro is applied is y, then something appears in the next column, if it is n something else appears. It looks like this:
For Row = 2 To LastRow
Cells(Row, 8).Formula = "=IF(""H"" & Row & ""="" ""Y"",""Included"",IF(""H"" & Row & ""="" ""N"",""SYS"", """"))"
Next
I believe the issue is with where I putting the quotes and how many, but I can't figure that out for the life of me. I was able to get the formula to work when I chose a specific cell, ie. H2, but the issue began when I tried to use a variable.
Any help or tips or references would be appreciated!
I find it easier to replace tokens than to use concatenation, as it's easier to manage the quotes that way.
Const FRM As String = "=IF(H<r>=""Y"",""Included"",IF(H<r>=""N"",""SYS"", """"))"
For Row = 2 To LastRow
Cells(Row, 8).Formula = Replace(FRM, "<r>", Row
Next
I have vlookup formula that takes the value of A2 and returns the corresponding match found in my Lookup_Table in cell O2, this lookup continues for the rows underneath.
How would I modify this code to lookup a range of values in A:M, with the results placed in O:AA? Or do I have to manually code each column separately?
With Sheets("Example")
.Range("O2:O" & .Range("A" & Rows.Count).End(xlUp).Row).Formula = _
"=IF(ISERROR(VLOOKUP(A2,'Lookup_Table'!A:H,4,FALSE)),0,VLOOKUP(A2,'Lookup_Table'!A:H,4,FALSE))"
.Range("O2:O" & .Range("A" & Rows.Count).End(xlUp).Row).Value = _
.Range("O2:O" & .Range("A" & Rows.Count).End(xlUp).Row).Value 'Comment out if you'd like to leave the above formula in place
End With
Assuming Lookup_Table = 'Lookup_Table'!A:H, you could try something like:
With worksheets("Cross_Walk") ' Assumes Activeworkbook
.Range("E2:H" & .Range("A" & .Rows.Count).End(xlUp).Row).Formula = _
"=Iferror(VLOOKUP(A2,'Lookup_Table'!$A:$H,4,FALSE),0)"
End With
We assign the formula to range E2:H? where ? is whatever the last row is determined to be.
Excel observes relative and absolute references when assigning the same formula to a range of cells.
So since A2 in the VLOOKUP has a relative row and column reference (no $ signs), it will change to B2 when the formula is entered in F2 -- and so forth for the remaining columns and rows.
Also, if you're going to test whether the result of the VLOOKUP is an error, and then conditionally assign either zero or the matching value, you may as well just use IFERROR in your formula -- rather than performing the VLOOKUP twice.
I am building a simple macro named Update for a form control button in excel. When the button is pressed, I want a vlookup to execute in the button's sheet (named "Checker"), against the $B$7:$E$1048576 range in another sheet named "All Account Log" within the same workbook.
This vlookup formula should populate in rows 5-105 in column 5 ("E") of Checker, and for each respective row should take an input from column B to check against All Account Log's range. Here is the code I have so far:
Sub Update()
Dim Month As String
Dim i As Integer
Month = Cells(2, 2).Value
For i = 5 To 105
Cells(i, 5).Value = "=IFERROR(VLOOKUP($B" & i & ",'All Account Log'!$B$7:$E$1048576,4,0)," & Month & ")"
Next i
End Sub
If the vlookup is successful, the formula in each row of column E should pull back the respective column's data. If the vlookup does not find a match, the formula should copy the value from Cell(2,2), which is a string input by the user - a month's name. When I run the macro, the vlookup does what I want, however, when it does not find a match, the resulting formulae read "#NAME?". What can I do to fix this? I have a feeling there is some syntactical issue with how my formula references the Month variable (the value of Cell(2,2)), but I cannot figure out what the error is.
After running the macro and clicking on one of the cells with the #NAME? result, the formula in the formula bar reads:
=IFERROR(VLOOKUP($B5,'All Account Log'!$B$7:$E$1048576,4,0),Jan)
Here I have the input "Jan" as the Month name, so I expect the result to be "Jan" rather than "#NAME?" Your thoughts are appreciated!
You really want:
=IFERROR(VLOOKUP($B5,'All Account Log'!$B$7:$E$1048576,4,0),"Jan")
instead of:
=IFERROR(VLOOKUP($B5,'All Account Log'!$B$7:$E$1048576,4,0),Jan)
so use:
Month = """" & Cells(2, 2).Value & """"
in the VBA code.
I have created a code that adds a new line to my spreadsheet with the information that users enter into a user-form. The problem I'm having is that I have formulas in two of the columns that I need to be carried into the new rows. I have tried several different ways to do this but keep receiving an error message. The formula I need carries is
=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B5&"-"&$A5,INDEX(Equipment!$B$3:$B$1000&"-"&Equipment!$A$3:$A$1000,0),0)),"")
The values that change line to line are the B and A cells after the MATCH command (in this case $B5 and $A5). Can anyone help me figure out how to convert this to VBA syntax so that I can add it to my code? Thanks in advance!
I Converted this formula, and tested it in a loop.
The formula is copied to Column D from rows 11 to 20, where $B5 and $A5 change according to whatever row they are (I hope I understand what you meant).
Sub ConvertFormula()
' modify to where (worksheet) you want to copy this formula
Set eqp_sht = ActiveWorkbook.Worksheets("Equipment")
' modify this according to whatever rows and column you want this formula copied
For i = 11 To 20
eqp_sht.Cells(i, 4).Formula = "=IFERROR(INDEX(Equipment!$E$3:$E$1000,MATCH($B" & i & "&-$A" & i & ",INDEX(Equipment!$B$3:$B$1000&-Equipment!$A$3:$A$1000,0),0))," & Chr(34) & Chr(34) & ")"
Next
End Sub