vba - 如何计算文本文件字符串长度并在 VBA 中执行检查
问题描述
在一个文本文件中,我有很多行,每行都是一个字符串。我需要对每一行/字符串进行检查,直到找到一个长度超过 15 个字符的行/字符串。当它找到一个时,它应该显示一个MsgBox
并结束循环。据了解,我需要使用 Do until EOF 循环,但无法进行检查。
Sub OpenFileAsText()
Dim FilePath As String
FilePath = "C:\Users\Default\Desktop\test.csv"
Open FilePath For Input As #1
row_number = 0
Loop Until EOF(1)
Line Input #1, LineFromFile
'cant work out the code to do the check
row_number = row_number + 1
Loop
Close #1
End Sub
解决方案
谢谢你的开始。这段代码可以满足我的需要。
Sub Sample()
Dim filename As String
filename = "C:\Users\Default\Desktop\339734.csv"
Dim file As Integer
file = FreeFile
Open filename For Input As #file
Dim rowNumber As Long
rowNumber = 0
Dim match As String
Do Until EOF(file)
rowNumber = rowNumber + 1
Dim line As String
Line Input #file, line
If Len(line) > 15 Then
match = line
Exit Do
End If
Loop
Close #file
If match <> vbNullString Then
MsgBox "Found"
Else
MsgBox "Did not find"
End If
End Sub
推荐阅读
- laravel - 无法从数据库中获取原始降价值
- php - 如何将自定义 php 插入特定的 html 代码(acf 字段/wordpress)?
- python - 揭示缺失值的数据框
- amazon-web-services - 函数内部或外部的s3接口初始化
- c++ - 如何在 QStyledItemDelegate 中设置 QStyleOptionProgressBar 的方向
- java - 如何使用 Release.apk 进行调试?
- javascript - 如何在 JavaScript 中将父级的值放入其子级函数中?
- android - 安装后flutter_vpn示例应用程序崩溃
- c++ - 无法从 wchar_t 构造 wstring
- python - 尽管没有实现,但缺少 __init__ 方法所需的位置