假設我有很多以.edf格式化的文件。我想搜索每個文件,如果它讓我們假設它'hihowareyou'。如果文件具有該文件,則該程序應將該文件的名稱保存在第二個電子表格的第1列中。然後進一步搜索文件讓我們假設經度,並應該在電子表格的第2列中保存屬性的值,例如570degrees(如經度:570degrees)。如何在excel中搜索文件並將屬性數據保存在電子表格中vba
在下面的代碼中,我找到了需要通過遞歸檢查的文件。我不知道如何搜索文件。
Function Recursive(FolderPath As String)
Dim fileName As String, textData As String, textRow As String, fileNo As Integer
Dim Value As String, Folders() As String
Dim Folder As Variant, a As Long
Dim Right_FolderPath As String
ReDim Folders(0)
If Right(FolderPath, 2) = "\\" Then Exit Function
Value = Dir(FolderPath, &H10)
Do Until Value = ""
If Value = "." Or Value = ".." Then
Else
If GetAttr(FolderPath & Value) = 16 Then
Folders(UBound(Folders)) = Value
ReDim Preserve Folders(UBound(Folders) + 1)
Else
If Right(Value, 4) = ".edf" Then
If Count = 4 Then
Right_FolderPath = Right(FolderPath, 7)
If Left(Right_FolderPath, 2) = "DR" Then
''''Here it goes all wrong
'myFile = FolderPath & Value
'myFile = Application.GetOpenFilename()
'fileNo = FreeFile 'Get first free file number
'Open fileName For Input As #fileNo
'Do While Not EOF(fileNo)
' Line Input #fileNo, textRow
' textData = textData & textRow
'Loop
'Close #fileNo
'posLat = InStr(text, "ff-ai")
'If Not posLat = vbNullString Then
' temp(0, UBound(temp, 2)) = Value
'End If
temp(0, UBound(temp, 2)) = FolderPath
temp(1, UBound(temp, 2)) = Value
temp(2, UBound(temp, 2)) = Count ' FileLen(FolderPath & Value)
ReDim Preserve temp(UBound(temp, 1), UBound(temp, 2) + 1)
End If
End If
End If
End If
End If
Value = Dir
Loop
For Each Folder In Folders
Count = Count + 1
Recursive FolderPath & Folder & "\"
Count = Count - 1
Next Folder
End Function
而且
Public temp() As String
公共計數爲整數 功能ListFiles(FOLDERPATH作爲字符串)
Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
Dim k As Long, i As Long
ReDim temp(2, 0)
Count = 1
If Right(FolderPath, 1) <> "\" Then
FolderPath = FolderPath & "\"
End If
Recursive FolderPath
k = Range(Application.Caller.Address).Rows.Count
If k < UBound(temp, 2) Then
MsgBox "There are more rows, extend user defined function"
Else
For i = UBound(temp, 2) To k
ReDim Preserve temp(UBound(temp, 1), i)
temp(0, i) = ""
temp(1, i) = ""
temp(2, i) = ""
Next i
End If
ListFiles = Application.Transpose(temp)
ReDim temp(0)
End Function
好。讓我們這樣說。但是如果你需要幫助,我們也可以說你嘗試了自己的解決方案,展示你的嘗試,然後解釋你嘗試的不足。那時人們會更感興趣幫助你。 http://stackoverflow.com/help/how-to-ask – Marc
感謝您的建議,但這就是爲什麼我需要幫助。 :) –