Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 基于另一个单元格的值的超链接单元格_Excel_Vba - Fatal编程技术网

Excel 基于另一个单元格的值的超链接单元格

Excel 基于另一个单元格的值的超链接单元格,excel,vba,Excel,Vba,我在论坛上搜寻,寻找解决方案 我有一个基于B列单元格值创建超链接的代码。它可以工作,但只有当我在选择单元格时运行sub时,它才能工作 我需要的是,如果H列中的单元格值为“ok”,则会自动添加超链接 子超链接() 调光范围 将文件路径设置为字符串 如果Intersect(列(“B”),选择)为空,则退出Sub 对于相交中的每个r(选择、范围(“B2:B”和_ 单元格(Rows.Count,“B”).End(xlUp.Row)) 如果r为空字符串,则 FilePath=“T:\BLUEMAC\Sea

我在论坛上搜寻,寻找解决方案

我有一个基于B列单元格值创建超链接的代码。它可以工作,但只有当我在选择单元格时运行sub时,它才能工作

我需要的是,如果H列中的单元格值为“ok”,则会自动添加超链接

子超链接()
调光范围
将文件路径设置为字符串
如果Intersect(列(“B”),选择)为空,则退出Sub
对于相交中的每个r(选择、范围(“B2:B”和_
单元格(Rows.Count,“B”).End(xlUp.Row))
如果r为空字符串,则
FilePath=“T:\BLUEMAC\Search path\PDF主文件夹”
ActiveSheet.Hyperlinks.Add锚定:=r_
地址:=FilePath&r.Value&“.pdf”,TextToDisplay:=r.Value
如果结束
下一个r
端接头
任何帮助都将不胜感激。

像这样吗

Sub Hyperlinks()

Dim r As Range
Dim FilePath As String

If Intersect(Columns("B"), Selection) Is Nothing Then Exit Sub

For Each r In Intersect(Selection, Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row))
    If r <> vbNullString And LCase$(r.Offset(0, 6).value) = "ok" Then
        FilePath = "T:\BLUEMAC\Search Paths\PDF MASTER FOLDER\"
        ActiveSheet.Hyperlinks.Add Anchor:=r, _
             Address:=FilePath & r.Value & ".pdf", TextToDisplay:=r.Value
    End If
Next r

End Sub
子超链接()
调光范围
将文件路径设置为字符串
如果Intersect(列(“B”),选择)为空,则退出Sub
对于相交中的每个r(选择、范围(“B2:B”和单元格(Rows.Count,“B”)。结束(xlUp.Row))
如果r vbNullString和LCase$(r.Offset(0,6).value)=“ok”,则
FilePath=“T:\BLUEMAC\Search path\PDF主文件夹”
ActiveSheet.Hyperlinks.Add锚定:=r_
地址:=FilePath&r.Value&“.pdf”,TextToDisplay:=r.Value
如果结束
下一个r
端接头
更改

If Intersect(Columns("B"), Selection) Is Nothing Then Exit Sub

For Each r In Intersect(Selection, Range("B2:B" & _
Cells(Rows.Count, "B").End(xlUp).Row))


我认为James需要的代码不依赖于正在运行的
选择
,但只有当单元格被选中并且子运行时,它才仍然有效。我怎么才能让它自动通过B列中的每个单元格呢?是的,看起来像是问题,我需要用其他东西替换相交部分,但是什么?Jochen有答案出去吃午饭了对不起-是的,看起来Jochen有it@JamesHurst如果答案有帮助,不要忘了标记为答案(左边的勾号)Jochen你帮了我很大的忙,你能看看我的另一个问题吗?
If Intersect(Columns("B"), Selection) Is Nothing Then Exit Sub

For Each r In Intersect(Selection, Range("B2:B" & _
Cells(Rows.Count, "B").End(xlUp).Row))
For Each r In Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row)
ActiveSheet.Hyperlinks.Add Anchor:=r, _
Address:=FilePath & r.Value & ".pdf", TextToDisplay:=r.Value
If r.offset(0,6).value = "ok" then ActiveSheet.Hyperlinks.Add Anchor:=r, _
Address:=FilePath & r.Value & ".pdf", TextToDisplay:=r.Value