【问题标题】:How can I write a macro to import a text file into Excel where the text file to be selected is based on a variable in the spreadsheet如何编写宏以将文本文件导入 Excel,其中要选择的文本文件基于电子表格中的变量
【发布时间】:2014-01-10 17:13:33
【问题描述】:

我需要选择一个文本文件以导入 Excel,其中文本文件的名称包含与 Excel 电子表格中的单元格匹配的文本字符串。

例如。

值为“D12345”的单元格

我需要将一个文本文件导入到文本文件名称中包含相同字符串(即“D12345”)的工作表中。

需要从一组文本文件中进行选择。集合中只有 1 个文件将包含匹配的字符串。

希望这是有道理的。

【问题讨论】:

    标签: excel select import vba


    【解决方案1】:

    试试这个:

    Sub SimpleFileListre()
    Dim s As String, FileName As String
    Dim mesage As String
    Range("A:A").Clear
    s = "C:\TestFolder\*.txt"
    sFolder = "C:\TestFolder\"
    FileName = Dir(s)
    Do Until FileName = ""
        If InStr(1, FileName, "D12345") > 0 Then
            Call GetStuff(sFolder & FileName)
        End If
        FileName = Dir()
    Loop
    End Sub
    
    Sub GetStuff(s)
        Close #2
        Open s For Input As #2
        j = 1
         Do While Not EOF(2)
                Line Input #2, TextLine
                Cells(j, 1) = TextLine
                j = j + 1
            Loop
        Close #2
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      • 2011-12-27
      • 2015-03-28
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多