超链接已关闭工作簿中新添加的单元格值

时间:2019-02-18 01:22:06

标签: excel vba

我似乎无法在这个论坛上讨论这个话题。
我这里有使用hyperlink函数但无法正常工作的代码。

我在运行时docname中打开了一个工作簿,该工作簿用作数据库。
wbSource是当前打开的工作簿。 wbSource会将一个单元格值发送到docname,并且必须将其转换为hyperlink
path是目录
Dest中的path目标文件夹
wb.Cells(BB + 2, 2)是接收者。超链接必须打开的文件几乎是在创建链接的同时创建的(使用saveas命令的行)。

Set wb = Workbooks(DocName).Sheets("Sheet1")
Set wbs = Workbooks(wbSource).Sheets("MACRO")

Dim BB As Integer
wb.Cells(1, 1) = "=counta(B:B)"
BB = wb.Cells(1, 1)

wbNew = InputBox("Enter the 'Mold Number' or 'Part Name' here", "FileName")
ActiveWorkbook.SaveAs FileName:=Path & "\" & DesT & "\" & "RFQ Details_" & wbNew 
Open wbNew For Output As #1
Close #1

wb.Cells(BB + 2, 2) = Hyperlink(Path & "\" & DesT & "\" & "RFQ Details_" & wbNew) 'THIS PART DOES NOT WORK!

除最后一行外,其他所有东西都工作正常。 我觉得最后一部分确实缺少某些东西。

2 个答案:

答案 0 :(得分:1)

提示:无论您有什么疑问,如果MS Excel允许,请记录宏并对其进行编辑。

我假设您具有BBPathDestDocName的有效值

这是您要尝试的吗?

Dim completePath As String

completePath = Path & "\" & DesT & "\" & "RFQ Details_" & wbNew

Set ws = Workbooks(DocName).Sheets("Sheet1")
Set rng = ws.Cells(BB + 2, 2)

rng.Hyperlinks.Add Anchor:=ws.Range(rng.Address), _
                   Address:=completePath, _
                   TextToDisplay:=completePath

使用以下示例值对其进行了测试,并且可以::

Sub Sample()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim BB As Long: BB = 1
    Dim rng As Range

    Dim Path As String, DesT As String, wbNew As String
    Dim completePath As String

    '~~> Sample Values
    Path = "C:\Users\routs\Desktop"
    DesT = "test"
    wbNew = "Sample.xlsx"
    DocName = ThisWorkbook.Name

    Set wb = Workbooks(DocName)
    Set ws = Workbooks(DocName).Sheets("Sheet1")
    Set rng = ws.Cells(BB + 2, 2)

    completePath = Path & "\" & DesT & "\" & "RFQ Details_" & wbNew

    rng.Hyperlinks.Add Anchor:=ws.Range(rng.Address), _
                       Address:=completePath, _
                       TextToDisplay:=completePath
End Sub

答案 1 :(得分:0)

我不知道为什么这行不通,但是我的行之有效,你能试试吗:

worksheets("Sheet1").hyperlinks.add _
    anchor:=range("A1"), _
    address:="https://9gag.com", _
    texttodisplay :="9Gag"

修改后的代码:

wb.Hyperlinks.Add _ 
    anchor:=Cells(BB + 2, 2), _  
    Address:=(Path & "\" & DesT & "\" & "RFQ Details_" & wbNew), _  
    TextToDisplay:=Cells(BB + 2, 2)