使用Excel从文件中提取信息

时间:2016-01-21 18:21:26

标签: excel

我是新来的,我希望有人可以帮助我。我有一个Excel文件,其中包含A列中客户端的代码名称。我还有一个包含.xml文件的文件夹,这些文件以Excel文件中A列中的代码名称命名。

我需要的是Excel搜索与单元格 A1 中的代码匹配的文件,并从该文件中提取某些信息。然后我需要它进入单元格 B1 并对它在那里找到的代码执行相同操作。有谁知道如何实现这一目标?非常感谢任何帮助。

2 个答案:

答案 0 :(得分:-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

答案 1 :(得分:-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