分享

excle vba双击从路径获取文件名

 hdzgx 2019-11-05
  1. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

  2.     Application.DisplayAlerts = False

  3.     If Not Application.Intersect(Target, Range("C3")) Is Nothing Then Call 制定项目章程

  4.     If Not Application.Intersect(Target, Range("D3")) Is Nothing Then Call 制定项目管理计划

  5.     If Not Application.Intersect(Target, Range("E3")) Is Nothing Then Call 指导与管理项目工作

  6.     If Not Application.Intersect(Target, Range("E4")) Is Nothing Then Call 管理项目知识

  7.     If Target.Column = 11 Then

  8.         If Target.Row > 2 And Target.Row < 13 Then GetFileList Target

  9.     End If

  10.     Application.DisplayAlerts = True

  11.     Cancel = True

  12. End Sub

  13. Function GetFileList(ByVal Target As Range)

  14.     Dim strBasicPath As String, strSelFolder As String, strFileType As String

  15.     Dim strFileName As String, lngID As Long

  16.     Dim strFullName As String, rgResult As Range

  17.     If Trim(Target.Value) = "" Then Exit Function

  18.     'C:\Users\DCT\Desktop\PMP项目管理\示例项目\指定项目章程\组织过程资产

  19.     strBasicPath = ThisWorkbook.Path & "\指定项目章程"

  20.     strSelFolder = Trim(Target.Value) & ""

  21.     strFileType = "*.xlsx"

  22.     Set rgResult = Range("I15")

  23.     rgResult.Resize(100, 4).Clear

  24.     strFullName = strBasicPath & strSelFolder & strFileType

  25.     lngID = 0

  26.     strFileName = Dir(strFullName)

  27.     Do Until strFileName = ""

  28.         rgResult.Offset(lngID, 0).Value = lngID + 1

  29.         rgResult.Offset(lngID, 1).Value = strFileName

  30.         lngID = lngID + 1

  31.         strFileName = Dir()

  32.     Loop

  33. End Function

  34. ***************************

  35. Sub test()

  36. mypath = "C:\Users\DCT\Desktop\PMP项目管理\示例项目\指定项目章程\组织过程资产"   '路径最后务必以\结尾

  37.     Dim StrFile As String

  38.     StrFile = Dir(mypath)

  39.     Do While Len(StrFile) > 0

  40.     MsgBox StrFile

  41.         StrFile = Dir

  42.     Loop

  43. End Sub



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多