'如果你是在VB下,那就增加一个FileListBox控件Private Sub Command1_Click() File1.Path = "c:\" '改为你自己的目录 File1.Pattern = "*.Xls" Dim i As Long For i = 0 To File1.ListCount - 1 MsgBox File1.List(0) Next End Sub '如果你是在别的系统下. '那就用这个 FileName= Dir("C:\" & "*.Xls") ' "C:\"改为自己的目录. while FileName<>"" msgbox FileName FileName=Dir wend
谢谢fvflove 我知道错在哪了,马上给分
VBA代码: Function fEarch(ByVal mPath As String) As String Dim w1 As String, w2 With Application.FileSearch .FileType = msoFileTypeExcelWorkbooks .LookIn = mPath .Execute If .FoundFiles.Count > 0 Then For Each w2 In .FoundFiles w1 = w1 & IIf(w1 <> "", vbCrLf, "") & w2 Next End If End With fEarch = Replace(w1, mPath, "", , , vbTextCompare) End Function '测试代码: Sub Test2() Debug.Print fEarch("e:\work\") End Sub
'如果你是在VB下,那就增加一个FileListBox控件Private Sub Command1_Click()
File1.Path = "c:\" '改为你自己的目录
File1.Pattern = "*.Xls"
Dim i As Long
For i = 0 To File1.ListCount - 1
MsgBox File1.List(0)
Next
End Sub
'如果你是在别的系统下.
'那就用这个 FileName= Dir("C:\" & "*.Xls") ' "C:\"改为自己的目录. while FileName<>""
msgbox FileName
FileName=Dir
wend
Function fEarch(ByVal mPath As String) As String
Dim w1 As String, w2
With Application.FileSearch
.FileType = msoFileTypeExcelWorkbooks
.LookIn = mPath
.Execute
If .FoundFiles.Count > 0 Then
For Each w2 In .FoundFiles
w1 = w1 & IIf(w1 <> "", vbCrLf, "") & w2
Next
End If
End With
fEarch = Replace(w1, mPath, "", , , vbTextCompare)
End Function
'测试代码:
Sub Test2()
Debug.Print fEarch("e:\work\")
End Sub