excel - 如果超链接断开,则将整行复制到目标工作表

标签 excel vba

如果出现以下任一情况,我想将整行复制到目标工作表:1) 行中没有超链接,或 2) 行中的超链接都是断开的链接(例如,它们在访问时返回错误)。

Sub Find_Value()

Dim sh1 As Worksheet, sh2 As Worksheet
Dim rFind As Range
Dim i As Long
Set sh1 = Sheets("data")
Set sh2 = Sheets("copy")

For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If Cells(i, 1).Hyperlinks.Count = 0 Then
            Cells(i, "A").EntireRow.Copy Destination:=sh2.Cells(i, "A").End(xlUp).Offset(1)
        Else
        End If
        Next i
End Sub
如果没有超链接(如我上面的代码所示),我可以这样做,但是如何检查链接是否损坏?

最佳答案

检查是否没有超链接或链接损坏

Option Explicit

Sub FindValue()
    
    Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("data")
    Dim srg As Range
    Set srg = sws.Range("A1", sws.Cells(sws.Rows.Count, "A").End(xlUp))
    
    Dim dws As Worksheet: Set dws = ThisWorkbook.Worksheets("copy")
    Dim dCell As Range: Set dCell = dws.Cells(dws.Rows.Count, "A").End(xlUp)
    
    Dim sCell As Range
    Dim rCount As Long
    Dim Invalid As Boolean
    
    For Each sCell In srg.Cells
        If sCell.Hyperlinks.Count = 0 Then
            Invalid = True
        Else
            If IsLinkBroken(sCell.Hyperlinks(1).Address) Then Invalid = True
        End If
        If Invalid Then
            Set dCell = dCell.Offset(1)
            sCell.EntireRow.Copy Destination:=dCell
            rCount = rCount + 1
            Invalid = False
        End If
    Next sCell

    MsgBox "Rows copied: " & rCount, vbInformation
    
End Sub

Function IsLinkBroken(ByVal url As String) As Boolean
    On Error GoTo ClearError
    With CreateObject("MSXML2.ServerXMLHTTP")
        .Open "HEAD", url, False
        .send
        If .Status = 200 Then Exit Function
    End With
ProcExit:
    IsLinkBroken = True
    Exit Function
ClearError:
    Resume ProcExit
End Function
紧凑型
  • 不太确定这是否会更快(甚至正确)。

  • Sub FindValueCompact()
        
        Dim sws As Worksheet: Set sws = ThisWorkbook.Worksheets("data")
        Dim srg As Range
        Set srg = sws.Range("A1", sws.Cells(sws.Rows.Count, "A").End(xlUp))
        
        Dim dws As Worksheet: Set dws = ThisWorkbook.Worksheets("copy")
        Dim dCell As Range: Set dCell = dws.Cells(dws.Rows.Count, "A").End(xlUp)
        
        Dim xhr As Object: Set xhr = CreateObject("MSXML2.ServerXMLHTTP")
        
        Dim sCell As Range
        Dim rCount As Long
        Dim ErrNum As Long
        Dim Invalid As Boolean
        
        For Each sCell In srg.Cells
            If sCell.Hyperlinks.Count = 0 Then
                Invalid = True
            Else
                xhr.Open "HEAD", sCell.Hyperlinks(1).Address, False
                On Error Resume Next
                    xhr.send
                    ErrNum = Err.Number
                On Error GoTo 0
                If ErrNum = 0 Then
                    If xhr.Status <> 200 Then Invalid = True
                Else
                    Invalid = True
                    ErrNum = 0
                End If
            End If
            If Invalid Then
                Set dCell = dCell.Offset(1)
                sCell.EntireRow.Copy Destination:=dCell
                rCount = rCount + 1
                Invalid = False
            End If
        Next sCell
    
        MsgBox "Rows copied: " & rCount, vbInformation
        
    End Sub
    

    关于excel - 如果超链接断开,则将整行复制到目标工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71794587/

    相关文章:

    linux - Vim:如何使用 .config 使未编译的行变暗

    vba - Excel VBA 工作簿.ChangeFileAccess

    excel - 条件格式 Excel 中的字母

    Excel 公式不起作用 - 无法识别数字

    excel - 从实例打印 Excel 工作表时字体错误?

    vba - XY 散点图和字典问题

    arrays - 为什么 Excel.Range.Group 的 Periods 参数需要一个数组?

    arrays - Excel数组查找公式

    vba - 集合对象 - ByRef - ByVal

    vba - 使用对象浏览器中未列出的对象属性