【问题标题】:How to compare words in a .txt file to an array and record the index in another array VBA如何将 .txt 文件中的单词与数组进行比较并在另一个数组 VBA 中记录索引
【发布时间】:2021-10-15 05:20:04
【问题描述】:

我有这个文本文件,当粘贴到 Excel 电子表格中时,它以制表符分隔,大约有 1,000 列和 12,000 行。我的目标是有一些方法可以比较字符串数组

arWords = Array("Title1", "Title2", "Title3")

到该 .txt 文件中的列标题。找到匹配项时,我想知道在哪个“列”中找到了该单词,并将其放入另一个数组中。在此示例中,它将是一个由 3 个整数组成的数组,每个整数表示找到每个 Title 的列。我的目标是最终得到一个看起来像这样的数组。

listIndex = array(159, 393, 400)

如果我在 arWords 中包含 4 个标题,那么我将得到一个由 4 个整数组成的数组,表示它们的列 #。 这是我的代码,我不擅长这个,但还是谢谢!

Const ForReading = 1
Dim FSO, FileIn, strTmp

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FileIn = FSO.OpenTextFile(movietitles.txt, ForReading)


arWords = Array("Title1", "Title2", "Title3")
size = UBound(arWords) - LBound(arWords) + 1
Dim listIndex() As Integer
ReDim listIndex(size)

Do Until FileIn.AtEndOfStream
    strTmp = FileIn.ReadLine
    If Len(strTmp) > 0 Then
        For i = 0 To UBound(arWords)
            If InStr(1, strTmp, arWords(i), vbTextCompare) > 0 Then
                listIndex(i) = i
                Exit For
            End If
        Next
    End If
Loop

FileIn.Close
     

    ReDim yArray(1 To lColumn)
    Dim Counter As Integer
       For Counter = 1 To lColumn
            yArray(Counter) = 9
        Next Counter
        
    For Each Index In listIndex
        yArray(Index) = 1
    Next Index

【问题讨论】:

  • movietitles.txt 应该是要搜索字符串数组的文本文件吗?如果是,它也必须包含路径,并且所有内容都在双引号之间。现在,讨论的标题在文本文件的第一行吗?
  • 嗨!啊,是的,我也有代码中的路径。文本文件的设置方式很奇怪。因此,当我在 excel 中复制并粘贴数据时,第一列中大约有 200 行包含随机数据。这 200 行的其余列是空的。从第 201 行开始,虽然开始了大约 1000 列和其余 11,800 行的标题(标题下只是数据(数字))
  • 我对您的“澄清”评论一无所知,抱歉... 那么,要搜索字符串数组元素的标题在哪一行? “第一列中有 200 行”有什么相关性?我应该用下一种方式翻译吗?是的,标题在第一行,但是有很多空列,最多 1000 个(或更多)。这种理解应该正确吗?如果没有,请尝试澄清这方面。基本上,标题在哪一行?或者确定这一行的逻辑,如果不是第一行...
  • 那么,标题行应该是201吗?

标签: arrays excel vba import


【解决方案1】:

如果我在阅读您的评论后的假设是正确的,请尝试下一个代码。我假设标题位于文本文件的第一行。无需在 Excel 中打开:

Sub MatchStringArrayToHeaders()
   Dim fileName As String, arWords, arrTxt, arrH, arrFin, El, mtch
   Dim k As Long, headRow As Long i As Long
   fileName = ThisWorkbook.path & "\MyTestFile.txt" 'use here your text file full name
   
   arWords = Array("Title1", "Title2", "Title3")
   ReDim arrFin(UBound(arWords)) 'redim the final array to be returned
   'put all the text file content in an array of rows:
   arrTxt = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fileName, 1).ReadAll, vbCrLf)
   
   'find the header row: __________________________
   For i = 0 To UBound(arrTxt)
      arrH = Split(arrTxt(i), vbTab)
      If UBound(arrH) > 0 Then
        If arrH(1) <> "" Then HeaderRow = i: Exit For
      End If
   Next i
   '_______________________________________________
   arrH = Split(arrTxt(headRow), vbTab)
   
   For Each El In arWords
        mtch = Application.match(El, arrH, 0) 'return the matching header number
        If IsNumeric(mtch) Then               'if a match could be found
            arrFin(k) = mtch: k = k + 1       'put the column number in the final array
        Else
            'if not any match, write in Immediate Window the not matching string
            Debug.Print El & " could not be found in the headers row..."
        End If
   Next
   'Only to visually check the returned array:
   Debug.Print Join(arrFin, "|") 'the obtained array is joined using "|" separator and returned in Immediate Window (`Ctrl + G`, being in VBE).
End Sub

如果标题行并非始终相同,请给我此标题行的第 1 列标记,我将调整代码以首先搜索此标记,设置标题行并使用它...

【讨论】:

  • 感谢您回复@FaneDuru!奇迹般有效!唯一的问题是它无法对第一列进行排序。我很抱歉解释不好。基本上粘贴到excel中的数据看起来像一个“L”的形状。第一列比其他列长。它通常会发生变化,因此这次在标题出现之前的第一列中有 200 行,而下一次数据文件在标题开始填充之前的第一列中可能有 210 行。
  • 我怎样才能自动化它,所以它基本上知道从哪一行开始阅读。
  • @kyle 好的。我将尝试调整代码以搜索第二列中的第一个非空单元格...
  • @kyle 请测试修改后的代码,它应该检测标题行,因为第一个在第二列中具有非空字符串。
  • @kyle 所以,看来我是对的......很高兴我能帮上忙!但是你必须知道,当你问一个问题时,你必须试着穿上那个试图帮助的人的鞋子,并以某种方式想你会成为他。如果您不帮助我帮助您,则帮助过程不起作用,或者消耗的资源超出了应有的范围...... :)
猜你喜欢
  • 1970-01-01
  • 2021-04-17
  • 2017-07-03
  • 2016-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
相关资源
最近更新 更多