excel VBA打开URL

f8rj6qna  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(231)

不知道如何让VBA打开链接。
我有一个如果在CN2公式,将导致5个网址之一,该单元格是可点击的,并将引导我到这些网址之一。
我想也许VBA代码不喜欢基于公式的URL,并将结果转换为CO2中的超链接,但我仍然无法获得它。
我的错误与这下面的代码是“不能打开这指定的文件类型”
有人能帮忙吗?

Sub OpenURLs()

Dim i As Integer

For i = 1 To ActiveSheet.Range("Co2" & Rows.Count).End(xlUp).Row
    Dim url As String
    url = ActiveSheet.Range("Co2" & i).Value
    ActiveWorkbook.FollowHyperlink url ' error here

    ' Copy the adjacent value in column CM
    Dim valueToCopy As String
    valueToCopy = ActiveSheet.Range("CM" & i).Value
    ActiveSheet.Range("CM" & i).Copy

    MsgBox "Click OK to continue to the next URL", vbOKOnly
Next i

End Sub
p4tfgftt

p4tfgftt1#

下面的代码仅在单元格包含超链接时有效:

Sub OpenUrls()
    Dim i As Long
    Dim sh As Worksheet
    Set sh = ActiveSheet
    With sh
        For i = 1 To .Range("A" & Rows.Count).End(xlUp).Row
            Dim valueToCopy As String
            If .Range("A" & i).Hyperlinks.Count > 0 Then
                .Range("A" & i).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=False
                valueToCopy = .Range("B" & i).Value 'Not being used, decided to leave it
                .Range("B" & i).Copy
                MsgBox "Click OK to continue to the next URL", vbOKOnly
                Application.CutCopyMode = False
            End If
        Next i
    End With
End Sub

相关问题