ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1663|回复: 9

[分享] 返回到指定级数的目录

[复制链接]

TA的精华主题

TA的得分主题

发表于 2010-4-13 16:25 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub mmll()
    Dim theSh As Object
    Dim theFolder As Object
    Set theSh = CreateObject("shell.application")
        ';;;;;;;;;
        myPath = Range("g7").Value
        
           If InStr(myPath, "\") Then
                k = Len(myPath) - Len(Replace(myPath, "\", "")) '计算单元格g7中路径名中的"\"个数
               
                x = Cells(6, 6) '为要返回目录的级数:0表示本级目录及以下;1表示上一级;2表示上两级,以此类推
                If x > 0 Then
                   If k >= x Then
                      N = InStr(Replace(myPath, "\", " ", , k - x), "\")
           
                      temp = Left(myPath, N - 1) '取得上x级目录名称
                           
                      Range("g7").Value = temp
                          Else
                      Range("g7").Value = "&H11" '如果上x级目录是硬盘的根目录,则给单元格g7赋值=&H11
                   End If
                 Else
                                    
                End If
           End If
            
        
    ';;;;;;;;;;;;;;;;;;
    Set theFolder = theSh.BrowseForFolder(&O0, "请选择文件夹", &H1 + &H10, Range("g7").Value)
   
    If Not theFolder Is Nothing Then
        Range("g7").Value = theFolder.Items.Item.Path
    End If
   
    Set theFolder = Nothing
End Sub
源码来自论坛拼凑


[ 本帖最后由 zzf_ 于 2010-4-23 13:51 编辑 ]

返回指定级数的目录.rar

11.36 KB, 下载次数: 58

TA的精华主题

TA的得分主题

发表于 2010-4-13 16:51 | 显示全部楼层
谢谢分享,学习

TA的精华主题

TA的得分主题

发表于 2010-4-13 16:53 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
谢谢楼主分享,支持原创.

TA的精华主题

TA的得分主题

发表于 2010-4-13 22:53 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-14 12:04 | 显示全部楼层

单元格Cells(6, 6)的数据有效性如何做?

各位大师
Cells(6, 6)的数据有效性如何做?
想让其显示下拉框,数值从0到k(单元格g7中路径名中的"\"个数)

这样当前目录能返回的最多级数就一目了然了

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-14 19:11 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
';;;;;;;;;;;;;;;;;;
    Set theFolder = theSh.BrowseForFolder(&O0, "请选择文件夹", &H1 + &H10, Range("g7").Value)
   
    If Not theFolder Is Nothing Then
        Range("g7").Value = theFolder.Items.Item.Path
    End If
    '==================
    If Range("g7").Value <> "&H11" Then
    Set theFolder = Nothing
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(Range("g7").Value)
      fNumber = f.SubFolders.Count
          If fNumber = 0 Then
                 MsgBox ("该文件夹下无子目录了!")
                      If f.Size = 0 Then
                          MsgBox ("无有效文件")
                      End If
          End If
    End If
   End Sub

[ 本帖最后由 zzf_ 于 2010-4-23 15:17 编辑 ]

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-15 11:19 | 显示全部楼层

debug

'==================判断g7单元格中路径下是否有文件及有效文件(文件长度大于0的)
   With Application.FileSearch
  .LookIn = Range("g7").Value

除错

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-23 15:05 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

代码修改,补丁一下

Sub mmll()
    Dim theSh As Object
    Dim theFolder As Object
        Set theSh = CreateObject("shell.application")
        ';;;;;;;;;
        myPath = Range("g7").Value
                   If InStr(myPath, "\") Then
                   k = Len(myPath) - Len(Replace(myPath, "\", "")) '计算单元格g7中路径名中的"\"个数
           x = Cells(6, 6) '为要返回目录的级数:0表示本级目录及以下;1表示上一级;2表示上两级,以此类推
                If x > 0 Then
                               If k >= x And Len(myPath) > 3 Then '改一下,能返回到我的电脑

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-24 20:17 | 显示全部楼层
Sub mmll()
    Dim theSh As Object
    Dim theFolder As Object
   
    Set theSh = CreateObject("shell.application")
        ';;;;;;;;;
        myPath = Range("g7").Value
        
           If InStr(myPath, "\") Then
                k = Len(myPath) - Len(Replace(myPath, "\", "")) '计算单元格g7中路径名中的"\"个数
               
                x = Cells(6, 7) '为要返回目录的级数:0表示本级目录及以下;1表示上一级;2表示上两级,以此类推,此处添加了数据有效性
                If x > 0 Then
                   If k >= x And Len(myPath) > 3 Then
                      N = InStr(Replace(myPath, "\", " ", , k - x), "\")
           
                      temp = Left(myPath, N - 1) '取得上x级目录名称
                       Range("g7").ClearContents
                       Range("g7").Value = temp
                          Else
                      Range("g7").Value = "&H11" '如果上x级目录是硬盘的根目录,则给单元格g7赋值=&H11
                   End If
                 Else
                                    
                End If
           End If
           
      
    ';;;;;;;;;;;;;;;;;;
    Set theFolder = theSh.BrowseForFolder(&O0, "请选择文件夹", &H1 + &H10, Range("g7").Value)
   
    If Not theFolder Is Nothing Then
        Range("g7").Value = theFolder.Items.Item.Path
    End If
    '==================
    If Range("g7").Value <> "&H11" Then
    Set theFolder = Nothing
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(Range("g7").Value)
      fNumber = f.SubFolders.Count
          If fNumber = 0 Then
            MsgBox ("该文件夹下无子目录了!")
                      If f.Size = 0 Then
                          MsgBox ("无有效文件")
                      End If
          End If
    End If
   '==================Cells(6, 7)添加数据有效性
   
   Dim R As String
   Dim ss As Long
     Cells(6, 7).Validation.Delete
     kkk = Len(Range("g7").Value) - Len(Replace(Range("g7").Value, "\", ""))
        For ss = 0 To kkk
              R = R & "," & ss
        Next ss
    Range("g6").ClearContents
    Cells(6, 7).Validation.Add 3, 1, 1, R

   '==================
   
   
   End Sub

返回指定级数的目录-final.rar

13.44 KB, 下载次数: 13

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-26 10:30 | 显示全部楼层

Sheet1 再添点

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Target.Validation.Type = 4 Then
Exit Sub
Else
SendKeys "%{down}"
Target.Value = Split(Target.Validation.Formula1, ",")(0)
End If
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-1 18:01 , Processed in 0.047314 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表