adding a dynamic cell reference in vba

 ̄綄美尐妖づ 提交于 2019-12-11 22:21:07

问题


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"")"

回答1:


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"")"



回答2:


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"")"


来源:https://stackoverflow.com/questions/26231912/adding-a-dynamic-cell-reference-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!