如何将 .txt 文件中的单词与数组进行比较并将索引记录在另一个数组中 VBA
How to compare words in a .txt file to an array and record the index in another array VBA
我有这个文本文件,当粘贴到 excel 电子表格中时,它以制表符分隔,大约有 1,000 列和 12,000 行。我的目标是有一些方法可以比较字符串数组
arWords = Array("Title1", "Title2", "Title3")
到该 .txt 文件中的 headers 列。找到匹配项后,我想知道在哪个“列”中找到了该词,并将其放入另一个数组中。在此示例中,它将是一个包含 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
如果我在阅读您的评论后的假设是正确的,请尝试下一个代码。我假设 header 在文本文件的第一行。无需在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
如果 headers 行不总是相同,请给我此 header 行的第 1 列标记,我将调整代码以首先搜索此标记,设置 headers 行并使用它...
我有这个文本文件,当粘贴到 excel 电子表格中时,它以制表符分隔,大约有 1,000 列和 12,000 行。我的目标是有一些方法可以比较字符串数组
arWords = Array("Title1", "Title2", "Title3")
到该 .txt 文件中的 headers 列。找到匹配项后,我想知道在哪个“列”中找到了该词,并将其放入另一个数组中。在此示例中,它将是一个包含 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
如果我在阅读您的评论后的假设是正确的,请尝试下一个代码。我假设 header 在文本文件的第一行。无需在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
如果 headers 行不总是相同,请给我此 header 行的第 1 列标记,我将调整代码以首先搜索此标记,设置 headers 行并使用它...