我想看看公司的收入是否存在于相应的txt文件中(C列中的文件路径)。如果列出的收入是否存在,请在D栏中注明?
想知道我的情况是否在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
答案 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