Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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 VBA:运行时错误1004尝试添加超链接时出现应用程序定义或对象定义错误_Excel_Vba_Hyperlink - Fatal编程技术网

Excel VBA:运行时错误1004尝试添加超链接时出现应用程序定义或对象定义错误

Excel VBA:运行时错误1004尝试添加超链接时出现应用程序定义或对象定义错误,excel,vba,hyperlink,Excel,Vba,Hyperlink,我是新使用excel VBA的,我正在尝试添加超链接。我不确定这个错误,因为我发现很多场景都有相同的错误代码,但与我自己的场景无关。我正在尝试使用2个命令bottoms从路径文件夹加载pdf文件,一个用于加载文件,另一个用于清除搜索。当我调试错误时,它指向以下特定行: ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=objFile.Path, TextToDisplay:=objFile.Name 下面是完整的代码,任何帮助都将不胜

我是新使用excel VBA的,我正在尝试添加超链接。我不确定这个错误,因为我发现很多场景都有相同的错误代码,但与我自己的场景无关。我正在尝试使用2个命令bottoms从路径文件夹加载pdf文件,一个用于加载文件,另一个用于清除搜索。当我调试错误时,它指向以下特定行:

ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=objFile.Path, TextToDisplay:=objFile.Name
下面是完整的代码,任何帮助都将不胜感激

子加载文件()
作为对象的Dim objFSO
将文件夹变暗为对象
Dim objFile作为对象
作为整数的Dim i
将路径设置为字符串
Application.ScreenUpdating=False
Application.EnableEvents=False
路径=范围(“A2”)
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objFolder=objFSO.GetFolder(路径)
i=范围(“B1”)
对于objFolder.Files中的每个objFile
如果不是数字(Application.Match(objFile.Name,Range(“A4:A”)和Range(“B1”)),0),则
范围(单元格(i+1,1)、单元格(i+1,1))。选择
ActiveSheet.Hyperlinks.Add锚定:=选择,地址:=objFile.Path_
TextToDisplay:=objFile.Name
i=i+1
如果结束
下一个objFile
端接头
次清洁()
ActiveSheet.Unprotect(“密码”)
范围(“A5”)。选择
范围(选择,选择。结束(xlDown))。选择
Selection.EntireRow.Delete
范围(“A2”)。选择
ActiveSheet.Protect(“密码”),DrawingObject:=True,Content:=True,Scenarios:=True
端接头
创建超链接列表
  • 如果计算最后一行,则不需要
    B1
    中的值
代码

Option Explicit

Sub LoadFiles()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim FolderPath As String
    Dim LastRow As Long
    Dim i As Long
        
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    FolderPath = Range("A2")
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    i = LastRow
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set objFolder = objFSO.GetFolder(FolderPath)
    On Error GoTo 0
    
    If Not objFolder Is Nothing Then
        ActiveSheet.Unprotect ("password")
        For Each objFile In objFolder.Files
            If Not IsNumeric(Application.Match(objFile.Name, _
                    Range("A4:A" & LastRow), 0)) Then
                ActiveSheet.Hyperlinks.Add _
                    Anchor:=Cells(i + 1, 1), _
                    Address:=objFile.Path, _
                    TextToDisplay:=objFile.Name
                i = i + 1
            End If
        Next objFile
        ActiveSheet.Protect ("password"), DrawingObjects:=True, Contents:=True, Scenarios:=True
    Else
        MsgBox "Folder '" & FolderPath & "' not found."
    End If
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

Sub Clean()
    
    Dim LastRow As Long
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    If LastRow > 4 Then
        ActiveSheet.Unprotect ("password")
        Range("A5", "A" & LastRow).EntireRow.Delete
        Range("A2").Select
        ActiveSheet.Protect ("password"), DrawingObjects:=True, Contents:=True, Scenarios:=True
    End If
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub
创建超链接列表
  • 如果计算最后一行,则不需要
    B1
    中的值
代码

Option Explicit

Sub LoadFiles()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim FolderPath As String
    Dim LastRow As Long
    Dim i As Long
        
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    FolderPath = Range("A2")
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    i = LastRow
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set objFolder = objFSO.GetFolder(FolderPath)
    On Error GoTo 0
    
    If Not objFolder Is Nothing Then
        ActiveSheet.Unprotect ("password")
        For Each objFile In objFolder.Files
            If Not IsNumeric(Application.Match(objFile.Name, _
                    Range("A4:A" & LastRow), 0)) Then
                ActiveSheet.Hyperlinks.Add _
                    Anchor:=Cells(i + 1, 1), _
                    Address:=objFile.Path, _
                    TextToDisplay:=objFile.Name
                i = i + 1
            End If
        Next objFile
        ActiveSheet.Protect ("password"), DrawingObjects:=True, Contents:=True, Scenarios:=True
    Else
        MsgBox "Folder '" & FolderPath & "' not found."
    End If
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

Sub Clean()
    
    Dim LastRow As Long
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    If LastRow > 4 Then
        ActiveSheet.Unprotect ("password")
        Range("A5", "A" & LastRow).EntireRow.Delete
        Range("A2").Select
        ActiveSheet.Protect ("password"), DrawingObjects:=True, Contents:=True, Scenarios:=True
    End If
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True

End Sub

如果工作表受保护,则无法添加超链接。您好,谢谢您的回复。当我删除以下行时:ActiveSheet.Unprotect(“密码”)ActiveSheet.Protect(“密码”),DrawingObject:=True,Contents:=True,Scenarios:=True我得到了错误1004:range类的Delete方法失败在进行更改之后,现在我得到了与行ActiveSheet.Hyperlinks.Add Anchor:=Selection,Address:=objFile.Path相同的错误,TextToDisplay:=objFile.name问题可能与单元格
B1
中的值有关:它必须是
=4
。其中的公式是什么?您是指行:i=Range(“B1”),如果不是数字(Application.Match(objFile.Name,Range(“A4:A”&Range(“B1”)),0)),那么我应该将B1更改为>=4吗?如果工作表受到保护,您不能添加超链接。您好,谢谢您的回复。当我删除以下行时:ActiveSheet.Unprotect(“密码”)ActiveSheet.Protect(“密码”),DrawingObject:=True,Contents:=True,Scenarios:=True我得到了错误1004:range类的Delete方法失败在进行更改之后,现在我得到了与行ActiveSheet.Hyperlinks.Add Anchor:=Selection,Address:=objFile.Path相同的错误,TextToDisplay:=objFile.name问题可能与单元格
B1
中的值有关:它必须是
=4
。其中的公式是什么?您是指行:i=Range(“B1”),如果不是数字(Application.Match(objFile.Name,Range(“A4:A”&Range(“B1”)),0)),那么我应该将B1更改为>=4吗?谢谢您,代码工作得非常好。非常感谢您的帮助。谢谢您,代码工作得非常好。非常感谢你的帮助