VBA for V-lookup from another sheet - excel

Please help me make my following VBA lines error-free.
Here FilepathRM is a variable to define the path
FileNameRM is a variable to define another workbook name
The code runs well if I use sheet-name in another work book instead of workbook.sheets(i).name function but since sheet name changes every time a new workbook is worked upon, I wish sheet code name is used instead of the sheet name.
Selection.FormulaR1C1 = "= XLookup(RC[-2],'" & FilePathRM & "[" & FileNameRM & "]" & OpenBookRM.Sheets(1).Name & " '!R1C2:R10000C2,'" & FilePathRM & "[" & FileNameRM & "]" & OpenBookRM.Sheets(1).Name & "'!R1C8:R10000C8)"

Related

I receive application define object defined error while inserting bloomberg BDP formula to a cell

I get application defined error while inserting a formula that is run by bloomberg addin.
Worksheets("Portfoy").Cells(portfoyCounter, 10).Formula = "=BDP(" & Chr(34) & Worksheets("Portfoy").Cells(portfoyCounter , 3).Value & " ISIN" & Chr(34) & "," & Chr(34) & "YAS_RISK" & Chr(34) & ")*100*"
This code should insert =BDP("XS1634372954 ISIN";"YAS_RISK")*100 this to the cell (portfoyCounter, 10) and XS1634372954 is located in Cells(portfoyCounter , 3)
portfoyCounter variable holds the correct row count I have checked it. I have tried above code in the same manner in different code blocks without any issue but I do not know why I get that error this time.
Tnx vm for you Help in advance.
I checked the counter values but it seems correct.

Vlookup formula with variable workbook and sheet names

This is the part I currently have:
Worksheets("Data").Cells(3, CRup).FormulaR1C1 = "=VLOOKUP(RC[-7],'[" & TOD & "]" & flt & "'!R1C1:C18, " & CRupT & ",0)"
Try this vba function Application.WorksheetFunction.VLookup
Example :
Dim lk_Val = 'Lookup Value String'
Worksheet("DestinationSheet").Range("A1") = WorkApplication.WorksheetFunction.VLookup(lk_Val,Workbooks("YourSourceWorkbook").Sheets("YourSourceSheet").Range("A:B"),2,False)
As far as i know the workbook must be opened

Application-defined or object-defined error when using .formula

I've been struggling to get a formula typed into a cell on a sheet. it's my activesheet (definitely).
Source is a file with full path. the sheet name is ... and lta_col_letter is the column letter I'm typing the formula into.
ActiveSheet.Range(lta_col_letter & 2).Formula = "=IFERROR(INDEX('[" & Source & "]...'!$G:$G,MATCH(" & versionref_col_letter & "2,'[" & Source & "]...'!$B:$B,0))," & Chr(34) & Chr(34) & ")"
I'm not sure why I'm getting this error. Help please!
EDIT:
Works when I added the missing [ brackets, but when it goes into the cell it adds the file name again after the sheet name and before the cell reference.It looks fine in a msgbox.
Looks like you are missing some opening square brackets around source.
ActiveSheet.Range(lta_col_letter & 2).Formula = "=IFERROR(INDEX('[" & source & "]...'!$G:$G,MATCH(" & versionref_col_letter & "2,'[" & source & "]...'!$B:$B,0)), text(,))"
To examine these types of errors, put a ' before the opening = and run the procedure to put the formula into the cell as a string. Go to the worksheet and perform a visual examination and try to remove the '. Make corrections until you have a working formula then transfer the corrections to the VBA formula string.
Since it seems that you have used an open workbook to retrieve the workbook name into source, you might consider using the Range.Address Property with External:=True to retrieve a fully punctuated external address.
dim sourceG as string, sourceB as string
sourceG = workbooks(source).worksheets("...").range("G:G").address(external:=true)
sourceB = workbooks(source).worksheets("...").range("B:B").address(external:=true)
ActiveSheet.Range(lta_col_letter & 2).Formula = _
"=IFERROR(INDEX(" & sourceG & ", MATCH(" & versionref_col_letter & "2, " & sourceB
& ",0)), text(,))"

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!

adding a dynamic cell reference in vba

I am using the following code to insert a formula into a cell using vba.
The code inserts a hyperlink with some static text leading to a file path and then at the end of my file path I want to be able to add a dynamic cell reference, for instance A and then the number of the row.
In my cell in column A I have the names of folders. I am using DestRow to define the current row number. So my question is how can I correct my formula so that when the link is clicked it opens the link to get the correct folder name of the row clicked? Thanks
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\"" & K" & DestRow & ",""Attached"")"
Try,
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\" & ws2.Range("K" & DestRow).Value & """,""Attached"")"
FWIW, I hate working with quoted strings as well.
Addendum: This should do for adding a static filename after the dynamic folder:
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\" & ws2.Range("K" & DestRow).Value & "\audit.xls"",""Attached"")"
You could try including the INDIRECT() function:
ws2.Range("S" & DestRow).Formula = "=HYPERLINK(""\\UKSH000-FILE06\Purchasing\New_Supplier_Set_Ups_&_Audits\ATTACHMENTS\"" & INDIRECT(""K""&" & DestRow & ",""Attached"")"

Resources