2016-01-21 66 views
-1

我是新來的人,我希望有人能幫助我。我有一個Excel文件,其中包含列A中客戶端的代碼名稱。我還有一個帶有.xml文件的文件夾,這些文件使用Excel文件中列A中的代碼名稱命名。使用Excel從文件中提取信息

我需要的是讓Excel搜索與單元格A1中的代碼相匹配的文件,並從該文件中提取某些信息。然後,我需要它進入單元格B1,併爲其在其中找到的代碼執行相同的操作。有誰知道如何做到這一點?很感謝任何形式的幫助。

+0

你會想循環瀏覽文件夾中的每個文件。以下是[開始搜索](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=vba%20get%20names%20of%20files%20in% 20folder)或[本頁](http://www.ozgrid.com/forum/showthread.php?t=65530)。 – BruceWayne

+0

謝謝!我知道如何檢索文件夾中已命名文件的列表,但如果可能,我需要的是讓Excel通過查找特定單元格中的特定名稱來查找文件夾中的特定名稱,一旦找到該名稱,我需要它從中複製某些信息並放入不同的電子表格中。 – Sil

+0

聽起來像一個非常簡單的'If'語句可以工作。當循環遍歷每個文件時,請執行如下操作:'if myFile.name Like Cells(1,1).Value Then ...'? (也許'喜歡'不是最好的,但基本上你想看看文件名是否類似於單元格A1(或任何你想要的))。 – BruceWayne

回答

-1

要開始用下面的代碼:

Dim filename as String 
Dim idx as integer 
idx = 65 
while idx < 92 
    col = char(idx) 
    filename = Dir(Sheets(1).Range(col + "1").Value) 
    While filename <> "" 
     process file 
     filename = Dir 
    Wend 
    idx = idx 1 
wend 
+0

請注意,如果您需要在初始Dir命令中包含完整文件夾路徑(如果它不在單元格中)。 – PKatona

-1

在您的意見,您對以下內容進行:

我不知道如何將一個文件夾中檢索命名的文件的列表,但我 需要,如果可能的話,是爲Excel通過找到它從一個特定的細胞比賽發現在 文件夾中的特定名稱,一旦發現 這個名字,我需要它的某些信息從它複製,並把 到不同的電子表格中

這裏是一些基本的代碼,演示如何做你正在尋找的東西。它是清晰的評論,並使其更容易定製。

Sub tgr() 

    Dim wb As Workbook 
    Dim wsData As Worksheet 
    Dim wsDest As Worksheet 
    Dim rName As Range 
    Dim sFldrPath As String 
    Dim sFileName As String 

    'Change to correct folder path 
    'Be sure to include ending \ 
    sFldrPath = "C:\Test\" 

    'Change to correct workbook if necessary. 
    'In most cases ActiveWorkbook will be what you want 
    Set wb = ActiveWorkbook 

    'Change to correct worksheet that contains the list of names 
    Set wsData = wb.Sheets("Sheet1") 

    'Change to correct destination worksheet where you will output results 
    Set wsDest = wb.Sheets("Sheet2") 

    'Change to correct range that contains the list of names 
    'This example assumes header row is row 1, actual data starts in row 2, names are in column A 
    With wsData.Range("A2", wsData.Cells(wsData.Rows.Count, "A").End(xlUp)) 
     If .Row < 2 Then Exit Sub 'First row is prior to the row that should contain data 
            'This means there is no data in the specified range, so exit out 

     'Begin iterating over the names in the range 
     For Each rName In .Cells 
      'Get the xml file where the name matches vName 
      sFileName = Dir(sFldrPath & "*" & CStr(rName.Text) & "*.xml") 

      'Did it find a matching file? 
      If Len(sFileName) > 0 Then 
       ''''''''''''''''''''''''''''''''''' 
       '         ' 
       ' Match Found, process the file ' 
       '         ' 
       ''''''''''''''''''''''''''''''''''' 

       'Change this to whatever it is you actually want to do 
       wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Value = sFldrPath & sFileName 

      End If 

     Next rName 'Advance the loop to the next name in the array 
    End With 

End Sub 
+0

謝謝!我會試試這個。 – Sil

+0

我嘗試了這一點,並在代碼中進行了更改,但它僅將文件路徑名添加到目標工作表,然後沒有其他任何我能看到的東西。不知道我做錯了什麼。 – Sil

+0

@Sil請創建一個新問題。在新問題中,請務必將您的*修改過的*代碼與您正在嘗試執行的操作一起發佈。這裏發佈的代碼只是示例代碼,以幫助您入門。 – tigeravatar