【问题标题】:VBA to Open a Hyperlink based on Specific Text in another cellVBA根据另一个单元格中的特定文本打开超链接
【发布时间】:2021-07-10 18:20:54
【问题描述】:

我正在尝试创建一个 VBA 以在单元格 E 中查找星期一,然后在 D 中打开它旁边的单元格中的超链接。

我已经尝试了以下但它不起作用。当我运行它时什么都没有发生。这些文件在我的网络上,超链接附加到。

enter image description here

Sub OpenFile()
         Dim oH As Hyperlink
    For Each oH In Hyperlinks
          If oH.Range.Cells(1, 2).Text = "Monday" Then If Dir(oH.Address) > "" Then Workbooks.Open oH.Address
    Next
End Sub 

【问题讨论】:

    标签: vba text hyperlink match excel-365


    【解决方案1】:
    Sub OpenFile()
        Dim i As Long
        Dim rowLast As Long
        Dim filePath As String
        'Dim loopLink As Hyperlink 'Uncomment if there can be multiple links in column D cells
        
        With Sheet1 'Change the worksheet name to the correct one
            rowLast = .Range("E" & .Rows.Count).End(xlUp).Row
        
            For i = 2 To rowLast 'Loop from 2nd row to the last row
                If .Cells(i, 5).Value = "Monday" Then 'Validation if cell in Column E is "Monday"
                    With .Cells(i, 4)
                        'Single link version, change to below version for multiple links
                        If .Hyperlinks.Count = 1 Then
                            filePath = .Hyperlinks(1).Address
                            If Dir(filePath) <> vbNullString Then Application.Workbooks.Open filePath
                        End If
                        
                        'Multiple links version
                        'For Each loopLink In .Hyperlinks
                        '   filePath = loopLink.Address
                        '   If Dir(filePath) <> vbNullString Then Application.Workbooks.Open filePath
                        'Next loopLink
                    End With
                End If
            Next i
        End With
    End Sub
    

    【讨论】:

    • 获得 rowLast = .Range("E" & .Rows.Count).End(xlUp).Row 的运行时 424 错误
    • @mlott - 您所指的工作表对象是否正确?如果是这样,你能打印出什么是 .rows.count 吗?或者,如果最后一行是一个常数,那么您可以直接赋值。
    • 我仍然需要这方面的帮助。我无法完成这项工作。我想单击特定日期示例“星期一”的按钮控件,它会打开列 D 中列出的所有超链接,这些超链接在 E 列中列出了“星期一”。
    • @mlott 请更具体一点,什么不起作用?有错误吗?如果是这样,哪一行以及错误描述是什么。同时使用您的代码更新您的问题。
    • 它不会让我在这里添加我的宏,因为它说它太长了。发生的事情是我点击运行,没有任何反应。它工作得很好,然后就停止了。没有错误消息或什么都没有。我尝试了这一步,没有任何停止。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多