2015-12-17 16 views
0

假設我有很多以.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 
+0

好。讓我們這樣說。但是如果你需要幫助,我們也可以說你嘗試了自己的解決方案,展示你的嘗試,然後解釋你嘗試的不足。那時人們會更感興趣幫助你。 http://stackoverflow.com/help/how-to-ask – Marc

+0

感謝您的建議,但這就是爲什麼我需要幫助。 :) –

回答

1

StackOverlow是一個社區來回答特定的有針對性的解答,幫助你成長爲一個編碼器。在遇到問題時,我們通常會幫助修復對方的代碼。
如果您正在尋找某人爲您編寫代碼,我建議僱用一名程序員。

現在,爲了讓您開始,我不確定.edf是什麼,但是您可以將文件的文本加載到一個字符串中,並檢查該短語「hihowareyou」是否在其中。

Dim strFilename As String: strFilename = "C:\user\desktop\yourfile.edf" 
Dim strFileContent As String 
Dim iFile As Integer: iFile = FreeFile 
Open strFilename For Input As #iFile 
strFileContent = Input(LOF(iFile), iFile) 
Close #iFile 

上面的代碼將打開在strfilename命名文件,並將其加載到一個字符串變量。
從這裏,你可以查看字符串變量的值,你正在尋找。

if inStr(1,strFileContent,"hihowareyou") <> 0 then 

instr是一個常見的方法來做到這一點。 instr通過字符串變量逐個字符地查看並返回短語開始的位置。如果它不在你的字符串內,它將返回零。

if inStr(1,strFileContent,"hihowareyou") <> 0 then 
    ActiveSheet.Cells(1,1)=strFilename 
    longLoc = inStr(1,strFileContent,"Longitude:") 
    if longLoc <> 0 then 
      ActiveSheet.Cells(1,2)= Mid(strFleContent,longLoc+len("Longitude:"),10) 
    end if 
end if 

最後,在這裏我提供了一個如何處理strFileContent的例子。首先檢查「hihowareyou」。如果它在那裏,它將文件名稱放在單元格A1中。接下來,它會尋找「經度:」如果它存在,它需要經度的未來十個字符出來的文件,並將它們放入電池B2


來源:Read text file into string variable

+0

謝謝,這是一個很大的幫助。不,我不需要完全編寫的代碼,但在我的代碼中,我嘗試了不同的方法來做這件事,但沒有任何工作。這是一個單獨的主題,所以我沒有發佈該代碼。我現在編輯了這篇文章。 –

+0

另外我這次沒有足夠的時間嘗試不同的組合。否則,我不太喜歡問自己是否有足夠的時間來調試。 –

+0

您的代碼與以前一樣給出相同的錯誤。我想我把它與代碼的其餘部分錯誤地整合在一起。你可以參考整個代碼來看看它嗎? –

相關問題