如何查看电子表格单元格中的值是否存在于相应的文本文件中?

时间:2018-01-16 03:06:56

标签: excel vba excel-vba

我想看看公司的收入是否存在于相应的txt文件中(C列中的文件路径)。如果列出的收入是否存在,请在D栏中注明?

enter image description here

想知道我的情况是否在VBA中有Excel宏?

谢谢,

编辑:我想我有一些东西,但我想弄清楚如何将这段代码循环到下一行

Sub SearchTextFile()
Dim strFileName As String
strFileName = Sheet1.Range("C2").Value
Dim strSearch As Long
strSearch = Sheet1.Range("b2").Value
Dim strLine As String
Dim f As Integer
Dim lngLine As Long
Dim blnFound As Boolean
f = FreeFile
Open strFileName For Input As #f
Do While Not EOF(f)
    lngLine = lngLine + 1
    Line Input #f, strLine
    If InStr(1, strLine, strSearch, vbBinaryCompare) > 0 Then
        Sheet1.Range("D2") = "Yes"
        MsgBox "Revenue is In the File"
        blnFound = True
        Exit Do
    End If
Loop
Close #f
If Not blnFound Then
    Sheet1.Range("D2") = "No"
    MsgBox "Search string not found", vbInformation
End If
End Sub

1 个答案:

答案 0 :(得分:2)

这是你在宏中循环的方法:

Sub SearchTextFile()
Dim rowCount as Long
rowCount = Range("A1").End(xlDown).Row
For i = 2 To rowCount
'Your code here. Use "i" as a row indicator, e.g. strFileName = Sheet1.Range("C" & i).Value
Next i
End Sub