I am trying to you vlookup on the specials where we have N/A in prior column but not able to succeed as cell reference for vlookup is creating or problem and i tried to research it and fix it but couldn't get the right one. Would be helpful if any of you can help me to correct it
N/A can be in any of the cells in column K and once i filter column k with N/A's here and need to vlookup in column L using below formula on the special cells which are filtered but i am facing challenge with giving vlookup reference cell for which i need to your assistance as N/A can be in K2/K16/K20/K50/K80
=IFERROR(VLOOKUP($D16,'BP Scoping'!A:B,2,0),D16)
'Second vlookup not working
With Sheets("Sheet4")
Dim LRW As Long
LRW = .Range("A" & Rows.Count).End(xlUp).Row
.Range("A1").AutoFilter Field:=11, Criteria1:="#N/A"
'need to check for right cells which is missing
'One way tried to use this
.Range(.Range("L2"), Cells(LRW, "L")).SpecialCells(xlCellTypeVisible).Formula = "=iferror(VLOOKUP($D2,'BP Scoping'!A:B,2,0),D2)"
Another way I tried to use this:
.Range("L2:L" & LRW).SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=iferror(VLOOKUP($D2,'BP Scoping'!A:B,2,0),D2)"
Your last effort was getting close but you need to use xlR1C1 referencing, not xlA1 referencing in the formula.
.Range("L2:L" & LRW).SpecialCells(xlCellTypeVisible).FormulaR1C1 = _
"=IFERROR(VLOOKUP(RC4, 'BP Scoping'!C1:C2, 2, FALSE),RC4)"
That should provide the correct references to the values in column D relative to where the formula is placed in column L.
fwiw, you can quickly switch back and forth from xlA1 to xlR1C1 referencing with alt+F,T,F then alt+R.
Related
I am trying to achieve the following in VBA:
Fill a column from 2nd row to LastRow with a vlookup with the mention that the range of the vlookup is dynamic.
I used Match (see code snippet below) to identify the position of that column and also the last row.
I thought about referencing it in FormulaR1C1 but I have no idea how to reference the range with these variables (esentially the last line of code in my snippet) in either .Formula or .FormulaR1C1
Snippet
LastRowBin = wsbin.Cells(Rows.Count, "B").End(xlUp).Row
Set wsbin = Inputwb.Worksheets("Raport_POS_BIN")
colcopydoi = Application.Match(Search_doi, wsbin.Range("4:4"), 0)
newtgtlastrow = shttgt.Cells(Rows.Count, "A").End(xlUp).Row
/ This would be a formula that works but that doesn't have a dynamic reference for the range.
With shttgt.Range("D2")
.Formula = "=IFNA(VLOOKUP(A2,Input!Z2:AA20500, 2, FALSE),0)"
.AutoFill Destination:=Range("D2:D" & newtgtlastrow)
End With
/Range to use
wsbin.Range(wsbin.Cells(5, colcopydoi), wsbin.Cells(LastRowBin, colcopydoi))
Thank you very much for the help.
I am adding a formula into a spreadsheet via VBA that uses iferror and vlookup. Below is the code from the VBA
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("O2:o" & LastRow).FormulaR1C1 = "=IFERROR(VLOOKUP(RC1,'Controls'!a:b,2,FALSE),""Missing"")"
But when I look back into the spreadsheet, I find the following formula in the cells
=IFERROR(VLOOKUP($A2,Controls!A:(B),2,FALSE),"Missing")
which always results in a value of "Missing". When I edited the formula in the sheet and removed the parens from the B in the lookup range, the formula resolved correctly by finding the value in the Controls sheet.
I've tried changing the lookup range from A:B to A1:B500, $A$1:$B$500, and $A:$B. I also tried breaking it up and using various concatenations.
My question is ... How can I get VBA to NOT add the parens around the B in my lookup range.
Your problem comes from the fact that you put a regular address (A:B) into a R1C1-Formula. Even if your lookup-range is in another sheet, Excel expect the address in R1C1-notation. Your formula would look like
dim formula as string
formula = "=IFERROR(VLOOKUP(RC1,Controls!C[-14]:C[-13],2,FALSE),""Missing"")"
Range("O2:o" & LastRow).FormulaR1C1 = formula
as this is super ugly, I would suggest you define a name for the range Controls!A:B. Then you can change the formula simply to
formula = "=IFERROR(VLOOKUP(RC1,MyControls,FALSE),""Missing"")"
I think you would need to use .Formula(...) instead of .FormulaR1C1(...) and use uppercase on your string like ...A:B...
Else, you could do the following:
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("O2:o" & LastRow).FormulaR1C1 = "=IFERROR(VLOOKUP(RC1,'Controls'!a:b,2,FALSE),""Missing"")"
Dim cell As Range
For Each cell in Range("O2:O" & LastRow)
cell.Formula = Replace(cell.Formula, "(B)","B")
Next cell
I'm trying to find a way to copy all rows up to the first empty cell in a formula column, and also do it faster than using a .Select loop. Right now I'm trying this:
Sub CopyValues2()
FinalRow = Columns(9).Find("0").Row
Worksheets("Worksheet").Range("I3:K" & FinalRow).Copy
End Sub
I have column I = to column A. Right now it copies to the first 0 even if it's in a legitimate value so I tried lookAt:=xlWhole but that gave me an error. I also tried putting if statements in column I: =IF(A3<>"",A3,"null") and searching for "null" but then it only selects the first row. I assume that's because it is seeing the "null" in the if statement.
I also tried using Rows.count but that did not work due to the formulas in "empty" cells.
My ideal would be to have an IF(A3<>"",A3,"") and then find the first "" cell but I haven't gotten that to work either. Recommendations would be appreciated!
Try finding the last non-blank value, perhaps using the following command:
FinalRow = Columns(9).Find(What:="*", SearchDirection:=xlPrevious, LookIn:=xlValues).Row
I have a list of data with various different columns.
What I am trying to achieve is for excel to look through one of the columns and find the first value that occurs, copy that cell and then paste it in another cell. The data starts in row three (Rows 1 and 2 are headers). Let's say I want to paste the first value in G3.
Then, I need excel to find the next value that is different from the first in the same column and perform the same action as before: copy the value, and paste it in the next row in column G.
I have tried coding this but I'm not getting anywhere with it, and I haven't found a way to find a cell and then the next cell if the value within the cell is not defined (as the .Find function requires a value to search for). I know how to code the copy/paste functions but I cannot figure out how to get it to find the cell in the first place.
Any help would be much appreciated. Many thanks in advance.
In this sample, we are looking for values in column A:
Sub FillG()
Dim i As Long
i = Rows.Count
Range("A3:A" & i).Copy Range("G3:G" & i)
ActiveSheet.Range("G3:G" & i).RemoveDuplicates Columns:=1, Header:=xlNo
If Range("G3").Value = "" Then Range("G3").Delete Shift:=xlUp
End Sub
The method is to copy all of column A to G, then remove duplicates, then remove an empty in G3 if it exists.:
Hello from an unexperienced vba user.. I'm having trouble with the code for autofill to the last row when trying to use named ranges for the column. Everything seems to work fine when I use a hard coding for the column, in this case Column CW, and what I need is to replace this column CW with a named range so that the macro will still work when adding or deleting columns in the worksheet.
I used the following named ranges:
First_Date: This is the header cell of one of the columns (in this case AP5)
Second_Row: This is the range of columns I want to copy the formulas from (AP7:CW7)
Second_Cell: The cell where I want to start to autofill (AP7)
Last_Column: This is column CW that I want to use in the code. Autofill would be up to this column and down to the last row.
After searching in different threads, I came up with the following code that seems to work fine. How can I change column CW to a named range? Or do I need to change the code?
Dim Lr As Integer
Lr = Range("First_Date").End(xlDown).Row 'Searching last row
Rows(Lr).Insert Shift:=xlDown 'Inserting new row
Range("Second_Row").AutoFill Destination:=Range(Range("Second_Cell"), Range("CW" & Lr))
Can anyone assist me here please?
This will get the job done :-)
Sub RangerFiller()
'The Cell that holds the formula B1
OriginalFormula = Cells(1, 2).Formula
'copies formula down to the last column next to B but use can use another column as
'a counting column....the column that hold the last value
Range("B2:B" & Cells(Rows.Count, "A").End(xlUp).Row).Formula = OriginalFormula
End Sub
Someone gave me the solution:
Change
Range("CW" & Lr)
To
Cells(Lr, Range("Last_Column").Column)
I faced a similar problem because I don't want to hard code the cell reference. I found this solution below to be useful, by using "& ______ &" to replace the cell number that can be calculated using input box or formula.
Eg.
cell1 = last row of column A
Range("CW " & cell1 &" :CW & Lr),
where cell1 = any number that can be added via input box/formula.
Hope this helps!