|
楼主 |
发表于 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
|
|