Excel Vlookup VBA - excel

I need to add Invoice as a Test and then V lookup formula should run
For example :="invoice"&" "&VLOOKUP(H3,FBL5N!D:G,4,0)
this formula i need to covert to Macro's
Let me know how to fix i tried my VBA formula as:
On Error Resume Next
For i = 3 To lastrow_d
ws_zfi.Range("AY3:AY" & lastrow_d) = "invoice" & Application.WorksheetFunction.VLookup(ws_zfi.Range("H3:H" & lastrow_d), ws_fbl5n.Range("D1:G" & lastrow_d), 4, 0)
Next i

It's difficult to answer without seeing the source tables but I think you want the following:
For i = 3 To lastrow_d
ws_zfi.Range("AY" & i) = "invoice" & Application.WorksheetFunction.VLookup(ws_zfi.Range("H" & i), ws_fbl5n.Range("D1:G" & lastrow_d), 4, 0)
Next i

Related

Compute countif in column Range & combine if with count if

I have two formulas that I need to transfer to VBA.
On Excel, my formula would be =countif(A$2:A2,A2) so I transferred that using this formula but everything is returning to 1. The rows didn't become dynamic and I want only the values to be displayed.
For a = 2 To lrow
ws.Range("T" & a).Formula = "=CountIf(A$2&"":""&A2)"",""&A2)"
Next a
Next formula that I use in Excel is
=IF(COUNTIF(A:A,A2)>Q2,"Check","Ok")
I tried this formula in VBA:
For i = 2 to lrow
If Countif(ws.Range("A2:A" & lrow), "A2") > ws.Range("Q2:Q", & lrow) Then
ws.Range("T" & i).Value = "Check"
Else
ws.Range("T" & i).Value = "Ok"
End If
Next i
You could populate column T with your first formula with this line of code:
ws.Range("T2:T" & lrow).FormulaR1C1 = "=COUNTIF(R2C[-19]:RC[-19],RC[-19])"
I can't advise on your second formula unless you clarify where you want to write it...

application define or object defined error in vba at vlookup formula

Need some help in resolving the below code. I am not able to get the vlookup value from another sheet, its giving error always "application define or object defined error"
For i = 1 To lrr
If Range("D" & i).Value = "" Then 'if cell in A is empty
On Error GoTo 0
Range("D" & i).FormulaR1C1 = "=VLookup(RC[-1], ('C:\Macros\[Consolidated BTG with Search Terms.xlsx]AU'!B:D),3,0)"
OR (with Sheet name by user input)
Range("D" & i).Formula = "=VLookup(RC[-1], Workbooks(nw).Sheets(MP).Range('B:D'), 3, 0)"
End If
i am trying to get values for a specific value if its empty and then vlookup from another sheet and past as special (values). I am stuck here. Please help
you are mixing A1 and R1C1 references. Choose one and make sure all the references use it.
So, Either:
Range("D" & i).FormulaR1C1 = "=VLookup(RC[-1], ('C:\Macros\[Consolidated BTG with Search Terms.xlsx]AU'!C2:C4),3,0)"
Or:
Range("D" & i).FormulaR1C1 = "=VLookup(RC[-1]," & Workbooks(nw).Sheets(MP).Range("B:D").Address(1,1,xlR1C1,1) & ", 3, 0)"

Incorporate Error-Checking Formula When Copying Excel Vba

I currently have the following code for copying cells:
Set Feeder = Sheets("Projects").Range("B" & Rows.Count).End(xlUp)
With Sheets("Database")
Set Storage = .Range("C" & .Rows.Count).End(xlUp).Offset(-Masterrow + 1)
Storage.Value2 = "=" & "Projects!" & Feeder.Address
End With
Is there a way to incorporate the formula =IFERROR(B2,0) so that my copy location contains =IFERROR(Projects!B2,0) as opposed to =Projects!B2?
I want erroneous cells to return a 0 as opposed to an error code so I can just run my delete rows code easily.
The fix was straightforward after realizing I had already constructed a formula before.
Code from before:
Storage.Value2 = "=" & "Projects!" & Feeder.Address
Code after:
Storage.Value2 = "=" & "IFERROR(" & "Projects!" & Feeder.Address & ",0)"
Sometimes it really is simple!

VBA - VLookup function not woking

Can anyone tell me why is this not working? This stop working my macro and an error message appears
Sh.Cells(2, 13).Formula = WorksheetFunction.VLookup(Sh.Cells(2, 6) & " - " & Sh.Cells(2, 8), Worksheets("Licenciaturas").Range("H2:K2928"), 4, False)
Thanks!
Are you trying to add it as a formula to the cell, or just return the result of the VLookup and place that in the cell?
Your code looks like it's trying to do a bit of both.
If you want a formula to appear in your sheet use:
Sh.Cells(2, 13).FormulaR1C1 = "=VLOOKUP(R2C6 & "" - "" & R2C8, 'Licenciaturas'!R2C8:R2928C8,4,FALSE)"
The formula will appear as: =VLOOKUP($F$2 & " - " & $H$2, Licenciaturas!$H$2:$H$2928,4,FALSE)

Concatenating SumIfs in VBA

I'm trying to get this segment of code to execute. This is a simplified version of the code. I've included the relevant code. I'm trying to concatenate strings and named ranges into a SumIfs formula, but I get error 1004 "Application-defined or Object-defined error." I have a working line of code above this problem section that is similar with the exception of doing a sum function, instead of sumif. Any idea how to get this code to execute? Thank you.
Dim wb As Workbook
Dim ara As Worksheet
Dim inv As Worksheet
Dim ARBlock As Range
Dim Invoices As Range
Dim AgedDays As Range
Set wb = ThisWorkbook
Set ara = wb.Sheets("AR Aging")
Set inv = wb.Sheets("Invoices")
Set ARBlock = ara.Range("a6")
Set Invoices = inv.Range("a6", inv.Range("a6").End(xlDown))
Set AgedDays = Invoices.Offset(0, 6)
'Populate A/R age buckets
For i = 6 To ARBlock.Rows.Count + 6
With ARBlock(i - 5, 1).Offset(0, 3)
.Value = "=SumIfs(" & Invoices.Offset(0, 4).Address & "," & _
Invoices.Address & "," & ARBlock(i - 5, 1).Value & "," & _
Invoices.Offset(0, 6).Address & ","" <= "" & &O30)"
End With
Next i
End Sub
The line beginning with ".value" is where I'm getting the error message. P.S.: I need the cell to contain the concatenated formula, as opposed to the output value.
UPDATE 1:
As some suggested I updated the .value line to:
.Offset(0, 3).Formula = "=SumIfs(Invoices.Offset(0, 4).Address,Invoices.Address,ARBlock.cells(i - 5, 1).Value)"
I'm still getting the same error. Some auditing I've done:
Removing the "=" before "Sumifs" allows the code to run fine; pasting in the formula into the target cell as text. In this form, my output for i=1 goes to ARBlock.cells(1,1), as it should.
I also used Debug.Print to view all of the components of the formula:
Debug.Print ARBlock.Cells(i - 5, 1).Address
'output $A$6
Debug.Print ARBlock.Cells(i - 5, 1).Value
' output International Business Machines
Debug.Print Invoices.Offset(0, 4).Address
'output $E$6:$E$255
Debug.Print Invoices.Address
'output $A$6:$A$255
I suspected the issue might be that the range dimensions might have been off, but this is not the case. My next suspicion is that the output International Business Machines needs to be in " " for the formula to read it correctly. I hardcoded in
""International Business Machines""
to see if this would fix the formula, but I keep getting the same error once I add the "=" back in. The formula syntax is correct, the dimensions are the same between the sum range and criteria range. Anyone else have any ideas?
.Offset(0, 3).Formula = "=SumIfs(Invoices.Offset(0, 4).Address,Invoices.Address,ARBlock.cells(i - 5, 1).Value)"
Change to:
.Offset(0, 3).Formula = "=SumIfs(" & Invoices.Offset(0, 4).Address & ", " & Invoices.Address & ", " & chr(34) & ARBlock.Cells(i - 5, 1).Value & chr(34) & ")"
EDIT: Added quotes chr(34) around your string!
Your ARBlock(i - 5, 1).Value most likely is an empty cell, which messes the SUMIFS formula as it builds it with to consecutive commas

Resources