【问题标题】:Macro will only create hyperlink in debug mode宏只会在调试模式下创建超链接
【发布时间】:2015-04-30 05:28:37
【问题描述】:

这是一个宏,它将搜索单个目录中包含的所有工作簿的所有工作表中的所有单元格。除了添加超链接方法外,一切都像宣传的那样工作,如果我反复混合 F8,它确实有效。

如何编辑宏以使超链接部分正常工作?

'Search all workbooks in a folder for string
Sub SearchWorkbooks()
Dim fso As Object
Dim fld As Object
Dim strSearch As String
Dim strPath As String
Dim strFile As String
Dim wOut As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim Lrow As Long
Dim rFound As Range
Dim strFirstAddress As String

On Error GoTo ErrHandler
Application.ScreenUpdating = False

strSearch = "Capacitor"
strPath = "C:\!Source"

Set wOut = Worksheets.Add
Lrow = 1
With wOut
    .Name = "Results"
    .Cells(Lrow, 1) = "Workbook"
    .Cells(Lrow, 2) = "Worksheet"
    .Cells(Lrow, 3) = "Cell"
    .Cells(Lrow, 4) = "Text in Cell"
    .Cells(Lrow, 5) = "Link"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(strPath)

    strFile = Dir(strPath & "\*.xls*")
    Do While strFile <> ""
        Set wbk = Workbooks.Open _
          (Filename:=strPath & "\" & strFile, _
          UpdateLinks:=0, _
          ReadOnly:=True, _
          AddToMRU:=False)

        For Each wks In wbk.Worksheets
            Set rFound = wks.UsedRange.Find(strSearch)
            If Not rFound Is Nothing Then
                strFirstAddress = rFound.Address
            End If
            Do
                If rFound Is Nothing Then
                    Exit Do
                Else
                    Lrow = Lrow + 1
                    .Cells(Lrow, 1) = wbk.Name
                    .Cells(Lrow, 2) = wks.Name
                    .Cells(Lrow, 3) = rFound.Address
                    .Cells(Lrow, 4) = rFound.Value

                    'This is the line that does not work
                    'well it actually works in debug mode but not in real time
                    wks.Hyperlinks.Add Anchor:=Cells(Lrow, 5), Address:=wbk.FullName, SubAddress:= _
                                    wks.Name & "!" & rFound.Address, TextToDisplay:="Link"

                End If
                Set rFound = wks.Cells.FindNext(After:=rFound)
            Loop While strFirstAddress <> rFound.Address
        Next

        wbk.Close (False)
        strFile = Dir
    Loop
    .Columns("A:D").EntireColumn.AutoFit
End With
'MsgBox "Done"    

ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set fld = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub

ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub

【问题讨论】:

  • 这听起来像是与时间相关的问题(这没有意义),请尝试注释掉 Application.ScreenUpdating = False 行并运行它。这可能会减慢它的运行速度。
  • 谢谢,但不幸的是它没有用。这绝对感觉像是一个时间问题,因为当驾驶缓慢时,它工作得很好。
  • 当您使用多个工作表时,不应使用对工作表的隐式引用。尝试将工作表引用添加到您的 Cells() 调用中,例如 wks.Cells(......)

标签: vba excel


【解决方案1】:

尝试将工作表引用添加到您的 Cells() 调用中,例如 wks.Cells(......)

【讨论】:

  • 就是这样!不仅如此,它还解决了另一个烦恼,有时它会写入错误的工作簿。非常感谢!
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 2019-06-12
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多