Excel 为followhyperlink构建正确的路径 全局 浏览列表并为列表中的每个项目创建一个选项卡(工作) 在列表中创建链接到关联工作表(工作)的超链接 在每个工作表上创建基本标题信息,并将其超链接回索引表(工作) 为索引表中相应单元格中列出的每个参考插入一个按钮(工作) 将超链接添加到打开pdf、doc或docx文件的按钮(未工作,正在工作) 当前问题

Excel 为followhyperlink构建正确的路径 全局 浏览列表并为列表中的每个项目创建一个选项卡(工作) 在列表中创建链接到关联工作表(工作)的超链接 在每个工作表上创建基本标题信息,并将其超链接回索引表(工作) 为索引表中相应单元格中列出的每个参考插入一个按钮(工作) 将超链接添加到打开pdf、doc或docx文件的按钮(未工作,正在工作) 当前问题,excel,vba,Excel,Vba,根据按钮名称,文件将存储在3个目录中的1个目录中。虽然按钮名是文件名的唯一部分,但文件名可能存在差异,扩展名在doc和docx之间可能有所不同 我有三种按钮名称格式 F-1010 F-0400-01 928 在第一种情况下,我可以生成准确的完整文件名,与文件的格式完全相同 在第二种情况下,文件名将以按钮名开头,后跟附加文本,然后是word文档扩展名的变体:F-0400-01 abc def.doc或F-0400-01 abc def.docx 在第三种情况下,文件名将以OPSS开头,后面有

根据按钮名称,文件将存储在3个目录中的1个目录中。虽然按钮名是文件名的唯一部分,但文件名可能存在差异,扩展名在doc和docx之间可能有所不同

我有三种按钮名称格式

F-1010
F-0400-01
928
在第一种情况下,我可以生成准确的完整文件名,与文件的格式完全相同

在第二种情况下,文件名将以按钮名开头,后跟附加文本,然后是word文档扩展名的变体:
F-0400-01 abc def.doc
F-0400-01 abc def.docx

在第三种情况下,文件名将以
OPSS
开头,后面有时是一些文本,后面是按钮名,后面是一堆文本,最后是.pdf:
OPSS 928 abc.pdf
OPSS.MUNI 928 abc.pdf

我尝试在字符串中使用通配符,但不起作用

Sub btnClick()

Dim btnName As String
Dim FPath As String
    
    'btnName = Application.Caller
    btnName = "F-0400-01" 'assigned name for testing purposes
        If Left(btnName, 1) = "F" Then
            If Num_Characters_In_String(btnName, "-") = 2 Then
                FPath = "P:\2019\1234 Folder\08. Working\Specifications\Section F" & btnName & "*.doc*"
            Else
                FPath = "P:\2019\1234 Folder\10. Construction\01. Tender\F\" & btnName & ".pdf"
            End If
        Else
            FPath = "P:\2019\1234 Folder\10. Construction\01. Tender\OPSS\OPSS*" & btnName & "*.pdf"
        End If
    
    ThisWorkbook.FollowHyperlink FPath
   
End Sub
第二和第三种情况的错误

我读书,然后找到我现在的位置

问题: 如何正确构建路径?如何打开各种文件类型?

我找到并使用了Rex的函数解决方案

我在函数的末尾遇到了一个问题,尽管转置部分给了我一个类型不匹配的错误。因此,我对代码进行了如下修改:

Sub btnClick()

Dim btnName As String
Dim FPath As String
Dim basePath As String
Dim sLink As String

    basePath = "P:\2019\1234 Folder"
    'btnName = Application.Caller
    btnName = "180"
        If Left(btnName, 1) = "F" Then
            If Num_Characters_In_String(btnName, "-") = 2 Then
                FPath = basePath & "\08. Working\Specifications\Section F\"
            Else
                FPath = basePath & "\10. Construction\01. Tender\F\"
            End If
        Else
            FPath = basePath & "\10. Construction\01. Tender\OPSS\"
        End If
    sLink = fileName(FPath, "*" & btnName & "*", ".*")
    ThisWorkbook.FollowHyperlink sLink
   
End Sub
Function fileName(path As String, sName As String, ext As String) As String

'path is Full path from root.  Can also use path = ActiveWorkbook.path & "\"
'sName is the string to search. ? and * are wildcards.  ? is for single char
'example sName = "book?" or sName ="March_*_2014*"
'ext is file extention ie .pdf .xlsm .xls? .j*

Dim file As Variant 'Store the next result of Dir
Dim fname() As String 'Dynamic Array for result set
ReDim fname(0 To 0)
Dim i As Integer ' Counter
i = 0

' Use dir to search and store first result
fname(i) = path & Dir(path & "\" & sName & ext)
i = i + 1

'Load next result
file = Dir

While file <> "" 'While a file is found store that file in the array
  ReDim Preserve fname(0 To i) As String
  fname(i) = path & file
  file = Dir
Wend

'fileName = Application.Transpose(fname) 'Print out array
fileName = fname(0)
End Function
和修改后的文件名子文件,如下所示:

Sub btnClick()

Dim btnName As String
Dim FPath As String
Dim basePath As String
Dim sLink As String

    basePath = "P:\2019\1234 Folder"
    'btnName = Application.Caller
    btnName = "180"
        If Left(btnName, 1) = "F" Then
            If Num_Characters_In_String(btnName, "-") = 2 Then
                FPath = basePath & "\08. Working\Specifications\Section F\"
            Else
                FPath = basePath & "\10. Construction\01. Tender\F\"
            End If
        Else
            FPath = basePath & "\10. Construction\01. Tender\OPSS\"
        End If
    sLink = fileName(FPath, "*" & btnName & "*", ".*")
    ThisWorkbook.FollowHyperlink sLink
   
End Sub
Function fileName(path As String, sName As String, ext As String) As String

'path is Full path from root.  Can also use path = ActiveWorkbook.path & "\"
'sName is the string to search. ? and * are wildcards.  ? is for single char
'example sName = "book?" or sName ="March_*_2014*"
'ext is file extention ie .pdf .xlsm .xls? .j*

Dim file As Variant 'Store the next result of Dir
Dim fname() As String 'Dynamic Array for result set
ReDim fname(0 To 0)
Dim i As Integer ' Counter
i = 0

' Use dir to search and store first result
fname(i) = path & Dir(path & "\" & sName & ext)
i = i + 1

'Load next result
file = Dir

While file <> "" 'While a file is found store that file in the array
  ReDim Preserve fname(0 To i) As String
  fname(i) = path & file
  file = Dir
Wend

'fileName = Application.Transpose(fname) 'Print out array
fileName = fname(0)
End Function
函数文件名(路径为字符串,sName为字符串,ext为字符串)为字符串
'路径是来自根的完整路径。也可以使用path=ActiveWorkbook.path&“\”
'sName是要搜索的字符串?和*是通配符?是单字符的
'示例sName=“book?”或sName=“2014年3月*
“ext是文件扩展名ie.pdf.xlsm.xls?”。j*
Dim file As Variant'存储Dir的下一个结果
Dim fname()作为结果集的字符串动态数组
重拨fname(0到0)
Dim i作为整数计数器
i=0
'使用dir搜索并存储第一个结果
fname(i)=路径和目录(路径和“\”&sName&ext)
i=i+1
'加载下一个结果
file=Dir
While file“”在找到文件时,将该文件存储在数组中
重拨将fname(0到i)保留为字符串
fname(i)=路径和文件
file=Dir
温德
“fileName=Application.Transpose(fname)”打印输出数组
fileName=fname(0)
端函数
我找到并使用了Rex的函数解决方案

我在函数的末尾遇到了一个问题,尽管转置部分给了我一个类型不匹配的错误。因此,我对代码进行了如下修改:

Sub btnClick()

Dim btnName As String
Dim FPath As String
Dim basePath As String
Dim sLink As String

    basePath = "P:\2019\1234 Folder"
    'btnName = Application.Caller
    btnName = "180"
        If Left(btnName, 1) = "F" Then
            If Num_Characters_In_String(btnName, "-") = 2 Then
                FPath = basePath & "\08. Working\Specifications\Section F\"
            Else
                FPath = basePath & "\10. Construction\01. Tender\F\"
            End If
        Else
            FPath = basePath & "\10. Construction\01. Tender\OPSS\"
        End If
    sLink = fileName(FPath, "*" & btnName & "*", ".*")
    ThisWorkbook.FollowHyperlink sLink
   
End Sub
Function fileName(path As String, sName As String, ext As String) As String

'path is Full path from root.  Can also use path = ActiveWorkbook.path & "\"
'sName is the string to search. ? and * are wildcards.  ? is for single char
'example sName = "book?" or sName ="March_*_2014*"
'ext is file extention ie .pdf .xlsm .xls? .j*

Dim file As Variant 'Store the next result of Dir
Dim fname() As String 'Dynamic Array for result set
ReDim fname(0 To 0)
Dim i As Integer ' Counter
i = 0

' Use dir to search and store first result
fname(i) = path & Dir(path & "\" & sName & ext)
i = i + 1

'Load next result
file = Dir

While file <> "" 'While a file is found store that file in the array
  ReDim Preserve fname(0 To i) As String
  fname(i) = path & file
  file = Dir
Wend

'fileName = Application.Transpose(fname) 'Print out array
fileName = fname(0)
End Function
和修改后的文件名子文件,如下所示:

Sub btnClick()

Dim btnName As String
Dim FPath As String
Dim basePath As String
Dim sLink As String

    basePath = "P:\2019\1234 Folder"
    'btnName = Application.Caller
    btnName = "180"
        If Left(btnName, 1) = "F" Then
            If Num_Characters_In_String(btnName, "-") = 2 Then
                FPath = basePath & "\08. Working\Specifications\Section F\"
            Else
                FPath = basePath & "\10. Construction\01. Tender\F\"
            End If
        Else
            FPath = basePath & "\10. Construction\01. Tender\OPSS\"
        End If
    sLink = fileName(FPath, "*" & btnName & "*", ".*")
    ThisWorkbook.FollowHyperlink sLink
   
End Sub
Function fileName(path As String, sName As String, ext As String) As String

'path is Full path from root.  Can also use path = ActiveWorkbook.path & "\"
'sName is the string to search. ? and * are wildcards.  ? is for single char
'example sName = "book?" or sName ="March_*_2014*"
'ext is file extention ie .pdf .xlsm .xls? .j*

Dim file As Variant 'Store the next result of Dir
Dim fname() As String 'Dynamic Array for result set
ReDim fname(0 To 0)
Dim i As Integer ' Counter
i = 0

' Use dir to search and store first result
fname(i) = path & Dir(path & "\" & sName & ext)
i = i + 1

'Load next result
file = Dir

While file <> "" 'While a file is found store that file in the array
  ReDim Preserve fname(0 To i) As String
  fname(i) = path & file
  file = Dir
Wend

'fileName = Application.Transpose(fname) 'Print out array
fileName = fname(0)
End Function
函数文件名(路径为字符串,sName为字符串,ext为字符串)为字符串
'路径是来自根的完整路径。也可以使用path=ActiveWorkbook.path&“\”
'sName是要搜索的字符串?和*是通配符?是单字符的
'示例sName=“book?”或sName=“2014年3月*
“ext是文件扩展名ie.pdf.xlsm.xls?”。j*
Dim file As Variant'存储Dir的下一个结果
Dim fname()作为结果集的字符串动态数组
重拨fname(0到0)
Dim i作为整数计数器
i=0
'使用dir搜索并存储第一个结果
fname(i)=路径和目录(路径和“\”&sName&ext)
i=i+1
'加载下一个结果
file=Dir
While file“”在找到文件时,将该文件存储在数组中
重拨将fname(0到i)保留为字符串
fname(i)=路径和文件
file=Dir
温德
“fileName=Application.Transpose(fname)”打印输出数组
fileName=fname(0)
端函数